40 #include <linux/types.h>
41 #include <linux/slab.h>
69 static int ocfs2_mknod_locked(
struct ocfs2_super *osb,
73 struct buffer_head **new_fe_bh,
74 struct buffer_head *parent_fe_bh,
78 static int ocfs2_prepare_orphan_dir(
struct ocfs2_super *osb,
79 struct inode **ret_orphan_dir,
84 static int ocfs2_orphan_add(
struct ocfs2_super *osb,
87 struct buffer_head *fe_bh,
90 struct inode *orphan_dir_inode);
92 static int ocfs2_create_symlink_data(
struct ocfs2_super *osb,
98 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
109 trace_ocfs2_lookup(dir, dentry, dentry->
d_name.len,
111 (
unsigned long long)OCFS2_I(dir)->
ip_blkno, 0);
122 ret = ERR_PTR(status);
127 dentry->
d_name.len, &blkno);
168 ret = ERR_PTR(status);
183 trace_ocfs2_lookup_ret(ret);
208 static int ocfs2_mknod(
struct inode *dir,
214 struct buffer_head *parent_fe_bh =
NULL;
218 struct buffer_head *new_fe_bh =
NULL;
219 struct inode *inode =
NULL;
223 int want_clusters = 0;
225 int xattr_credits = 0;
229 int did_quota_inode = 0;
232 int did_block_signals = 0;
234 trace_ocfs2_mknod(dir, dentry, dentry->
d_name.len, dentry->
d_name.name,
235 (
unsigned long long)OCFS2_I(dir)->ip_blkno,
236 (
unsigned long)dev, mode);
256 if (!ocfs2_read_links_count(dirfe)) {
270 dentry->
d_name.len, &lookup);
284 inode = ocfs2_get_init_inode(dir, mode);
305 &xattr_credits, &want_meta);
312 if (
S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
316 if (ocfs2_supports_indexed_dirs(osb))
337 if (IS_ERR(handle)) {
338 status = PTR_ERR(handle);
346 did_block_signals = 1;
354 status = ocfs2_mknod_locked(osb, dir, inode, dev,
355 &new_fe_bh, parent_fe_bh, handle,
364 new_fe_bh, data_ac, meta_ac);
377 ocfs2_add_links_count(dirfe, 1);
382 status =
ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
404 OCFS2_I(dir)->ip_blkno);
410 status = ocfs2_add_entry(handle, dentry, inode,
411 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
418 insert_inode_hash(inode);
422 if (status < 0 && did_quota_inode)
428 if (did_block_signals)
432 brelse(parent_fe_bh);
452 if ((status < 0) && inode) {
464 static int __ocfs2_mknod_locked(
struct inode *dir,
467 struct buffer_head **new_fe_bh,
468 struct buffer_head *parent_fe_bh,
471 u64 fe_blkno,
u64 suballoc_loc,
u16 suballoc_bit)
484 inode->
i_ino = ino_from_blkno(osb->
sb, fe_blkno);
485 OCFS2_I(inode)->ip_blkno = fe_blkno;
490 *new_fe_bh = sb_getblk(osb->
sb, fe_blkno);
507 memset(fe, 0, osb->
sb->s_blocksize);
521 ocfs2_set_links_count(fe, inode->
i_nlink);
537 if (
S_ISDIR(inode->
i_mode) && ocfs2_supports_inline_data(osb)) {
541 ocfs2_max_inline_data_with_xattr(osb->
sb, fe));
552 ocfs2_ci_set_new(osb, INODE_CACHE(inode));
553 if (!ocfs2_mount_local(osb)) {
575 static int ocfs2_mknod_locked(
struct ocfs2_super *osb,
579 struct buffer_head **new_fe_bh,
580 struct buffer_head *parent_fe_bh,
585 u64 suballoc_loc, fe_blkno = 0;
591 inode_ac, &suballoc_loc,
592 &suballoc_bit, &fe_blkno);
598 return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
599 parent_fe_bh, handle, inode_ac,
600 fe_blkno, suballoc_loc, suballoc_bit);
603 static int ocfs2_mkdir(
struct inode *dir,
604 struct dentry *dentry,
609 trace_ocfs2_mkdir(dir, dentry, dentry->
d_name.len, dentry->
d_name.name,
610 OCFS2_I(dir)->ip_blkno, mode);
611 ret = ocfs2_mknod(dir, dentry, mode |
S_IFDIR, 0);
618 static int ocfs2_create(
struct inode *dir,
619 struct dentry *dentry,
625 trace_ocfs2_create(dir, dentry, dentry->
d_name.len, dentry->
d_name.name,
626 (
unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
627 ret = ocfs2_mknod(dir, dentry, mode |
S_IFREG, 0);
634 static int ocfs2_link(
struct dentry *old_dentry,
636 struct dentry *dentry)
639 struct inode *inode = old_dentry->
d_inode;
641 struct buffer_head *fe_bh =
NULL;
642 struct buffer_head *parent_fe_bh =
NULL;
648 trace_ocfs2_link((
unsigned long long)OCFS2_I(inode)->ip_blkno,
676 dentry->
d_name.len, &lookup);
690 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
692 goto out_unlock_inode;
696 if (IS_ERR(handle)) {
697 err = PTR_ERR(handle);
700 goto out_unlock_inode;
715 ocfs2_set_links_count(fe, inode->
i_nlink);
720 err = ocfs2_add_entry(handle, dentry, inode,
721 OCFS2_I(inode)->ip_blkno,
722 parent_fe_bh, &lookup);
724 ocfs2_add_links_count(fe, -1);
749 brelse(parent_fe_bh);
763 static int ocfs2_remote_dentry_delete(
struct dentry *dentry)
776 static inline int inode_is_unlinkable(
struct inode *inode)
789 static int ocfs2_unlink(
struct inode *dir,
790 struct dentry *dentry)
793 int child_locked = 0;
794 struct inode *inode = dentry->
d_inode;
795 struct inode *orphan_dir =
NULL;
799 struct buffer_head *fe_bh =
NULL;
800 struct buffer_head *parent_node_bh =
NULL;
801 handle_t *handle =
NULL;
806 trace_ocfs2_unlink(dir, dentry, dentry->
d_name.len,
808 (
unsigned long long)OCFS2_I(dir)->ip_blkno,
809 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
827 dentry->
d_name.len, &blkno, dir,
835 if (OCFS2_I(inode)->ip_blkno != blkno) {
838 trace_ocfs2_unlink_noent(
839 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
840 (
unsigned long long)blkno,
841 OCFS2_I(inode)->ip_flags);
860 status = ocfs2_remote_dentry_delete(dentry);
868 if (inode_is_unlinkable(inode)) {
869 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
870 OCFS2_I(inode)->ip_blkno,
871 orphan_name, &orphan_insert);
879 if (IS_ERR(handle)) {
880 status = PTR_ERR(handle);
895 if (inode_is_unlinkable(inode)) {
896 status = ocfs2_orphan_add(osb, handle, inode, fe_bh, orphan_name,
897 &orphan_insert, orphan_dir);
914 ocfs2_set_links_count(fe, inode->
i_nlink);
945 brelse(parent_node_bh);
960 static int ocfs2_double_lock(
struct ocfs2_super *osb,
961 struct buffer_head **bh1,
962 struct inode *inode1,
963 struct buffer_head **bh2,
964 struct inode *inode2)
969 struct buffer_head **tmpbh;
970 struct inode *tmpinode;
972 trace_ocfs2_double_lock((
unsigned long long)oi1->
ip_blkno,
1019 trace_ocfs2_double_lock_end(
1020 (
unsigned long long)OCFS2_I(inode1)->ip_blkno,
1021 (
unsigned long long)OCFS2_I(inode2)->ip_blkno);
1029 static void ocfs2_double_unlock(
struct inode *inode1,
struct inode *inode2)
1033 if (inode1 != inode2)
1037 static int ocfs2_rename(
struct inode *old_dir,
1038 struct dentry *old_dentry,
1039 struct inode *new_dir,
1040 struct dentry *new_dentry)
1042 int status = 0,
rename_lock = 0, parents_locked = 0, target_exists = 0;
1043 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
1044 struct inode *old_inode = old_dentry->
d_inode;
1046 struct inode *orphan_dir =
NULL;
1049 struct buffer_head *newfe_bh =
NULL;
1050 struct buffer_head *old_inode_bh =
NULL;
1052 u64 newfe_blkno, old_de_ino;
1053 handle_t *handle =
NULL;
1054 struct buffer_head *old_dir_bh =
NULL;
1055 struct buffer_head *new_dir_bh =
NULL;
1067 trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
1077 if (!
igrab(new_inode))
1102 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1103 &new_dir_bh, new_dir);
1114 new_dir_bh = old_dir_bh;
1136 old_child_locked = 1;
1138 status = ocfs2_remote_dentry_delete(old_dentry);
1145 u64 old_inode_parent;
1150 &old_inode_dot_dot_res);
1156 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1161 if (!new_inode && new_dir != old_dir &&
1162 new_dir->
i_nlink >= ocfs2_link_max(osb)) {
1182 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1191 &newfe_blkno, new_dir,
1192 &target_lookup_res);
1195 if ((status < 0) && (status != -
ENOENT)) {
1204 if (!target_exists && new_inode) {
1217 if (target_exists) {
1226 trace_ocfs2_rename_target_exists(new_dentry->
d_name.len,
1227 new_dentry->
d_name.name);
1231 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1234 trace_ocfs2_rename_disagree(
1235 (
unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1236 (
unsigned long long)newfe_blkno,
1237 OCFS2_I(new_inode)->ip_flags);
1247 new_child_locked = 1;
1249 status = ocfs2_remote_dentry_delete(new_dentry);
1257 trace_ocfs2_rename_over_existing(
1258 (
unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1259 (
unsigned long long)newfe_bh->b_blocknr : 0ULL);
1262 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1263 OCFS2_I(new_inode)->ip_blkno,
1264 orphan_name, &orphan_insert);
1290 if (IS_ERR(handle)) {
1291 status = PTR_ERR(handle);
1297 if (target_exists) {
1299 if (new_inode->
i_nlink != 2 ||
1314 (ocfs2_read_links_count(newfe) == 1)) {
1315 status = ocfs2_orphan_add(osb, handle, new_inode,
1316 newfe_bh, orphan_name,
1317 &orphan_insert, orphan_dir);
1334 ocfs2_set_links_count(newfe, 0);
1336 ocfs2_add_links_count(newfe, -1);
1340 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1341 OCFS2_I(old_inode)->ip_blkno,
1342 new_dir_bh, &target_insert);
1346 mark_inode_dirty(old_inode);
1368 old_dentry->
d_name.len, old_dir,
1385 if (update_dot_dot) {
1387 &old_inode_dot_dot_res, new_dir);
1393 mark_inode_dirty(new_dir);
1396 mark_inode_dirty(old_dir);
1399 mark_inode_dirty(new_inode);
1403 if (old_dir != new_dir) {
1414 if (old_dir_nlink != old_dir->
i_nlink) {
1417 "%llu from %d to %d but bh is NULL!\n",
1418 (
unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1419 (
int)old_dir_nlink, old_dir->
i_nlink);
1423 INODE_CACHE(old_dir),
1427 ocfs2_set_links_count(fe, old_dir->
i_nlink);
1441 ocfs2_double_unlock(old_dir, new_dir);
1443 if (old_child_locked)
1446 if (new_child_locked)
1469 brelse(old_inode_bh);
1483 static int ocfs2_create_symlink_data(
struct ocfs2_super *osb,
1485 struct inode *inode,
1486 const char *symname)
1488 struct buffer_head **bhs =
NULL;
1491 u64 p_blkno, p_blocks;
1492 int virtual, blocks,
status,
i, bytes_left;
1494 bytes_left = i_size_read(inode) + 1;
1499 trace_ocfs2_create_symlink_data((
unsigned long long)inode->
i_blocks,
1500 i_size_read(inode), blocks);
1504 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1510 bhs = kcalloc(blocks,
sizeof(
struct buffer_head *),
GFP_KERNEL);
1534 while(bytes_left > 0) {
1537 bhs[
virtual] = sb_getblk(sb, p_blkno);
1538 if (!bhs[
virtual]) {
1556 memcpy(bhs[
virtual]->b_data, c,
1571 for(i = 0; i < blocks; i++)
1581 static int ocfs2_symlink(
struct inode *dir,
1582 struct dentry *dentry,
1583 const char *symname)
1588 struct inode *inode =
NULL;
1590 struct buffer_head *new_fe_bh =
NULL;
1591 struct buffer_head *parent_fe_bh =
NULL;
1594 handle_t *handle =
NULL;
1598 int want_clusters = 0;
1599 int xattr_credits = 0;
1603 int did_quota = 0, did_quota_inode = 0;
1606 int did_block_signals = 0;
1608 trace_ocfs2_symlink_begin(dir, dentry, symname,
1618 credits = ocfs2_calc_symlink_credits(sb);
1629 if (!ocfs2_read_links_count(dirfe)) {
1642 dentry->
d_name.len, &lookup);
1676 &xattr_credits, &xattr_ac);
1684 if (l > ocfs2_fast_symlink_chars(sb))
1695 if (IS_ERR(handle)) {
1696 status = PTR_ERR(handle);
1704 did_block_signals = 1;
1709 did_quota_inode = 1;
1711 trace_ocfs2_symlink_create(dir, dentry, dentry->
d_name.len,
1713 (
unsigned long long)OCFS2_I(dir)->ip_blkno,
1716 status = ocfs2_mknod_locked(osb, dir, inode,
1717 0, &new_fe_bh, parent_fe_bh, handle,
1728 if (l > ocfs2_fast_symlink_chars(sb)) {
1731 status = dquot_alloc_space_nodirty(inode,
1732 ocfs2_clusters_to_bytes(osb->
sb, 1));
1739 handle, data_ac,
NULL,
1744 "Failed to extend file to %llu\n",
1745 (
unsigned long long)newsize);
1751 i_size_write(inode, newsize);
1752 inode->
i_blocks = ocfs2_inode_sector_count(inode);
1756 i_size_write(inode, newsize);
1766 if (!ocfs2_inode_is_fast_symlink(inode)) {
1767 status = ocfs2_create_symlink_data(osb, handle, inode,
1795 status = ocfs2_add_entry(handle, dentry, inode,
1803 insert_inode_hash(inode);
1806 if (status < 0 && did_quota)
1807 dquot_free_space_nodirty(inode,
1808 ocfs2_clusters_to_bytes(osb->
sb, 1));
1809 if (status < 0 && did_quota_inode)
1815 if (did_block_signals)
1819 brelse(parent_fe_bh);
1829 if ((status < 0) && inode) {
1841 static int ocfs2_blkno_stringify(
u64 blkno,
char *
name)
1861 trace_ocfs2_blkno_stringify(blkno, name, namelen);
1870 static int ocfs2_lookup_lock_orphan_dir(
struct ocfs2_super *osb,
1871 struct inode **ret_orphan_dir,
1872 struct buffer_head **ret_orphan_dir_bh)
1874 struct inode *orphan_dir_inode;
1875 struct buffer_head *orphan_dir_bh =
NULL;
1881 if (!orphan_dir_inode) {
1892 iput(orphan_dir_inode);
1898 *ret_orphan_dir = orphan_dir_inode;
1899 *ret_orphan_dir_bh = orphan_dir_bh;
1904 static int __ocfs2_prepare_orphan_dir(
struct inode *orphan_dir_inode,
1905 struct buffer_head *orphan_dir_bh,
1913 ret = ocfs2_blkno_stringify(blkno, name);
1920 orphan_dir_bh, name,
1944 static int ocfs2_prepare_orphan_dir(
struct ocfs2_super *osb,
1945 struct inode **ret_orphan_dir,
1950 struct inode *orphan_dir_inode =
NULL;
1951 struct buffer_head *orphan_dir_bh =
NULL;
1954 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
1961 ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
1962 blkno, name, lookup);
1968 *ret_orphan_dir = orphan_dir_inode;
1971 brelse(orphan_dir_bh);
1976 iput(orphan_dir_inode);
1984 static int ocfs2_orphan_add(
struct ocfs2_super *osb,
1986 struct inode *inode,
1987 struct buffer_head *fe_bh,
1990 struct inode *orphan_dir_inode)
1992 struct buffer_head *orphan_dir_bh =
NULL;
1997 trace_ocfs2_orphan_add_begin(
1998 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
2007 INODE_CACHE(orphan_dir_inode),
2017 orphan_fe = (
struct ocfs2_dinode *) orphan_dir_bh->b_data;
2019 ocfs2_add_links_count(orphan_fe, 1);
2020 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2025 OCFS2_I(inode)->ip_blkno,
2026 orphan_dir_bh, lookup);
2057 trace_ocfs2_orphan_add_end((
unsigned long long)OCFS2_I(inode)->ip_blkno,
2061 brelse(orphan_dir_bh);
2071 struct inode *orphan_dir_inode,
2072 struct inode *inode,
2073 struct buffer_head *orphan_dir_bh)
2080 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2086 trace_ocfs2_orphan_del(
2087 (
unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2106 INODE_CACHE(orphan_dir_inode),
2115 orphan_fe = (
struct ocfs2_dinode *) orphan_dir_bh->b_data;
2117 ocfs2_add_links_count(orphan_fe, -1);
2118 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2154 static int ocfs2_prep_new_orphaned_file(
struct inode *dir,
2155 struct buffer_head *dir_bh,
2157 struct inode **ret_orphan_dir,
2165 struct inode *orphan_dir =
NULL;
2166 struct buffer_head *orphan_dir_bh =
NULL;
2169 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
2190 ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
2191 di_blkno, orphan_name, orphan_insert);
2199 *ret_orphan_dir = orphan_dir;
2200 *ret_di_blkno = di_blkno;
2201 *ret_inode_ac = inode_ac;
2217 brelse(orphan_dir_bh);
2224 struct inode **new_inode)
2226 int status, did_quota_inode = 0;
2227 struct inode *inode =
NULL;
2228 struct inode *orphan_dir =
NULL;
2231 handle_t *handle =
NULL;
2233 struct buffer_head *parent_di_bh =
NULL;
2234 struct buffer_head *new_di_bh =
NULL;
2247 status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
2248 orphan_name, &orphan_dir,
2249 &di_blkno, &orphan_insert, &inode_ac);
2256 inode = ocfs2_get_init_inode(dir, mode);
2264 if (IS_ERR(handle)) {
2265 status = PTR_ERR(handle);
2274 did_quota_inode = 1;
2278 &suballoc_bit, di_blkno);
2286 status = __ocfs2_mknod_locked(dir, inode,
2287 0, &new_di_bh, parent_di_bh, handle,
2288 inode_ac, di_blkno, suballoc_loc,
2296 status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
2297 &orphan_insert, orphan_dir);
2308 insert_inode_hash(inode);
2310 if (status < 0 && did_quota_inode)
2322 if ((status < 0) && inode) {
2338 brelse(parent_di_bh);
2343 struct inode *inode,
2344 struct dentry *dentry)
2347 struct buffer_head *parent_di_bh =
NULL;
2348 handle_t *handle =
NULL;
2351 struct inode *orphan_dir_inode =
NULL;
2352 struct buffer_head *orphan_dir_bh =
NULL;
2353 struct buffer_head *di_bh =
NULL;
2356 trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
2358 (
unsigned long long)OCFS2_I(dir)->ip_blkno,
2359 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
2383 dentry->
d_name.len, &lookup);
2392 if (!orphan_dir_inode) {
2404 iput(orphan_dir_inode);
2415 if (IS_ERR(handle)) {
2416 status = PTR_ERR(handle);
2440 ocfs2_set_links_count(di, inode->
i_nlink);
2443 status = ocfs2_add_entry(handle, dentry, inode,
2444 OCFS2_I(inode)->ip_blkno, parent_di_bh,
2452 OCFS2_I(dir)->ip_blkno);
2465 iput(orphan_dir_inode);
2471 brelse(parent_di_bh);
2472 brelse(orphan_dir_bh);
2483 .create = ocfs2_create,
2484 .lookup = ocfs2_lookup,
2486 .unlink = ocfs2_unlink,
2487 .rmdir = ocfs2_unlink,
2488 .symlink = ocfs2_symlink,
2489 .mkdir = ocfs2_mkdir,
2490 .mknod = ocfs2_mknod,
2491 .rename = ocfs2_rename,