]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
2bd0ea18 | 5 | */ |
9c799827 | 6 | #include "libxfs_priv.h" |
b626fb59 | 7 | #include "xfs_fs.h" |
27846843 | 8 | #include "xfs_shared.h" |
b626fb59 DC |
9 | #include "xfs_format.h" |
10 | #include "xfs_log_format.h" | |
11 | #include "xfs_trans_resv.h" | |
12 | #include "xfs_mount.h" | |
b626fb59 DC |
13 | #include "xfs_inode.h" |
14 | #include "xfs_trans.h" | |
15 | #include "xfs_bmap.h" | |
16 | #include "xfs_dir2.h" | |
17 | #include "xfs_dir2_priv.h" | |
2cf10e4c | 18 | #include "xfs_errortag.h" |
b626fb59 | 19 | #include "xfs_trace.h" |
bcf1c967 | 20 | #include "xfs_health.h" |
7ce57cdc DW |
21 | #include "xfs_bmap_btree.h" |
22 | #include "xfs_trans_space.h" | |
23 | #include "xfs_parent.h" | |
92650440 | 24 | #include "xfs_ag.h" |
70696163 | 25 | #include "xfs_ialloc.h" |
2bd0ea18 | 26 | |
f040050e DW |
27 | const struct xfs_name xfs_name_dotdot = { |
28 | .name = (const unsigned char *)"..", | |
29 | .len = 2, | |
30 | .type = XFS_DIR3_FT_DIR, | |
31 | }; | |
494434d7 | 32 | |
36da3ad5 DW |
33 | const struct xfs_name xfs_name_dot = { |
34 | .name = (const unsigned char *)".", | |
35 | .len = 1, | |
36 | .type = XFS_DIR3_FT_DIR, | |
37 | }; | |
38 | ||
aaca101b | 39 | /* |
3297e0ca | 40 | * Convert inode mode to directory entry filetype |
aaca101b | 41 | */ |
16c101bb DW |
42 | unsigned char |
43 | xfs_mode_to_ftype( | |
44 | int mode) | |
3297e0ca AG |
45 | { |
46 | switch (mode & S_IFMT) { | |
47 | case S_IFREG: | |
48 | return XFS_DIR3_FT_REG_FILE; | |
49 | case S_IFDIR: | |
50 | return XFS_DIR3_FT_DIR; | |
51 | case S_IFCHR: | |
52 | return XFS_DIR3_FT_CHRDEV; | |
53 | case S_IFBLK: | |
54 | return XFS_DIR3_FT_BLKDEV; | |
55 | case S_IFIFO: | |
56 | return XFS_DIR3_FT_FIFO; | |
57 | case S_IFSOCK: | |
58 | return XFS_DIR3_FT_SOCK; | |
59 | case S_IFLNK: | |
60 | return XFS_DIR3_FT_SYMLINK; | |
61 | default: | |
62 | return XFS_DIR3_FT_UNKNOWN; | |
63 | } | |
64 | } | |
51ca7008 BN |
65 | |
66 | /* | |
5e656dbb BN |
67 | * ASCII case-insensitive (ie. A-Z) support for directories that was |
68 | * used in IRIX. | |
51ca7008 | 69 | */ |
e169cc9b | 70 | xfs_dahash_t |
51ca7008 | 71 | xfs_ascii_ci_hashname( |
c331b654 | 72 | const struct xfs_name *name) |
51ca7008 | 73 | { |
c331b654 DW |
74 | xfs_dahash_t hash; |
75 | int i; | |
51ca7008 | 76 | |
5e656dbb | 77 | for (i = 0, hash = 0; i < name->len; i++) |
bd970a73 | 78 | hash = xfs_ascii_ci_xfrm(name->name[i]) ^ rol32(hash, 7); |
51ca7008 BN |
79 | |
80 | return hash; | |
81 | } | |
82 | ||
e169cc9b | 83 | enum xfs_dacmp |
51ca7008 | 84 | xfs_ascii_ci_compname( |
e169cc9b CH |
85 | struct xfs_da_args *args, |
86 | const unsigned char *name, | |
87 | int len) | |
51ca7008 | 88 | { |
e169cc9b CH |
89 | enum xfs_dacmp result; |
90 | int i; | |
51ca7008 | 91 | |
5e656dbb | 92 | if (args->namelen != len) |
51ca7008 BN |
93 | return XFS_CMP_DIFFERENT; |
94 | ||
95 | result = XFS_CMP_EXACT; | |
5e656dbb BN |
96 | for (i = 0; i < len; i++) { |
97 | if (args->name[i] == name[i]) | |
51ca7008 | 98 | continue; |
bd970a73 DW |
99 | if (xfs_ascii_ci_xfrm(args->name[i]) != |
100 | xfs_ascii_ci_xfrm(name[i])) | |
51ca7008 BN |
101 | return XFS_CMP_DIFFERENT; |
102 | result = XFS_CMP_CASE; | |
103 | } | |
104 | ||
105 | return result; | |
106 | } | |
107 | ||
ff105f75 DC |
108 | int |
109 | xfs_da_mount( | |
110 | struct xfs_mount *mp) | |
2bd0ea18 | 111 | { |
ff105f75 | 112 | struct xfs_da_geometry *dageo; |
ed59338e DC |
113 | |
114 | ||
ff105f75 | 115 | ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT); |
fdef0e8b | 116 | ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE); |
ff105f75 | 117 | |
cdc4e34c DC |
118 | mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry), |
119 | GFP_KERNEL | __GFP_RETRY_MAYFAIL); | |
120 | mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry), | |
121 | GFP_KERNEL | __GFP_RETRY_MAYFAIL); | |
ff105f75 | 122 | if (!mp->m_dir_geo || !mp->m_attr_geo) { |
336b689a DC |
123 | kfree(mp->m_dir_geo); |
124 | kfree(mp->m_attr_geo); | |
12b53197 | 125 | return -ENOMEM; |
ff105f75 DC |
126 | } |
127 | ||
128 | /* set up directory geometry */ | |
129 | dageo = mp->m_dir_geo; | |
130 | dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog; | |
131 | dageo->fsblog = mp->m_sb.sb_blocklog; | |
fdef0e8b | 132 | dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb); |
ff105f75 | 133 | dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog; |
94541a16 | 134 | if (xfs_has_crc(mp)) { |
52be9b6a | 135 | dageo->node_hdr_size = sizeof(struct xfs_da3_node_hdr); |
99e7b975 | 136 | dageo->leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr); |
6006410b | 137 | dageo->free_hdr_size = sizeof(struct xfs_dir3_free_hdr); |
58a1d356 CH |
138 | dageo->data_entry_offset = |
139 | sizeof(struct xfs_dir3_data_hdr); | |
99e7b975 | 140 | } else { |
52be9b6a | 141 | dageo->node_hdr_size = sizeof(struct xfs_da_node_hdr); |
99e7b975 | 142 | dageo->leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr); |
6006410b | 143 | dageo->free_hdr_size = sizeof(struct xfs_dir2_free_hdr); |
58a1d356 CH |
144 | dageo->data_entry_offset = |
145 | sizeof(struct xfs_dir2_data_hdr); | |
99e7b975 | 146 | } |
d106a3e0 CH |
147 | dageo->leaf_max_ents = (dageo->blksize - dageo->leaf_hdr_size) / |
148 | sizeof(struct xfs_dir2_leaf_entry); | |
ae3cd5b1 CH |
149 | dageo->free_max_bests = (dageo->blksize - dageo->free_hdr_size) / |
150 | sizeof(xfs_dir2_data_off_t); | |
ff105f75 | 151 | |
58a1d356 CH |
152 | dageo->data_first_offset = dageo->data_entry_offset + |
153 | xfs_dir2_data_entsize(mp, 1) + | |
154 | xfs_dir2_data_entsize(mp, 2); | |
155 | ||
ff105f75 DC |
156 | /* |
157 | * Now we've set up the block conversion variables, we can calculate the | |
158 | * segment block constants using the geometry structure. | |
159 | */ | |
160 | dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET); | |
161 | dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET); | |
162 | dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET); | |
52be9b6a | 163 | dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) / |
ed59338e | 164 | (uint)sizeof(xfs_da_node_entry_t); |
f0683d63 CB |
165 | dageo->max_extents = (XFS_DIR2_MAX_SPACES * XFS_DIR2_SPACE_SIZE) >> |
166 | mp->m_sb.sb_blocklog; | |
ff105f75 DC |
167 | dageo->magicpct = (dageo->blksize * 37) / 100; |
168 | ||
169 | /* set up attribute geometry - single fsb only */ | |
170 | dageo = mp->m_attr_geo; | |
171 | dageo->blklog = mp->m_sb.sb_blocklog; | |
172 | dageo->fsblog = mp->m_sb.sb_blocklog; | |
173 | dageo->blksize = 1 << dageo->blklog; | |
174 | dageo->fsbcount = 1; | |
52be9b6a CH |
175 | dageo->node_hdr_size = mp->m_dir_geo->node_hdr_size; |
176 | dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) / | |
ed59338e | 177 | (uint)sizeof(xfs_da_node_entry_t); |
f0683d63 CB |
178 | |
179 | if (xfs_has_large_extent_counts(mp)) | |
180 | dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_LARGE; | |
181 | else | |
182 | dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_SMALL; | |
183 | ||
ff105f75 | 184 | dageo->magicpct = (dageo->blksize * 37) / 100; |
ff105f75 DC |
185 | return 0; |
186 | } | |
187 | ||
188 | void | |
189 | xfs_da_unmount( | |
190 | struct xfs_mount *mp) | |
191 | { | |
336b689a DC |
192 | kfree(mp->m_dir_geo); |
193 | kfree(mp->m_attr_geo); | |
2bd0ea18 NS |
194 | } |
195 | ||
5e656dbb BN |
196 | /* |
197 | * Return 1 if directory contains only "." and "..". | |
198 | */ | |
e4d11913 | 199 | static bool |
5e656dbb BN |
200 | xfs_dir_isempty( |
201 | xfs_inode_t *dp) | |
202 | { | |
a2ceac1f | 203 | xfs_dir2_sf_hdr_t *sfp; |
5e656dbb | 204 | |
e37bf53c | 205 | ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); |
509dcb4b | 206 | if (dp->i_disk_size == 0) /* might happen during shutdown. */ |
e4d11913 | 207 | return true; |
eae3e30d | 208 | if (dp->i_disk_size > xfs_inode_data_fork_size(dp)) |
e4d11913 | 209 | return false; |
f8146197 | 210 | sfp = dp->i_df.if_data; |
a2ceac1f | 211 | return !sfp->count; |
5e656dbb BN |
212 | } |
213 | ||
214 | /* | |
215 | * Validate a given inode number. | |
216 | */ | |
217 | int | |
218 | xfs_dir_ino_validate( | |
219 | xfs_mount_t *mp, | |
220 | xfs_ino_t ino) | |
221 | { | |
be3bf25c DW |
222 | bool ino_ok = xfs_verify_dir_ino(mp, ino); |
223 | ||
bc73da84 DW |
224 | if (XFS_IS_CORRUPT(mp, !ino_ok) || |
225 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DIR_INO_VALIDATE)) { | |
a2ceac1f | 226 | xfs_warn(mp, "Invalid inode number 0x%Lx", |
5e656dbb | 227 | (unsigned long long) ino); |
12b53197 | 228 | return -EFSCORRUPTED; |
5e656dbb BN |
229 | } |
230 | return 0; | |
231 | } | |
232 | ||
2bd0ea18 NS |
233 | /* |
234 | * Initialize a directory with its "." and ".." entries. | |
235 | */ | |
5e656dbb BN |
236 | int |
237 | xfs_dir_init( | |
238 | xfs_trans_t *tp, | |
239 | xfs_inode_t *dp, | |
240 | xfs_inode_t *pdp) | |
2bd0ea18 | 241 | { |
ff105f75 | 242 | struct xfs_da_args *args; |
5e656dbb | 243 | int error; |
2bd0ea18 | 244 | |
e37bf53c | 245 | ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); |
ff105f75 DC |
246 | error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino); |
247 | if (error) | |
2bd0ea18 | 248 | return error; |
ff105f75 | 249 | |
a6cb3f66 | 250 | args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); |
ff105f75 | 251 | if (!args) |
12b53197 | 252 | return -ENOMEM; |
ff105f75 DC |
253 | |
254 | args->geo = dp->i_mount->m_dir_geo; | |
255 | args->dp = dp; | |
256 | args->trans = tp; | |
0e232547 | 257 | args->owner = dp->i_ino; |
ff105f75 | 258 | error = xfs_dir2_sf_create(args, pdp->i_ino); |
336b689a | 259 | kfree(args); |
ff105f75 | 260 | return error; |
2bd0ea18 NS |
261 | } |
262 | ||
3cedb37a CH |
263 | enum xfs_dir2_fmt |
264 | xfs_dir2_format( | |
265 | struct xfs_da_args *args, | |
266 | int *error) | |
267 | { | |
268 | struct xfs_inode *dp = args->dp; | |
269 | struct xfs_mount *mp = dp->i_mount; | |
270 | struct xfs_da_geometry *geo = mp->m_dir_geo; | |
271 | xfs_fileoff_t eof; | |
272 | ||
273 | xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL); | |
274 | ||
275 | *error = 0; | |
276 | if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) | |
277 | return XFS_DIR2_FMT_SF; | |
278 | ||
279 | *error = xfs_bmap_last_offset(dp, &eof, XFS_DATA_FORK); | |
280 | if (*error) | |
281 | return XFS_DIR2_FMT_ERROR; | |
282 | ||
283 | if (eof == XFS_B_TO_FSB(mp, geo->blksize)) { | |
284 | if (XFS_IS_CORRUPT(mp, dp->i_disk_size != geo->blksize)) { | |
285 | xfs_da_mark_sick(args); | |
286 | *error = -EFSCORRUPTED; | |
287 | return XFS_DIR2_FMT_ERROR; | |
288 | } | |
289 | return XFS_DIR2_FMT_BLOCK; | |
290 | } | |
291 | if (eof == geo->leafblk + geo->fsbcount) | |
292 | return XFS_DIR2_FMT_LEAF; | |
293 | return XFS_DIR2_FMT_NODE; | |
294 | } | |
295 | ||
c201c18f CH |
296 | int |
297 | xfs_dir_createname_args( | |
298 | struct xfs_da_args *args) | |
299 | { | |
c201c18f CH |
300 | int error; |
301 | ||
302 | if (!args->inumber) | |
303 | args->op_flags |= XFS_DA_OP_JUSTCHECK; | |
304 | ||
3cedb37a CH |
305 | switch (xfs_dir2_format(args, &error)) { |
306 | case XFS_DIR2_FMT_SF: | |
c201c18f | 307 | return xfs_dir2_sf_addname(args); |
3cedb37a | 308 | case XFS_DIR2_FMT_BLOCK: |
c201c18f | 309 | return xfs_dir2_block_addname(args); |
3cedb37a | 310 | case XFS_DIR2_FMT_LEAF: |
c201c18f | 311 | return xfs_dir2_leaf_addname(args); |
3cedb37a CH |
312 | case XFS_DIR2_FMT_NODE: |
313 | return xfs_dir2_node_addname(args); | |
314 | default: | |
315 | return error; | |
316 | } | |
c201c18f CH |
317 | } |
318 | ||
2bd0ea18 | 319 | /* |
5a35bf2c DC |
320 | * Enter a name in a directory, or check for available space. |
321 | * If inum is 0, only the available space test is performed. | |
2bd0ea18 | 322 | */ |
5e656dbb BN |
323 | int |
324 | xfs_dir_createname( | |
a50d2ab0 BF |
325 | struct xfs_trans *tp, |
326 | struct xfs_inode *dp, | |
c331b654 | 327 | const struct xfs_name *name, |
2bd0ea18 | 328 | xfs_ino_t inum, /* new entry inode number */ |
2bd0ea18 NS |
329 | xfs_extlen_t total) /* bmap's total block count */ |
330 | { | |
ff105f75 | 331 | struct xfs_da_args *args; |
5e656dbb | 332 | int rval; |
2bd0ea18 | 333 | |
e37bf53c | 334 | ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); |
44be3822 | 335 | |
5a35bf2c DC |
336 | if (inum) { |
337 | rval = xfs_dir_ino_validate(tp->t_mountp, inum); | |
338 | if (rval) | |
339 | return rval; | |
79896434 | 340 | XFS_STATS_INC(dp->i_mount, xs_dir_create); |
5a35bf2c | 341 | } |
5e656dbb | 342 | |
a6cb3f66 | 343 | args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); |
ff105f75 | 344 | if (!args) |
12b53197 | 345 | return -ENOMEM; |
ff105f75 DC |
346 | |
347 | args->geo = dp->i_mount->m_dir_geo; | |
348 | args->name = name->name; | |
349 | args->namelen = name->len; | |
350 | args->filetype = name->type; | |
e169cc9b | 351 | args->hashval = xfs_dir2_hashname(dp->i_mount, name); |
ff105f75 DC |
352 | args->inumber = inum; |
353 | args->dp = dp; | |
ff105f75 DC |
354 | args->total = total; |
355 | args->whichfork = XFS_DATA_FORK; | |
356 | args->trans = tp; | |
357 | args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT; | |
0e232547 | 358 | args->owner = dp->i_ino; |
ff105f75 | 359 | |
c201c18f | 360 | rval = xfs_dir_createname_args(args); |
336b689a | 361 | kfree(args); |
2bd0ea18 NS |
362 | return rval; |
363 | } | |
364 | ||
5e656dbb BN |
365 | /* |
366 | * If doing a CI lookup and case-insensitive match, dup actual name into | |
367 | * args.value. Return EEXIST for success (ie. name found) or an error. | |
368 | */ | |
369 | int | |
370 | xfs_dir_cilookup_result( | |
371 | struct xfs_da_args *args, | |
56b2de80 | 372 | const unsigned char *name, |
5e656dbb BN |
373 | int len) |
374 | { | |
375 | if (args->cmpresult == XFS_CMP_DIFFERENT) | |
12b53197 | 376 | return -ENOENT; |
5e656dbb BN |
377 | if (args->cmpresult != XFS_CMP_CASE || |
378 | !(args->op_flags & XFS_DA_OP_CILOOKUP)) | |
12b53197 | 379 | return -EEXIST; |
5e656dbb | 380 | |
dce20b26 | 381 | args->value = kmemdup(name, len, |
c3bf5a1f | 382 | GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL); |
5e656dbb | 383 | if (!args->value) |
12b53197 | 384 | return -ENOMEM; |
5e656dbb | 385 | |
5e656dbb | 386 | args->valuelen = len; |
12b53197 | 387 | return -EEXIST; |
5e656dbb BN |
388 | } |
389 | ||
7ae5bcb2 CH |
390 | int |
391 | xfs_dir_lookup_args( | |
392 | struct xfs_da_args *args) | |
393 | { | |
7ae5bcb2 CH |
394 | int error; |
395 | ||
3cedb37a CH |
396 | switch (xfs_dir2_format(args, &error)) { |
397 | case XFS_DIR2_FMT_SF: | |
7ae5bcb2 | 398 | error = xfs_dir2_sf_lookup(args); |
3cedb37a CH |
399 | break; |
400 | case XFS_DIR2_FMT_BLOCK: | |
7ae5bcb2 | 401 | error = xfs_dir2_block_lookup(args); |
3cedb37a CH |
402 | break; |
403 | case XFS_DIR2_FMT_LEAF: | |
7ae5bcb2 | 404 | error = xfs_dir2_leaf_lookup(args); |
3cedb37a CH |
405 | break; |
406 | case XFS_DIR2_FMT_NODE: | |
7ae5bcb2 | 407 | error = xfs_dir2_node_lookup(args); |
3cedb37a CH |
408 | break; |
409 | default: | |
410 | break; | |
411 | } | |
412 | ||
7ae5bcb2 CH |
413 | if (error != -EEXIST) |
414 | return error; | |
415 | return 0; | |
416 | } | |
417 | ||
2bd0ea18 NS |
418 | /* |
419 | * Lookup a name in a directory, give back the inode number. | |
5e656dbb BN |
420 | * If ci_name is not NULL, returns the actual name in ci_name if it differs |
421 | * to name, or ci_name->name is set to NULL for an exact match. | |
2bd0ea18 | 422 | */ |
5e656dbb BN |
423 | |
424 | int | |
425 | xfs_dir_lookup( | |
c331b654 DW |
426 | struct xfs_trans *tp, |
427 | struct xfs_inode *dp, | |
428 | const struct xfs_name *name, | |
429 | xfs_ino_t *inum, /* out: inode number */ | |
430 | struct xfs_name *ci_name) /* out: actual name if CI match */ | |
2bd0ea18 | 431 | { |
c331b654 DW |
432 | struct xfs_da_args *args; |
433 | int rval; | |
c331b654 | 434 | int lock_mode; |
2bd0ea18 | 435 | |
e37bf53c | 436 | ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); |
79896434 | 437 | XFS_STATS_INC(dp->i_mount, xs_dir_lookup); |
a95cf252 | 438 | |
c3bf5a1f DC |
439 | args = kzalloc(sizeof(*args), |
440 | GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL); | |
ff105f75 DC |
441 | args->geo = dp->i_mount->m_dir_geo; |
442 | args->name = name->name; | |
443 | args->namelen = name->len; | |
444 | args->filetype = name->type; | |
e169cc9b | 445 | args->hashval = xfs_dir2_hashname(dp->i_mount, name); |
ff105f75 DC |
446 | args->dp = dp; |
447 | args->whichfork = XFS_DATA_FORK; | |
448 | args->trans = tp; | |
449 | args->op_flags = XFS_DA_OP_OKNOENT; | |
0e232547 | 450 | args->owner = dp->i_ino; |
5e656dbb | 451 | if (ci_name) |
ff105f75 | 452 | args->op_flags |= XFS_DA_OP_CILOOKUP; |
5e656dbb | 453 | |
7ebb7646 | 454 | lock_mode = xfs_ilock_data_map_shared(dp); |
7ae5bcb2 | 455 | rval = xfs_dir_lookup_args(args); |
5e656dbb | 456 | if (!rval) { |
ff105f75 | 457 | *inum = args->inumber; |
5e656dbb | 458 | if (ci_name) { |
ff105f75 DC |
459 | ci_name->name = args->value; |
460 | ci_name->len = args->valuelen; | |
5e656dbb BN |
461 | } |
462 | } | |
7ebb7646 | 463 | xfs_iunlock(dp, lock_mode); |
336b689a | 464 | kfree(args); |
2bd0ea18 NS |
465 | return rval; |
466 | } | |
467 | ||
b649719d CH |
468 | int |
469 | xfs_dir_removename_args( | |
470 | struct xfs_da_args *args) | |
471 | { | |
b649719d CH |
472 | int error; |
473 | ||
3cedb37a CH |
474 | switch (xfs_dir2_format(args, &error)) { |
475 | case XFS_DIR2_FMT_SF: | |
b649719d | 476 | return xfs_dir2_sf_removename(args); |
3cedb37a | 477 | case XFS_DIR2_FMT_BLOCK: |
b649719d | 478 | return xfs_dir2_block_removename(args); |
3cedb37a | 479 | case XFS_DIR2_FMT_LEAF: |
b649719d | 480 | return xfs_dir2_leaf_removename(args); |
3cedb37a CH |
481 | case XFS_DIR2_FMT_NODE: |
482 | return xfs_dir2_node_removename(args); | |
483 | default: | |
484 | return error; | |
485 | } | |
b649719d CH |
486 | } |
487 | ||
2bd0ea18 NS |
488 | /* |
489 | * Remove an entry from a directory. | |
490 | */ | |
5e656dbb BN |
491 | int |
492 | xfs_dir_removename( | |
a50d2ab0 BF |
493 | struct xfs_trans *tp, |
494 | struct xfs_inode *dp, | |
b813c7e5 | 495 | const struct xfs_name *name, |
a50d2ab0 | 496 | xfs_ino_t ino, |
a50d2ab0 | 497 | xfs_extlen_t total) /* bmap's total block count */ |
2bd0ea18 | 498 | { |
a50d2ab0 BF |
499 | struct xfs_da_args *args; |
500 | int rval; | |
2bd0ea18 | 501 | |
e37bf53c | 502 | ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); |
79896434 | 503 | XFS_STATS_INC(dp->i_mount, xs_dir_remove); |
5e656dbb | 504 | |
a6cb3f66 | 505 | args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); |
ff105f75 | 506 | if (!args) |
12b53197 | 507 | return -ENOMEM; |
ff105f75 DC |
508 | |
509 | args->geo = dp->i_mount->m_dir_geo; | |
510 | args->name = name->name; | |
511 | args->namelen = name->len; | |
512 | args->filetype = name->type; | |
e169cc9b | 513 | args->hashval = xfs_dir2_hashname(dp->i_mount, name); |
ff105f75 DC |
514 | args->inumber = ino; |
515 | args->dp = dp; | |
ff105f75 DC |
516 | args->total = total; |
517 | args->whichfork = XFS_DATA_FORK; | |
518 | args->trans = tp; | |
0e232547 | 519 | args->owner = dp->i_ino; |
b649719d | 520 | rval = xfs_dir_removename_args(args); |
336b689a | 521 | kfree(args); |
2bd0ea18 NS |
522 | return rval; |
523 | } | |
524 | ||
fcdabef8 CH |
525 | int |
526 | xfs_dir_replace_args( | |
527 | struct xfs_da_args *args) | |
528 | { | |
fcdabef8 CH |
529 | int error; |
530 | ||
3cedb37a CH |
531 | switch (xfs_dir2_format(args, &error)) { |
532 | case XFS_DIR2_FMT_SF: | |
fcdabef8 | 533 | return xfs_dir2_sf_replace(args); |
3cedb37a | 534 | case XFS_DIR2_FMT_BLOCK: |
fcdabef8 | 535 | return xfs_dir2_block_replace(args); |
3cedb37a | 536 | case XFS_DIR2_FMT_LEAF: |
fcdabef8 | 537 | return xfs_dir2_leaf_replace(args); |
3cedb37a CH |
538 | case XFS_DIR2_FMT_NODE: |
539 | return xfs_dir2_node_replace(args); | |
540 | default: | |
541 | return error; | |
542 | } | |
fcdabef8 CH |
543 | } |
544 | ||
2bd0ea18 NS |
545 | /* |
546 | * Replace the inode number of a directory entry. | |
547 | */ | |
5e656dbb BN |
548 | int |
549 | xfs_dir_replace( | |
a50d2ab0 BF |
550 | struct xfs_trans *tp, |
551 | struct xfs_inode *dp, | |
c331b654 | 552 | const struct xfs_name *name, /* name of entry to replace */ |
a50d2ab0 | 553 | xfs_ino_t inum, /* new inode number */ |
a50d2ab0 | 554 | xfs_extlen_t total) /* bmap's total block count */ |
2bd0ea18 | 555 | { |
a50d2ab0 BF |
556 | struct xfs_da_args *args; |
557 | int rval; | |
2bd0ea18 | 558 | |
e37bf53c | 559 | ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); |
a95cf252 | 560 | |
ff105f75 DC |
561 | rval = xfs_dir_ino_validate(tp->t_mountp, inum); |
562 | if (rval) | |
2bd0ea18 | 563 | return rval; |
5e656dbb | 564 | |
a6cb3f66 | 565 | args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); |
ff105f75 | 566 | if (!args) |
12b53197 | 567 | return -ENOMEM; |
ff105f75 DC |
568 | |
569 | args->geo = dp->i_mount->m_dir_geo; | |
570 | args->name = name->name; | |
571 | args->namelen = name->len; | |
572 | args->filetype = name->type; | |
e169cc9b | 573 | args->hashval = xfs_dir2_hashname(dp->i_mount, name); |
ff105f75 DC |
574 | args->inumber = inum; |
575 | args->dp = dp; | |
ff105f75 DC |
576 | args->total = total; |
577 | args->whichfork = XFS_DATA_FORK; | |
578 | args->trans = tp; | |
0e232547 | 579 | args->owner = dp->i_ino; |
fcdabef8 | 580 | rval = xfs_dir_replace_args(args); |
336b689a | 581 | kfree(args); |
2bd0ea18 NS |
582 | return rval; |
583 | } | |
584 | ||
4a34b33d DC |
585 | /* |
586 | * See if this entry can be added to the directory without allocating space. | |
4a34b33d DC |
587 | */ |
588 | int | |
589 | xfs_dir_canenter( | |
92650440 DW |
590 | struct xfs_trans *tp, |
591 | struct xfs_inode *dp, | |
592 | const struct xfs_name *name) /* name of entry to add */ | |
4a34b33d | 593 | { |
015bc82e | 594 | return xfs_dir_createname(tp, dp, name, 0, 0); |
4a34b33d DC |
595 | } |
596 | ||
2bd0ea18 NS |
597 | /* |
598 | * Utility routines. | |
599 | */ | |
600 | ||
601 | /* | |
602 | * Add a block to the directory. | |
a2ceac1f DC |
603 | * |
604 | * This routine is for data and free blocks, not leaf/node blocks which are | |
605 | * handled by xfs_da_grow_inode. | |
2bd0ea18 | 606 | */ |
5e656dbb | 607 | int |
2bd0ea18 | 608 | xfs_dir2_grow_inode( |
a2ceac1f DC |
609 | struct xfs_da_args *args, |
610 | int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */ | |
611 | xfs_dir2_db_t *dbp) /* out: block number added */ | |
2bd0ea18 | 612 | { |
a2ceac1f DC |
613 | struct xfs_inode *dp = args->dp; |
614 | struct xfs_mount *mp = dp->i_mount; | |
615 | xfs_fileoff_t bno; /* directory offset of new block */ | |
616 | int count; /* count of filesystem blocks */ | |
617 | int error; | |
56b2de80 DC |
618 | |
619 | trace_xfs_dir2_grow_inode(args, space); | |
2bd0ea18 | 620 | |
2bd0ea18 NS |
621 | /* |
622 | * Set lowest possible block in the space requested. | |
623 | */ | |
624 | bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE); | |
ff105f75 | 625 | count = args->geo->fsbcount; |
2bd0ea18 | 626 | |
a2ceac1f DC |
627 | error = xfs_da_grow_inode_int(args, &bno, count); |
628 | if (error) | |
629 | return error; | |
56b2de80 | 630 | |
ff105f75 | 631 | *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno); |
56b2de80 | 632 | |
2bd0ea18 NS |
633 | /* |
634 | * Update file's size if this is the data space and it grew. | |
635 | */ | |
636 | if (space == XFS_DIR2_DATA_SPACE) { | |
637 | xfs_fsize_t size; /* directory file (data) size */ | |
638 | ||
639 | size = XFS_FSB_TO_B(mp, bno + count); | |
509dcb4b CH |
640 | if (size > dp->i_disk_size) { |
641 | dp->i_disk_size = size; | |
a2ceac1f | 642 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); |
2bd0ea18 NS |
643 | } |
644 | } | |
645 | return 0; | |
646 | } | |
647 | ||
2bd0ea18 NS |
648 | /* |
649 | * Remove the given block from the directory. | |
650 | * This routine is used for data and free blocks, leaf/node are done | |
651 | * by xfs_da_shrink_inode. | |
652 | */ | |
653 | int | |
654 | xfs_dir2_shrink_inode( | |
a50d2ab0 BF |
655 | struct xfs_da_args *args, |
656 | xfs_dir2_db_t db, | |
657 | struct xfs_buf *bp) | |
2bd0ea18 | 658 | { |
a50d2ab0 BF |
659 | xfs_fileoff_t bno; /* directory file offset */ |
660 | xfs_dablk_t da; /* directory file offset */ | |
661 | int done; /* bunmap is finished */ | |
662 | struct xfs_inode *dp; | |
663 | int error; | |
664 | struct xfs_mount *mp; | |
665 | struct xfs_trans *tp; | |
2bd0ea18 | 666 | |
56b2de80 DC |
667 | trace_xfs_dir2_shrink_inode(args, db); |
668 | ||
2bd0ea18 NS |
669 | dp = args->dp; |
670 | mp = dp->i_mount; | |
671 | tp = args->trans; | |
ff105f75 | 672 | da = xfs_dir2_db_to_da(args->geo, db); |
db9cba4e DC |
673 | |
674 | /* Unmap the fsblock(s). */ | |
d3c5f3dd | 675 | error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done); |
db9cba4e | 676 | if (error) { |
2bd0ea18 | 677 | /* |
db9cba4e DC |
678 | * ENOSPC actually can happen if we're in a removename with no |
679 | * space reservation, and the resulting block removal would | |
680 | * cause a bmap btree split or conversion from extents to btree. | |
681 | * This can only happen for un-fragmented directory blocks, | |
682 | * since you need to be punching out the middle of an extent. | |
683 | * In this case we need to leave the block in the file, and not | |
684 | * binval it. So the block has to be in a consistent empty | |
685 | * state and appropriately logged. We don't free up the buffer, | |
686 | * the caller can tell it hasn't happened since it got an error | |
687 | * back. | |
2bd0ea18 NS |
688 | */ |
689 | return error; | |
690 | } | |
691 | ASSERT(done); | |
692 | /* | |
693 | * Invalidate the buffer from the transaction. | |
694 | */ | |
a2ceac1f | 695 | xfs_trans_binval(tp, bp); |
2bd0ea18 NS |
696 | /* |
697 | * If it's not a data block, we're done. | |
698 | */ | |
ff105f75 | 699 | if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET)) |
2bd0ea18 NS |
700 | return 0; |
701 | /* | |
702 | * If the block isn't the last one in the directory, we're done. | |
703 | */ | |
509dcb4b | 704 | if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0)) |
2bd0ea18 NS |
705 | return 0; |
706 | bno = da; | |
0e266570 | 707 | if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) { |
2bd0ea18 NS |
708 | /* |
709 | * This can't really happen unless there's kernel corruption. | |
710 | */ | |
711 | return error; | |
712 | } | |
ff105f75 | 713 | if (db == args->geo->datablk) |
2bd0ea18 NS |
714 | ASSERT(bno == 0); |
715 | else | |
716 | ASSERT(bno > 0); | |
717 | /* | |
718 | * Set the size to the new last block. | |
719 | */ | |
509dcb4b | 720 | dp->i_disk_size = XFS_FSB_TO_B(mp, bno); |
2bd0ea18 NS |
721 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
722 | return 0; | |
723 | } | |
d2b2d412 DW |
724 | |
725 | /* Returns true if the directory entry name is valid. */ | |
726 | bool | |
727 | xfs_dir2_namecheck( | |
728 | const void *name, | |
729 | size_t length) | |
730 | { | |
731 | /* | |
732 | * MAXNAMELEN includes the trailing null, but (name/length) leave it | |
733 | * out, so use >= for the length check. | |
734 | */ | |
735 | if (length >= MAXNAMELEN) | |
736 | return false; | |
737 | ||
738 | /* There shouldn't be any slashes or nulls here */ | |
739 | return !memchr(name, '/', length) && !memchr(name, 0, length); | |
740 | } | |
846e459c DW |
741 | |
742 | xfs_dahash_t | |
743 | xfs_dir2_hashname( | |
744 | struct xfs_mount *mp, | |
c331b654 | 745 | const struct xfs_name *name) |
846e459c | 746 | { |
94541a16 | 747 | if (unlikely(xfs_has_asciici(mp))) |
846e459c DW |
748 | return xfs_ascii_ci_hashname(name); |
749 | return xfs_da_hashname(name->name, name->len); | |
750 | } | |
751 | ||
752 | enum xfs_dacmp | |
753 | xfs_dir2_compname( | |
754 | struct xfs_da_args *args, | |
755 | const unsigned char *name, | |
756 | int len) | |
757 | { | |
94541a16 | 758 | if (unlikely(xfs_has_asciici(args->dp->i_mount))) |
846e459c DW |
759 | return xfs_ascii_ci_compname(args, name, len); |
760 | return xfs_da_compname(args, name, len); | |
761 | } | |
7ce57cdc | 762 | |
2ab755da DW |
763 | #ifdef CONFIG_XFS_LIVE_HOOKS |
764 | /* | |
765 | * Use a static key here to reduce the overhead of directory live update hooks. | |
766 | * If the compiler supports jump labels, the static branch will be replaced by | |
767 | * a nop sled when there are no hook users. Online fsck is currently the only | |
768 | * caller, so this is a reasonable tradeoff. | |
769 | * | |
770 | * Note: Patching the kernel code requires taking the cpu hotplug lock. Other | |
771 | * parts of the kernel allocate memory with that lock held, which means that | |
772 | * XFS callers cannot hold any locks that might be used by memory reclaim or | |
773 | * writeback when calling the static_branch_{inc,dec} functions. | |
774 | */ | |
775 | DEFINE_STATIC_XFS_HOOK_SWITCH(xfs_dir_hooks_switch); | |
776 | ||
777 | void | |
778 | xfs_dir_hook_disable(void) | |
779 | { | |
780 | xfs_hooks_switch_off(&xfs_dir_hooks_switch); | |
781 | } | |
782 | ||
783 | void | |
784 | xfs_dir_hook_enable(void) | |
785 | { | |
786 | xfs_hooks_switch_on(&xfs_dir_hooks_switch); | |
787 | } | |
788 | ||
789 | /* Call hooks for a directory update relating to a child dirent update. */ | |
790 | inline void | |
791 | xfs_dir_update_hook( | |
792 | struct xfs_inode *dp, | |
793 | struct xfs_inode *ip, | |
794 | int delta, | |
795 | const struct xfs_name *name) | |
796 | { | |
797 | if (xfs_hooks_switched_on(&xfs_dir_hooks_switch)) { | |
798 | struct xfs_dir_update_params p = { | |
799 | .dp = dp, | |
800 | .ip = ip, | |
801 | .delta = delta, | |
802 | .name = name, | |
803 | }; | |
804 | struct xfs_mount *mp = ip->i_mount; | |
805 | ||
806 | xfs_hooks_call(&mp->m_dir_update_hooks, 0, &p); | |
807 | } | |
808 | } | |
809 | ||
810 | /* Call the specified function during a directory update. */ | |
811 | int | |
812 | xfs_dir_hook_add( | |
813 | struct xfs_mount *mp, | |
814 | struct xfs_dir_hook *hook) | |
815 | { | |
816 | return xfs_hooks_add(&mp->m_dir_update_hooks, &hook->dirent_hook); | |
817 | } | |
818 | ||
819 | /* Stop calling the specified function during a directory update. */ | |
820 | void | |
821 | xfs_dir_hook_del( | |
822 | struct xfs_mount *mp, | |
823 | struct xfs_dir_hook *hook) | |
824 | { | |
825 | xfs_hooks_del(&mp->m_dir_update_hooks, &hook->dirent_hook); | |
826 | } | |
827 | ||
828 | /* Configure directory update hook functions. */ | |
829 | void | |
830 | xfs_dir_hook_setup( | |
831 | struct xfs_dir_hook *hook, | |
832 | notifier_fn_t mod_fn) | |
833 | { | |
834 | xfs_hook_setup(&hook->dirent_hook, mod_fn); | |
835 | } | |
836 | #endif /* CONFIG_XFS_LIVE_HOOKS */ | |
837 | ||
7ce57cdc DW |
838 | /* |
839 | * Given a directory @dp, a newly allocated inode @ip, and a @name, link @ip | |
840 | * into @dp under the given @name. If @ip is a directory, it will be | |
841 | * initialized. Both inodes must have the ILOCK held and the transaction must | |
842 | * have sufficient blocks reserved. | |
843 | */ | |
844 | int | |
845 | xfs_dir_create_child( | |
846 | struct xfs_trans *tp, | |
847 | unsigned int resblks, | |
848 | struct xfs_dir_update *du) | |
849 | { | |
850 | struct xfs_inode *dp = du->dp; | |
851 | const struct xfs_name *name = du->name; | |
852 | struct xfs_inode *ip = du->ip; | |
853 | int error; | |
854 | ||
855 | xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); | |
856 | xfs_assert_ilocked(dp, XFS_ILOCK_EXCL); | |
857 | ||
858 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks); | |
859 | if (error) { | |
860 | ASSERT(error != -ENOSPC); | |
861 | return error; | |
862 | } | |
863 | ||
864 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
865 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
866 | ||
867 | if (S_ISDIR(VFS_I(ip)->i_mode)) { | |
868 | error = xfs_dir_init(tp, ip, dp); | |
869 | if (error) | |
870 | return error; | |
871 | ||
872 | xfs_bumplink(tp, dp); | |
873 | } | |
874 | ||
875 | /* | |
876 | * If we have parent pointers, we need to add the attribute containing | |
877 | * the parent information now. | |
878 | */ | |
879 | if (du->ppargs) { | |
880 | error = xfs_parent_addname(tp, du->ppargs, dp, name, ip); | |
881 | if (error) | |
882 | return error; | |
883 | } | |
884 | ||
2ab755da | 885 | xfs_dir_update_hook(dp, ip, 1, name); |
7ce57cdc DW |
886 | return 0; |
887 | } | |
92650440 DW |
888 | |
889 | /* | |
890 | * Given a directory @dp, an existing non-directory inode @ip, and a @name, | |
891 | * link @ip into @dp under the given @name. Both inodes must have the ILOCK | |
892 | * held. | |
893 | */ | |
894 | int | |
895 | xfs_dir_add_child( | |
896 | struct xfs_trans *tp, | |
897 | unsigned int resblks, | |
898 | struct xfs_dir_update *du) | |
899 | { | |
900 | struct xfs_inode *dp = du->dp; | |
901 | const struct xfs_name *name = du->name; | |
902 | struct xfs_inode *ip = du->ip; | |
903 | struct xfs_mount *mp = tp->t_mountp; | |
904 | int error; | |
905 | ||
906 | xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); | |
907 | xfs_assert_ilocked(dp, XFS_ILOCK_EXCL); | |
908 | ASSERT(!S_ISDIR(VFS_I(ip)->i_mode)); | |
909 | ||
910 | if (!resblks) { | |
911 | error = xfs_dir_canenter(tp, dp, name); | |
912 | if (error) | |
913 | return error; | |
914 | } | |
915 | ||
916 | /* | |
917 | * Handle initial link state of O_TMPFILE inode | |
918 | */ | |
919 | if (VFS_I(ip)->i_nlink == 0) { | |
920 | struct xfs_perag *pag; | |
921 | ||
922 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); | |
923 | error = xfs_iunlink_remove(tp, pag, ip); | |
924 | xfs_perag_put(pag); | |
925 | if (error) | |
926 | return error; | |
927 | } | |
928 | ||
929 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks); | |
930 | if (error) | |
931 | return error; | |
932 | ||
933 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
934 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
935 | ||
936 | xfs_bumplink(tp, ip); | |
937 | ||
938 | /* | |
939 | * If we have parent pointers, we now need to add the parent record to | |
940 | * the attribute fork of the inode. If this is the initial parent | |
941 | * attribute, we need to create it correctly, otherwise we can just add | |
942 | * the parent to the inode. | |
943 | */ | |
944 | if (du->ppargs) { | |
945 | error = xfs_parent_addname(tp, du->ppargs, dp, name, ip); | |
946 | if (error) | |
947 | return error; | |
948 | } | |
949 | ||
2ab755da | 950 | xfs_dir_update_hook(dp, ip, 1, name); |
92650440 DW |
951 | return 0; |
952 | } | |
ffed33c5 DW |
953 | |
954 | /* | |
955 | * Given a directory @dp, a child @ip, and a @name, remove the (@name, @ip) | |
956 | * entry from the directory. Both inodes must have the ILOCK held. | |
957 | */ | |
958 | int | |
959 | xfs_dir_remove_child( | |
960 | struct xfs_trans *tp, | |
961 | unsigned int resblks, | |
962 | struct xfs_dir_update *du) | |
963 | { | |
964 | struct xfs_inode *dp = du->dp; | |
965 | const struct xfs_name *name = du->name; | |
966 | struct xfs_inode *ip = du->ip; | |
967 | int error; | |
968 | ||
969 | xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); | |
970 | xfs_assert_ilocked(dp, XFS_ILOCK_EXCL); | |
971 | ||
972 | /* | |
973 | * If we're removing a directory perform some additional validation. | |
974 | */ | |
975 | if (S_ISDIR(VFS_I(ip)->i_mode)) { | |
976 | ASSERT(VFS_I(ip)->i_nlink >= 2); | |
977 | if (VFS_I(ip)->i_nlink != 2) | |
978 | return -ENOTEMPTY; | |
979 | if (!xfs_dir_isempty(ip)) | |
980 | return -ENOTEMPTY; | |
981 | ||
982 | /* Drop the link from ip's "..". */ | |
983 | error = xfs_droplink(tp, dp); | |
984 | if (error) | |
985 | return error; | |
986 | ||
987 | /* Drop the "." link from ip to self. */ | |
988 | error = xfs_droplink(tp, ip); | |
989 | if (error) | |
990 | return error; | |
991 | ||
992 | /* | |
993 | * Point the unlinked child directory's ".." entry to the root | |
994 | * directory to eliminate back-references to inodes that may | |
995 | * get freed before the child directory is closed. If the fs | |
996 | * gets shrunk, this can lead to dirent inode validation errors. | |
997 | */ | |
998 | if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) { | |
999 | error = xfs_dir_replace(tp, ip, &xfs_name_dotdot, | |
1000 | tp->t_mountp->m_sb.sb_rootino, 0); | |
1001 | if (error) | |
1002 | return error; | |
1003 | } | |
1004 | } else { | |
1005 | /* | |
1006 | * When removing a non-directory we need to log the parent | |
1007 | * inode here. For a directory this is done implicitly | |
1008 | * by the xfs_droplink call for the ".." entry. | |
1009 | */ | |
1010 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
1011 | } | |
1012 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1013 | ||
1014 | /* Drop the link from dp to ip. */ | |
1015 | error = xfs_droplink(tp, ip); | |
1016 | if (error) | |
1017 | return error; | |
1018 | ||
1019 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks); | |
1020 | if (error) { | |
1021 | ASSERT(error != -ENOENT); | |
1022 | return error; | |
1023 | } | |
1024 | ||
1025 | /* Remove parent pointer. */ | |
1026 | if (du->ppargs) { | |
1027 | error = xfs_parent_removename(tp, du->ppargs, dp, name, ip); | |
1028 | if (error) | |
1029 | return error; | |
1030 | } | |
1031 | ||
2ab755da | 1032 | xfs_dir_update_hook(dp, ip, -1, name); |
ffed33c5 DW |
1033 | return 0; |
1034 | } | |
273c0ead DW |
1035 | |
1036 | /* | |
1037 | * Exchange the entry (@name1, @ip1) in directory @dp1 with the entry (@name2, | |
1038 | * @ip2) in directory @dp2, and update '..' @ip1 and @ip2's entries as needed. | |
1039 | * @ip1 and @ip2 need not be of the same type. | |
1040 | * | |
1041 | * All inodes must have the ILOCK held, and both entries must already exist. | |
1042 | */ | |
1043 | int | |
1044 | xfs_dir_exchange_children( | |
1045 | struct xfs_trans *tp, | |
1046 | struct xfs_dir_update *du1, | |
1047 | struct xfs_dir_update *du2, | |
1048 | unsigned int spaceres) | |
1049 | { | |
1050 | struct xfs_inode *dp1 = du1->dp; | |
1051 | const struct xfs_name *name1 = du1->name; | |
1052 | struct xfs_inode *ip1 = du1->ip; | |
1053 | struct xfs_inode *dp2 = du2->dp; | |
1054 | const struct xfs_name *name2 = du2->name; | |
1055 | struct xfs_inode *ip2 = du2->ip; | |
1056 | int ip1_flags = 0; | |
1057 | int ip2_flags = 0; | |
1058 | int dp2_flags = 0; | |
1059 | int error; | |
1060 | ||
1061 | /* Swap inode number for dirent in first parent */ | |
1062 | error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres); | |
1063 | if (error) | |
1064 | return error; | |
1065 | ||
1066 | /* Swap inode number for dirent in second parent */ | |
1067 | error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres); | |
1068 | if (error) | |
1069 | return error; | |
1070 | ||
1071 | /* | |
1072 | * If we're renaming one or more directories across different parents, | |
1073 | * update the respective ".." entries (and link counts) to match the new | |
1074 | * parents. | |
1075 | */ | |
1076 | if (dp1 != dp2) { | |
1077 | dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | |
1078 | ||
1079 | if (S_ISDIR(VFS_I(ip2)->i_mode)) { | |
1080 | error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot, | |
1081 | dp1->i_ino, spaceres); | |
1082 | if (error) | |
1083 | return error; | |
1084 | ||
1085 | /* transfer ip2 ".." reference to dp1 */ | |
1086 | if (!S_ISDIR(VFS_I(ip1)->i_mode)) { | |
1087 | error = xfs_droplink(tp, dp2); | |
1088 | if (error) | |
1089 | return error; | |
1090 | xfs_bumplink(tp, dp1); | |
1091 | } | |
1092 | ||
1093 | /* | |
1094 | * Although ip1 isn't changed here, userspace needs | |
1095 | * to be warned about the change, so that applications | |
1096 | * relying on it (like backup ones), will properly | |
1097 | * notify the change | |
1098 | */ | |
1099 | ip1_flags |= XFS_ICHGTIME_CHG; | |
1100 | ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | |
1101 | } | |
1102 | ||
1103 | if (S_ISDIR(VFS_I(ip1)->i_mode)) { | |
1104 | error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot, | |
1105 | dp2->i_ino, spaceres); | |
1106 | if (error) | |
1107 | return error; | |
1108 | ||
1109 | /* transfer ip1 ".." reference to dp2 */ | |
1110 | if (!S_ISDIR(VFS_I(ip2)->i_mode)) { | |
1111 | error = xfs_droplink(tp, dp1); | |
1112 | if (error) | |
1113 | return error; | |
1114 | xfs_bumplink(tp, dp2); | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * Although ip2 isn't changed here, userspace needs | |
1119 | * to be warned about the change, so that applications | |
1120 | * relying on it (like backup ones), will properly | |
1121 | * notify the change | |
1122 | */ | |
1123 | ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | |
1124 | ip2_flags |= XFS_ICHGTIME_CHG; | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | if (ip1_flags) { | |
1129 | xfs_trans_ichgtime(tp, ip1, ip1_flags); | |
1130 | xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE); | |
1131 | } | |
1132 | if (ip2_flags) { | |
1133 | xfs_trans_ichgtime(tp, ip2, ip2_flags); | |
1134 | xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE); | |
1135 | } | |
1136 | if (dp2_flags) { | |
1137 | xfs_trans_ichgtime(tp, dp2, dp2_flags); | |
1138 | xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE); | |
1139 | } | |
1140 | xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1141 | xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE); | |
1142 | ||
1143 | /* Schedule parent pointer replacements */ | |
1144 | if (du1->ppargs) { | |
1145 | error = xfs_parent_replacename(tp, du1->ppargs, dp1, name1, | |
1146 | dp2, name2, ip1); | |
1147 | if (error) | |
1148 | return error; | |
1149 | } | |
1150 | ||
1151 | if (du2->ppargs) { | |
1152 | error = xfs_parent_replacename(tp, du2->ppargs, dp2, name2, | |
1153 | dp1, name1, ip2); | |
1154 | if (error) | |
1155 | return error; | |
1156 | } | |
1157 | ||
2ab755da DW |
1158 | /* |
1159 | * Inform our hook clients that we've finished an exchange operation as | |
1160 | * follows: removed the source and target files from their directories; | |
1161 | * added the target to the source directory; and added the source to | |
1162 | * the target directory. All inodes are locked, so it's ok to model a | |
1163 | * rename this way so long as we say we deleted entries before we add | |
1164 | * new ones. | |
1165 | */ | |
1166 | xfs_dir_update_hook(dp1, ip1, -1, name1); | |
1167 | xfs_dir_update_hook(dp2, ip2, -1, name2); | |
1168 | xfs_dir_update_hook(dp1, ip2, 1, name1); | |
1169 | xfs_dir_update_hook(dp2, ip1, 1, name2); | |
273c0ead DW |
1170 | return 0; |
1171 | } | |
70696163 DW |
1172 | |
1173 | /* | |
1174 | * Given an entry (@src_name, @src_ip) in directory @src_dp, make the entry | |
1175 | * @target_name in directory @target_dp point to @src_ip and remove the | |
1176 | * original entry, cleaning up everything left behind. | |
1177 | * | |
1178 | * Cleanup involves dropping a link count on @target_ip, and either removing | |
1179 | * the (@src_name, @src_ip) entry from @src_dp or simply replacing the entry | |
1180 | * with (@src_name, @wip) if a whiteout inode @wip is supplied. | |
1181 | * | |
1182 | * All inodes must have the ILOCK held. We assume that if @src_ip is a | |
1183 | * directory then its '..' doesn't already point to @target_dp, and that @wip | |
1184 | * is a freshly allocated whiteout. | |
1185 | */ | |
1186 | int | |
1187 | xfs_dir_rename_children( | |
1188 | struct xfs_trans *tp, | |
1189 | struct xfs_dir_update *du_src, | |
1190 | struct xfs_dir_update *du_tgt, | |
1191 | unsigned int spaceres, | |
1192 | struct xfs_dir_update *du_wip) | |
1193 | { | |
1194 | struct xfs_mount *mp = tp->t_mountp; | |
1195 | struct xfs_inode *src_dp = du_src->dp; | |
1196 | const struct xfs_name *src_name = du_src->name; | |
1197 | struct xfs_inode *src_ip = du_src->ip; | |
1198 | struct xfs_inode *target_dp = du_tgt->dp; | |
1199 | const struct xfs_name *target_name = du_tgt->name; | |
1200 | struct xfs_inode *target_ip = du_tgt->ip; | |
1201 | bool new_parent = (src_dp != target_dp); | |
1202 | bool src_is_directory; | |
1203 | int error; | |
1204 | ||
1205 | src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode); | |
1206 | ||
1207 | /* | |
1208 | * Check for expected errors before we dirty the transaction | |
1209 | * so we can return an error without a transaction abort. | |
1210 | */ | |
1211 | if (target_ip == NULL) { | |
1212 | /* | |
1213 | * If there's no space reservation, check the entry will | |
1214 | * fit before actually inserting it. | |
1215 | */ | |
1216 | if (!spaceres) { | |
1217 | error = xfs_dir_canenter(tp, target_dp, target_name); | |
1218 | if (error) | |
1219 | return error; | |
1220 | } | |
1221 | } else { | |
1222 | /* | |
1223 | * If target exists and it's a directory, check that whether | |
1224 | * it can be destroyed. | |
1225 | */ | |
1226 | if (S_ISDIR(VFS_I(target_ip)->i_mode) && | |
1227 | (!xfs_dir_isempty(target_ip) || | |
1228 | (VFS_I(target_ip)->i_nlink > 2))) | |
1229 | return -EEXIST; | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * Directory entry creation below may acquire the AGF. Remove | |
1234 | * the whiteout from the unlinked list first to preserve correct | |
1235 | * AGI/AGF locking order. This dirties the transaction so failures | |
1236 | * after this point will abort and log recovery will clean up the | |
1237 | * mess. | |
1238 | * | |
1239 | * For whiteouts, we need to bump the link count on the whiteout | |
1240 | * inode. After this point, we have a real link, clear the tmpfile | |
1241 | * state flag from the inode so it doesn't accidentally get misused | |
1242 | * in future. | |
1243 | */ | |
1244 | if (du_wip->ip) { | |
1245 | struct xfs_perag *pag; | |
1246 | ||
1247 | ASSERT(VFS_I(du_wip->ip)->i_nlink == 0); | |
1248 | ||
1249 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, du_wip->ip->i_ino)); | |
1250 | error = xfs_iunlink_remove(tp, pag, du_wip->ip); | |
1251 | xfs_perag_put(pag); | |
1252 | if (error) | |
1253 | return error; | |
1254 | ||
1255 | xfs_bumplink(tp, du_wip->ip); | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * Set up the target. | |
1260 | */ | |
1261 | if (target_ip == NULL) { | |
1262 | /* | |
1263 | * If target does not exist and the rename crosses | |
1264 | * directories, adjust the target directory link count | |
1265 | * to account for the ".." reference from the new entry. | |
1266 | */ | |
1267 | error = xfs_dir_createname(tp, target_dp, target_name, | |
1268 | src_ip->i_ino, spaceres); | |
1269 | if (error) | |
1270 | return error; | |
1271 | ||
1272 | xfs_trans_ichgtime(tp, target_dp, | |
1273 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1274 | ||
1275 | if (new_parent && src_is_directory) { | |
1276 | xfs_bumplink(tp, target_dp); | |
1277 | } | |
1278 | } else { /* target_ip != NULL */ | |
1279 | /* | |
1280 | * Link the source inode under the target name. | |
1281 | * If the source inode is a directory and we are moving | |
1282 | * it across directories, its ".." entry will be | |
1283 | * inconsistent until we replace that down below. | |
1284 | * | |
1285 | * In case there is already an entry with the same | |
1286 | * name at the destination directory, remove it first. | |
1287 | */ | |
1288 | error = xfs_dir_replace(tp, target_dp, target_name, | |
1289 | src_ip->i_ino, spaceres); | |
1290 | if (error) | |
1291 | return error; | |
1292 | ||
1293 | xfs_trans_ichgtime(tp, target_dp, | |
1294 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1295 | ||
1296 | /* | |
1297 | * Decrement the link count on the target since the target | |
1298 | * dir no longer points to it. | |
1299 | */ | |
1300 | error = xfs_droplink(tp, target_ip); | |
1301 | if (error) | |
1302 | return error; | |
1303 | ||
1304 | if (src_is_directory) { | |
1305 | /* | |
1306 | * Drop the link from the old "." entry. | |
1307 | */ | |
1308 | error = xfs_droplink(tp, target_ip); | |
1309 | if (error) | |
1310 | return error; | |
1311 | } | |
1312 | } /* target_ip != NULL */ | |
1313 | ||
1314 | /* | |
1315 | * Remove the source. | |
1316 | */ | |
1317 | if (new_parent && src_is_directory) { | |
1318 | /* | |
1319 | * Rewrite the ".." entry to point to the new | |
1320 | * directory. | |
1321 | */ | |
1322 | error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, | |
1323 | target_dp->i_ino, spaceres); | |
1324 | ASSERT(error != -EEXIST); | |
1325 | if (error) | |
1326 | return error; | |
1327 | } | |
1328 | ||
1329 | /* | |
1330 | * We always want to hit the ctime on the source inode. | |
1331 | * | |
1332 | * This isn't strictly required by the standards since the source | |
1333 | * inode isn't really being changed, but old unix file systems did | |
1334 | * it and some incremental backup programs won't work without it. | |
1335 | */ | |
1336 | xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); | |
1337 | xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); | |
1338 | ||
1339 | /* | |
1340 | * Adjust the link count on src_dp. This is necessary when | |
1341 | * renaming a directory, either within one parent when | |
1342 | * the target existed, or across two parent directories. | |
1343 | */ | |
1344 | if (src_is_directory && (new_parent || target_ip != NULL)) { | |
1345 | ||
1346 | /* | |
1347 | * Decrement link count on src_directory since the | |
1348 | * entry that's moved no longer points to it. | |
1349 | */ | |
1350 | error = xfs_droplink(tp, src_dp); | |
1351 | if (error) | |
1352 | return error; | |
1353 | } | |
1354 | ||
1355 | /* | |
1356 | * For whiteouts, we only need to update the source dirent with the | |
1357 | * inode number of the whiteout inode rather than removing it | |
1358 | * altogether. | |
1359 | */ | |
1360 | if (du_wip->ip) | |
1361 | error = xfs_dir_replace(tp, src_dp, src_name, du_wip->ip->i_ino, | |
1362 | spaceres); | |
1363 | else | |
1364 | error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, | |
1365 | spaceres); | |
1366 | if (error) | |
1367 | return error; | |
1368 | ||
1369 | xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1370 | xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); | |
1371 | if (new_parent) | |
1372 | xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); | |
1373 | ||
1374 | /* Schedule parent pointer updates. */ | |
1375 | if (du_wip->ppargs) { | |
1376 | error = xfs_parent_addname(tp, du_wip->ppargs, src_dp, | |
1377 | src_name, du_wip->ip); | |
1378 | if (error) | |
1379 | return error; | |
1380 | } | |
1381 | ||
1382 | if (du_src->ppargs) { | |
1383 | error = xfs_parent_replacename(tp, du_src->ppargs, src_dp, | |
1384 | src_name, target_dp, target_name, src_ip); | |
1385 | if (error) | |
1386 | return error; | |
1387 | } | |
1388 | ||
1389 | if (du_tgt->ppargs) { | |
1390 | error = xfs_parent_removename(tp, du_tgt->ppargs, target_dp, | |
1391 | target_name, target_ip); | |
1392 | if (error) | |
1393 | return error; | |
1394 | } | |
1395 | ||
2ab755da DW |
1396 | /* |
1397 | * Inform our hook clients that we've finished a rename operation as | |
1398 | * follows: removed the source and target files from their directories; | |
1399 | * that we've added the source to the target directory; and finally | |
1400 | * that we've added the whiteout, if there was one. All inodes are | |
1401 | * locked, so it's ok to model a rename this way so long as we say we | |
1402 | * deleted entries before we add new ones. | |
1403 | */ | |
1404 | if (target_ip) | |
1405 | xfs_dir_update_hook(target_dp, target_ip, -1, target_name); | |
1406 | xfs_dir_update_hook(src_dp, src_ip, -1, src_name); | |
1407 | xfs_dir_update_hook(target_dp, src_ip, 1, target_name); | |
1408 | if (du_wip->ip) | |
1409 | xfs_dir_update_hook(src_dp, du_wip->ip, 1, src_name); | |
70696163 DW |
1410 | return 0; |
1411 | } |