1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* dir.c: AFS filesystem directory handling
3 *
4 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/fs.h>
10 #include <linux/namei.h>
11 #include <linux/pagemap.h>
12 #include <linux/swap.h>
13 #include <linux/ctype.h>
14 #include <linux/sched.h>
15 #include <linux/iversion.h>
16 #include <linux/iov_iter.h>
17 #include <linux/task_io_accounting_ops.h>
18 #include "internal.h"
19 #include "afs_fs.h"
20 #include "xdr_fs.h"
21
22 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
23 unsigned int flags);
24 static int afs_dir_open(struct inode *inode, struct file *file);
25 static int afs_readdir(struct file *file, struct dir_context *ctx);
26 static int afs_d_revalidate(struct inode *dir, const struct qstr *name,
27 struct dentry *dentry, unsigned int flags);
28 static int afs_d_delete(const struct dentry *dentry);
29 static void afs_d_iput(struct dentry *dentry, struct inode *inode);
30 static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
31 loff_t fpos, u64 ino, unsigned dtype);
32 static bool afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
33 loff_t fpos, u64 ino, unsigned dtype);
34 static int afs_create(struct mnt_idmap *idmap, struct inode *dir,
35 struct dentry *dentry, umode_t mode, bool excl);
36 static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
37 struct dentry *dentry, umode_t mode);
38 static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39 static int afs_unlink(struct inode *dir, struct dentry *dentry);
40 static int afs_link(struct dentry *from, struct inode *dir,
41 struct dentry *dentry);
42 static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir,
43 struct dentry *dentry, const char *content);
44 static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
45 struct dentry *old_dentry, struct inode *new_dir,
46 struct dentry *new_dentry, unsigned int flags);
47 static int afs_dir_writepages(struct address_space *mapping,
48 struct writeback_control *wbc);
49
50 const struct file_operations afs_dir_file_operations = {
51 .open = afs_dir_open,
52 .release = afs_release,
53 .iterate_shared = afs_readdir,
54 .lock = afs_lock,
55 .llseek = generic_file_llseek,
56 };
57
58 const struct inode_operations afs_dir_inode_operations = {
59 .create = afs_create,
60 .lookup = afs_lookup,
61 .link = afs_link,
62 .unlink = afs_unlink,
63 .symlink = afs_symlink,
64 .mkdir = afs_mkdir,
65 .rmdir = afs_rmdir,
66 .rename = afs_rename,
67 .permission = afs_permission,
68 .getattr = afs_getattr,
69 .setattr = afs_setattr,
70 };
71
72 const struct address_space_operations afs_dir_aops = {
73 .writepages = afs_dir_writepages,
74 };
75
76 const struct dentry_operations afs_fs_dentry_operations = {
77 .d_revalidate = afs_d_revalidate,
78 .d_delete = afs_d_delete,
79 .d_release = afs_d_release,
80 .d_automount = afs_d_automount,
81 .d_iput = afs_d_iput,
82 };
83
84 struct afs_lookup_one_cookie {
85 struct dir_context ctx;
86 struct qstr name;
87 bool found;
88 struct afs_fid fid;
89 };
90
91 struct afs_lookup_cookie {
92 struct dir_context ctx;
93 struct qstr name;
94 unsigned short nr_fids;
95 struct afs_fid fids[50];
96 };
97
afs_dir_unuse_cookie(struct afs_vnode * dvnode,int ret)98 static void afs_dir_unuse_cookie(struct afs_vnode *dvnode, int ret)
99 {
100 if (ret == 0) {
101 struct afs_vnode_cache_aux aux;
102 loff_t i_size = i_size_read(&dvnode->netfs.inode);
103
104 afs_set_cache_aux(dvnode, &aux);
105 fscache_unuse_cookie(afs_vnode_cache(dvnode), &aux, &i_size);
106 } else {
107 fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL);
108 }
109 }
110
111 /*
112 * Iterate through a kmapped directory segment, dumping a summary of
113 * the contents.
114 */
afs_dir_dump_step(void * iter_base,size_t progress,size_t len,void * priv,void * priv2)115 static size_t afs_dir_dump_step(void *iter_base, size_t progress, size_t len,
116 void *priv, void *priv2)
117 {
118 do {
119 union afs_xdr_dir_block *block = iter_base;
120
121 pr_warn("[%05zx] %32phN\n", progress, block);
122 iter_base += AFS_DIR_BLOCK_SIZE;
123 progress += AFS_DIR_BLOCK_SIZE;
124 len -= AFS_DIR_BLOCK_SIZE;
125 } while (len > 0);
126
127 return len;
128 }
129
130 /*
131 * Dump the contents of a directory.
132 */
afs_dir_dump(struct afs_vnode * dvnode)133 static void afs_dir_dump(struct afs_vnode *dvnode)
134 {
135 struct iov_iter iter;
136 unsigned long long i_size = i_size_read(&dvnode->netfs.inode);
137
138 pr_warn("DIR %llx:%llx is=%llx\n",
139 dvnode->fid.vid, dvnode->fid.vnode, i_size);
140
141 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);
142 iterate_folioq(&iter, iov_iter_count(&iter), NULL, NULL,
143 afs_dir_dump_step);
144 }
145
146 /*
147 * check that a directory folio is valid
148 */
afs_dir_check_block(struct afs_vnode * dvnode,size_t progress,union afs_xdr_dir_block * block)149 static bool afs_dir_check_block(struct afs_vnode *dvnode, size_t progress,
150 union afs_xdr_dir_block *block)
151 {
152 if (block->hdr.magic != AFS_DIR_MAGIC) {
153 pr_warn("%s(%llx): [%zx] bad magic %04x\n",
154 __func__, dvnode->netfs.inode.i_ino,
155 progress, ntohs(block->hdr.magic));
156 trace_afs_dir_check_failed(dvnode, progress);
157 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
158 return false;
159 }
160
161 /* Make sure each block is NUL terminated so we can reasonably
162 * use string functions on it. The filenames in the folio
163 * *should* be NUL-terminated anyway.
164 */
165 ((u8 *)block)[AFS_DIR_BLOCK_SIZE - 1] = 0;
166 afs_stat_v(dvnode, n_read_dir);
167 return true;
168 }
169
170 /*
171 * Iterate through a kmapped directory segment, checking the content.
172 */
afs_dir_check_step(void * iter_base,size_t progress,size_t len,void * priv,void * priv2)173 static size_t afs_dir_check_step(void *iter_base, size_t progress, size_t len,
174 void *priv, void *priv2)
175 {
176 struct afs_vnode *dvnode = priv;
177
178 if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE ||
179 len % AFS_DIR_BLOCK_SIZE))
180 return len;
181
182 do {
183 if (!afs_dir_check_block(dvnode, progress, iter_base))
184 break;
185 iter_base += AFS_DIR_BLOCK_SIZE;
186 len -= AFS_DIR_BLOCK_SIZE;
187 } while (len > 0);
188
189 return len;
190 }
191
192 /*
193 * Check all the blocks in a directory.
194 */
afs_dir_check(struct afs_vnode * dvnode)195 static int afs_dir_check(struct afs_vnode *dvnode)
196 {
197 struct iov_iter iter;
198 unsigned long long i_size = i_size_read(&dvnode->netfs.inode);
199 size_t checked = 0;
200
201 if (unlikely(!i_size))
202 return 0;
203
204 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);
205 checked = iterate_folioq(&iter, iov_iter_count(&iter), dvnode, NULL,
206 afs_dir_check_step);
207 if (checked != i_size) {
208 afs_dir_dump(dvnode);
209 return -EIO;
210 }
211 return 0;
212 }
213
214 /*
215 * open an AFS directory file
216 */
afs_dir_open(struct inode * inode,struct file * file)217 static int afs_dir_open(struct inode *inode, struct file *file)
218 {
219 _enter("{%llu}", inode->i_ino);
220
221 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
222 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
223
224 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
225 return -ENOENT;
226
227 return afs_open(inode, file);
228 }
229
230 /*
231 * Read a file in a single download.
232 */
afs_do_read_single(struct afs_vnode * dvnode,struct file * file)233 static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file)
234 {
235 struct iov_iter iter;
236 ssize_t ret;
237 loff_t i_size;
238
239 i_size = i_size_read(&dvnode->netfs.inode);
240 if (i_size < AFS_DIR_BLOCK_SIZE)
241 return afs_bad(dvnode, afs_file_error_dir_small);
242 if (i_size > AFS_DIR_BLOCK_SIZE * 1024) {
243 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
244 return -EFBIG;
245 }
246
247 /* Expand the storage. TODO: Shrink the storage too. */
248 if (dvnode->directory_size < i_size) {
249 size_t cur_size = dvnode->directory_size;
250
251 ret = netfs_alloc_folioq_buffer(NULL,
252 &dvnode->directory, &cur_size, i_size,
253 mapping_gfp_mask(dvnode->netfs.inode.i_mapping));
254 dvnode->directory_size = cur_size;
255 if (ret < 0)
256 return ret;
257 }
258
259 iov_iter_folio_queue(&iter, ITER_DEST, dvnode->directory, 0, 0, dvnode->directory_size);
260
261 /* AFS requires us to perform the read of a directory synchronously as
262 * a single unit to avoid issues with the directory contents being
263 * changed between reads.
264 */
265 ret = netfs_read_single(&dvnode->netfs.inode, file, &iter);
266 if (ret >= 0) {
267 i_size = i_size_read(&dvnode->netfs.inode);
268 if (i_size > ret) {
269 /* The content has grown, so we need to expand the
270 * buffer.
271 */
272 ret = -ESTALE;
273 } else {
274 int ret2 = afs_dir_check(dvnode);
275
276 if (ret2 < 0)
277 ret = ret2;
278 }
279 }
280
281 return ret;
282 }
283
afs_read_single(struct afs_vnode * dvnode,struct file * file)284 static ssize_t afs_read_single(struct afs_vnode *dvnode, struct file *file)
285 {
286 ssize_t ret;
287
288 fscache_use_cookie(afs_vnode_cache(dvnode), false);
289 ret = afs_do_read_single(dvnode, file);
290 fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL);
291 return ret;
292 }
293
294 /*
295 * Read the directory into a folio_queue buffer in one go, scrubbing the
296 * previous contents. We return -ESTALE if the caller needs to call us again.
297 */
afs_read_dir(struct afs_vnode * dvnode,struct file * file)298 ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file)
299 __acquires(&dvnode->validate_lock)
300 {
301 ssize_t ret;
302 loff_t i_size;
303
304 i_size = i_size_read(&dvnode->netfs.inode);
305
306 ret = -ERESTARTSYS;
307 if (down_read_killable(&dvnode->validate_lock) < 0)
308 goto error;
309
310 /* We only need to reread the data if it became invalid - or if we
311 * haven't read it yet.
312 */
313 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
314 test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) {
315 ret = i_size;
316 goto valid;
317 }
318
319 up_read(&dvnode->validate_lock);
320 if (down_write_killable(&dvnode->validate_lock) < 0)
321 goto error;
322
323 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
324 afs_invalidate_cache(dvnode, 0);
325
326 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) ||
327 !test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) {
328 trace_afs_reload_dir(dvnode);
329 ret = afs_read_single(dvnode, file);
330 if (ret < 0)
331 goto error_unlock;
332
333 // TODO: Trim excess pages
334
335 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
336 set_bit(AFS_VNODE_DIR_READ, &dvnode->flags);
337 } else {
338 ret = i_size;
339 }
340
341 downgrade_write(&dvnode->validate_lock);
342 valid:
343 return ret;
344
345 error_unlock:
346 up_write(&dvnode->validate_lock);
347 error:
348 _leave(" = %zd", ret);
349 return ret;
350 }
351
352 /*
353 * deal with one block in an AFS directory
354 */
afs_dir_iterate_block(struct afs_vnode * dvnode,struct dir_context * ctx,union afs_xdr_dir_block * block)355 static int afs_dir_iterate_block(struct afs_vnode *dvnode,
356 struct dir_context *ctx,
357 union afs_xdr_dir_block *block)
358 {
359 union afs_xdr_dirent *dire;
360 unsigned int blknum, base, hdr, pos, next, nr_slots;
361 size_t nlen;
362 int tmp;
363
364 blknum = ctx->pos / AFS_DIR_BLOCK_SIZE;
365 base = blknum * AFS_DIR_SLOTS_PER_BLOCK;
366 hdr = (blknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
367 pos = DIV_ROUND_UP(ctx->pos, AFS_DIR_DIRENT_SIZE) - base;
368
369 _enter("%llx,%x", ctx->pos, blknum);
370
371 /* walk through the block, an entry at a time */
372 for (unsigned int slot = hdr; slot < AFS_DIR_SLOTS_PER_BLOCK; slot = next) {
373 /* skip entries marked unused in the bitmap */
374 if (!(block->hdr.bitmap[slot / 8] &
375 (1 << (slot % 8)))) {
376 _debug("ENT[%x]: Unused", base + slot);
377 next = slot + 1;
378 if (next >= pos)
379 ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);
380 continue;
381 }
382
383 /* got a valid entry */
384 dire = &block->dirents[slot];
385 nlen = strnlen(dire->u.name,
386 (unsigned long)(block + 1) - (unsigned long)dire->u.name - 1);
387 if (nlen > AFSNAMEMAX - 1) {
388 _debug("ENT[%x]: Name too long (len %zx)",
389 base + slot, nlen);
390 return afs_bad(dvnode, afs_file_error_dir_name_too_long);
391 }
392
393 _debug("ENT[%x]: %s %zx \"%s\"",
394 base + slot, (slot < pos ? "skip" : "fill"),
395 nlen, dire->u.name);
396
397 nr_slots = afs_dir_calc_slots(nlen);
398 next = slot + nr_slots;
399 if (next > AFS_DIR_SLOTS_PER_BLOCK) {
400 _debug("ENT[%x]: extends beyond end dir block (len %zx)",
401 base + slot, nlen);
402 return afs_bad(dvnode, afs_file_error_dir_over_end);
403 }
404
405 /* Check that the name-extension dirents are all allocated */
406 for (tmp = 1; tmp < nr_slots; tmp++) {
407 unsigned int xslot = slot + tmp;
408
409 if (!(block->hdr.bitmap[xslot / 8] & (1 << (xslot % 8)))) {
410 _debug("ENT[%x]: Unmarked extension (%x/%x)",
411 base + slot, tmp, nr_slots);
412 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
413 }
414 }
415
416 /* skip if starts before the current position */
417 if (slot < pos) {
418 if (next > pos)
419 ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);
420 continue;
421 }
422
423 /* found the next entry */
424 if (!dir_emit(ctx, dire->u.name, nlen,
425 ntohl(dire->u.vnode),
426 (ctx->actor == afs_lookup_filldir ||
427 ctx->actor == afs_lookup_one_filldir)?
428 ntohl(dire->u.unique) : DT_UNKNOWN)) {
429 _leave(" = 0 [full]");
430 return 0;
431 }
432
433 ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);
434 }
435
436 _leave(" = 1 [more]");
437 return 1;
438 }
439
440 struct afs_dir_iteration_ctx {
441 struct dir_context *dir_ctx;
442 int error;
443 };
444
445 /*
446 * Iterate through a kmapped directory segment.
447 */
afs_dir_iterate_step(void * iter_base,size_t progress,size_t len,void * priv,void * priv2)448 static size_t afs_dir_iterate_step(void *iter_base, size_t progress, size_t len,
449 void *priv, void *priv2)
450 {
451 struct afs_dir_iteration_ctx *ctx = priv2;
452 struct afs_vnode *dvnode = priv;
453 int ret;
454
455 if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE ||
456 len % AFS_DIR_BLOCK_SIZE)) {
457 pr_err("Mis-iteration prog=%zx len=%zx\n",
458 progress % AFS_DIR_BLOCK_SIZE,
459 len % AFS_DIR_BLOCK_SIZE);
460 return len;
461 }
462
463 do {
464 ret = afs_dir_iterate_block(dvnode, ctx->dir_ctx, iter_base);
465 if (ret != 1)
466 break;
467
468 ctx->dir_ctx->pos = round_up(ctx->dir_ctx->pos, AFS_DIR_BLOCK_SIZE);
469 iter_base += AFS_DIR_BLOCK_SIZE;
470 len -= AFS_DIR_BLOCK_SIZE;
471 } while (len > 0);
472
473 return len;
474 }
475
476 /*
477 * Iterate through the directory folios.
478 */
afs_dir_iterate_contents(struct inode * dir,struct dir_context * dir_ctx)479 static int afs_dir_iterate_contents(struct inode *dir, struct dir_context *dir_ctx)
480 {
481 struct afs_dir_iteration_ctx ctx = { .dir_ctx = dir_ctx };
482 struct afs_vnode *dvnode = AFS_FS_I(dir);
483 struct iov_iter iter;
484 unsigned long long i_size = i_size_read(dir);
485
486 /* Round the file position up to the next entry boundary */
487 dir_ctx->pos = round_up(dir_ctx->pos, sizeof(union afs_xdr_dirent));
488
489 if (i_size <= 0 || dir_ctx->pos >= i_size)
490 return 0;
491
492 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);
493 iov_iter_advance(&iter, round_down(dir_ctx->pos, AFS_DIR_BLOCK_SIZE));
494
495 iterate_folioq(&iter, iov_iter_count(&iter), dvnode, &ctx,
496 afs_dir_iterate_step);
497
498 if (ctx.error == -ESTALE)
499 afs_invalidate_dir(dvnode, afs_dir_invalid_iter_stale);
500 return ctx.error;
501 }
502
503 /*
504 * iterate through the data blob that lists the contents of an AFS directory
505 */
afs_dir_iterate(struct inode * dir,struct dir_context * ctx,struct file * file,afs_dataversion_t * _dir_version)506 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
507 struct file *file, afs_dataversion_t *_dir_version)
508 {
509 struct afs_vnode *dvnode = AFS_FS_I(dir);
510 int retry_limit = 100;
511 int ret;
512
513 _enter("{%llu},%llx,,", dir->i_ino, ctx->pos);
514
515 do {
516 if (--retry_limit < 0) {
517 pr_warn("afs_read_dir(): Too many retries\n");
518 ret = -ESTALE;
519 break;
520 }
521 ret = afs_read_dir(dvnode, file);
522 if (ret < 0) {
523 if (ret != -ESTALE)
524 break;
525 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
526 ret = -ESTALE;
527 break;
528 }
529 continue;
530 }
531 *_dir_version = inode_peek_iversion_raw(dir);
532
533 ret = afs_dir_iterate_contents(dir, ctx);
534 up_read(&dvnode->validate_lock);
535 } while (ret == -ESTALE);
536
537 _leave(" = %d", ret);
538 return ret;
539 }
540
541 /*
542 * read an AFS directory
543 */
afs_readdir(struct file * file,struct dir_context * ctx)544 static int afs_readdir(struct file *file, struct dir_context *ctx)
545 {
546 afs_dataversion_t dir_version;
547
548 return afs_dir_iterate(file_inode(file), ctx, file, &dir_version);
549 }
550
551 /*
552 * Search the directory for a single name
553 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
554 * uniquifier through dtype
555 */
afs_lookup_one_filldir(struct dir_context * ctx,const char * name,int nlen,loff_t fpos,u64 ino,unsigned dtype)556 static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
557 int nlen, loff_t fpos, u64 ino, unsigned dtype)
558 {
559 struct afs_lookup_one_cookie *cookie =
560 container_of(ctx, struct afs_lookup_one_cookie, ctx);
561
562 _enter("{%s,%u},%s,%u,,%llu,%u",
563 cookie->name.name, cookie->name.len, name, nlen,
564 (unsigned long long) ino, dtype);
565
566 /* insanity checks first */
567 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
568 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
569
570 if (cookie->name.len != nlen ||
571 memcmp(cookie->name.name, name, nlen) != 0) {
572 _leave(" = true [keep looking]");
573 return true;
574 }
575
576 cookie->fid.vnode = ino;
577 cookie->fid.unique = dtype;
578 cookie->found = 1;
579
580 _leave(" = false [found]");
581 return false;
582 }
583
584 /*
585 * Do a lookup of a single name in a directory
586 * - just returns the FID the dentry name maps to if found
587 */
afs_do_lookup_one(struct inode * dir,const struct qstr * name,struct afs_fid * fid,afs_dataversion_t * _dir_version)588 static int afs_do_lookup_one(struct inode *dir, const struct qstr *name,
589 struct afs_fid *fid,
590 afs_dataversion_t *_dir_version)
591 {
592 struct afs_super_info *as = dir->i_sb->s_fs_info;
593 struct afs_lookup_one_cookie cookie = {
594 .ctx.actor = afs_lookup_one_filldir,
595 .name = *name,
596 .fid.vid = as->volume->vid
597 };
598 int ret;
599
600 _enter("{%llu},{%.*s},", dir->i_ino, name->len, name->name);
601
602 /* search the directory */
603 ret = afs_dir_iterate(dir, &cookie.ctx, NULL, _dir_version);
604 if (ret < 0) {
605 _leave(" = %d [iter]", ret);
606 return ret;
607 }
608
609 if (!cookie.found) {
610 _leave(" = -ENOENT [not found]");
611 return -ENOENT;
612 }
613
614 *fid = cookie.fid;
615 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
616 return 0;
617 }
618
619 /*
620 * search the directory for a name
621 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
622 * uniquifier through dtype
623 */
afs_lookup_filldir(struct dir_context * ctx,const char * name,int nlen,loff_t fpos,u64 ino,unsigned dtype)624 static bool afs_lookup_filldir(struct dir_context *ctx, const char *name,
625 int nlen, loff_t fpos, u64 ino, unsigned dtype)
626 {
627 struct afs_lookup_cookie *cookie =
628 container_of(ctx, struct afs_lookup_cookie, ctx);
629
630 _enter("{%s,%u},%s,%u,,%llu,%u",
631 cookie->name.name, cookie->name.len, name, nlen,
632 (unsigned long long) ino, dtype);
633
634 /* insanity checks first */
635 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
636 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
637
638 if (cookie->nr_fids < 50) {
639 cookie->fids[cookie->nr_fids].vnode = ino;
640 cookie->fids[cookie->nr_fids].unique = dtype;
641 cookie->nr_fids++;
642 }
643
644 return cookie->nr_fids < 50;
645 }
646
647 /*
648 * Deal with the result of a successful lookup operation. Turn all the files
649 * into inodes and save the first one - which is the one we actually want.
650 */
afs_do_lookup_success(struct afs_operation * op)651 static void afs_do_lookup_success(struct afs_operation *op)
652 {
653 struct afs_vnode_param *vp;
654 struct afs_vnode *vnode;
655 struct inode *inode;
656 u32 abort_code;
657 int i;
658
659 _enter("");
660
661 for (i = 0; i < op->nr_files; i++) {
662 switch (i) {
663 case 0:
664 vp = &op->file[0];
665 abort_code = vp->scb.status.abort_code;
666 if (abort_code != 0) {
667 op->call_abort_code = abort_code;
668 afs_op_set_error(op, afs_abort_to_error(abort_code));
669 op->cumul_error.abort_code = abort_code;
670 }
671 break;
672
673 case 1:
674 vp = &op->file[1];
675 break;
676
677 default:
678 vp = &op->more_files[i - 2];
679 break;
680 }
681
682 if (vp->scb.status.abort_code)
683 trace_afs_bulkstat_error(op, &vp->fid, i, vp->scb.status.abort_code);
684 if (!vp->scb.have_status && !vp->scb.have_error)
685 continue;
686
687 _debug("do [%u]", i);
688 if (vp->vnode) {
689 if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
690 afs_vnode_commit_status(op, vp);
691 } else if (vp->scb.status.abort_code == 0) {
692 inode = afs_iget(op, vp);
693 if (!IS_ERR(inode)) {
694 vnode = AFS_FS_I(inode);
695 afs_cache_permit(vnode, op->key,
696 0 /* Assume vnode->cb_break is 0 */ +
697 op->cb_v_break,
698 &vp->scb);
699 vp->vnode = vnode;
700 vp->put_vnode = true;
701 }
702 } else {
703 _debug("- abort %d %llx:%llx.%x",
704 vp->scb.status.abort_code,
705 vp->fid.vid, vp->fid.vnode, vp->fid.unique);
706 }
707 }
708
709 _leave("");
710 }
711
712 static const struct afs_operation_ops afs_inline_bulk_status_operation = {
713 .issue_afs_rpc = afs_fs_inline_bulk_status,
714 .issue_yfs_rpc = yfs_fs_inline_bulk_status,
715 .success = afs_do_lookup_success,
716 };
717
718 static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
719 .issue_afs_rpc = afs_fs_fetch_status,
720 .issue_yfs_rpc = yfs_fs_fetch_status,
721 .success = afs_do_lookup_success,
722 .aborted = afs_check_for_remote_deletion,
723 };
724
725 /*
726 * See if we know that the server we expect to use doesn't support
727 * FS.InlineBulkStatus.
728 */
afs_server_supports_ibulk(struct afs_vnode * dvnode)729 static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
730 {
731 struct afs_server_list *slist;
732 struct afs_volume *volume = dvnode->volume;
733 struct afs_server *server;
734 bool ret = true;
735 int i;
736
737 if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
738 return true;
739
740 rcu_read_lock();
741 slist = rcu_dereference(volume->servers);
742
743 for (i = 0; i < slist->nr_servers; i++) {
744 server = slist->servers[i].server;
745 if (server == dvnode->cb_server) {
746 if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
747 ret = false;
748 break;
749 }
750 }
751
752 rcu_read_unlock();
753 return ret;
754 }
755
756 /*
757 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
758 * files in one go and create inodes for them. The inode of the file we were
759 * asked for is returned.
760 */
afs_do_lookup(struct inode * dir,struct dentry * dentry)761 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry)
762 {
763 struct afs_lookup_cookie *cookie;
764 struct afs_vnode_param *vp;
765 struct afs_operation *op;
766 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
767 struct inode *inode = NULL, *ti;
768 afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
769 bool supports_ibulk, isnew;
770 long ret;
771 int i;
772
773 _enter("{%llu},%p{%pd},", dir->i_ino, dentry, dentry);
774
775 cookie = kzalloc_obj(struct afs_lookup_cookie);
776 if (!cookie)
777 return ERR_PTR(-ENOMEM);
778
779 for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
780 cookie->fids[i].vid = dvnode->fid.vid;
781 cookie->ctx.actor = afs_lookup_filldir;
782 cookie->name = dentry->d_name;
783 cookie->nr_fids = 2; /* slot 1 is saved for the fid we actually want
784 * and slot 0 for the directory */
785
786 /* Search the directory for the named entry using the hash table... */
787 ret = afs_dir_search(dvnode, &dentry->d_name, &cookie->fids[1], &data_version);
788 if (ret < 0)
789 goto out;
790
791 supports_ibulk = afs_server_supports_ibulk(dvnode);
792 if (supports_ibulk) {
793 /* ...then scan linearly from that point for entries to lookup-ahead. */
794 cookie->ctx.pos = (ret + 1) * AFS_DIR_DIRENT_SIZE;
795 afs_dir_iterate(dir, &cookie->ctx, NULL, &data_version);
796 }
797
798 dentry->d_fsdata = (void *)(unsigned long)data_version;
799
800 /* Check to see if we already have an inode for the primary fid. */
801 inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
802 afs_ilookup5_test_by_fid, &cookie->fids[1]);
803 if (inode)
804 goto out; /* We do */
805
806 /* Okay, we didn't find it. We need to query the server - and whilst
807 * we're doing that, we're going to attempt to look up a bunch of other
808 * vnodes also.
809 */
810 op = afs_alloc_operation(NULL, dvnode->volume);
811 if (IS_ERR(op)) {
812 ret = PTR_ERR(op);
813 goto out;
814 }
815
816 afs_op_set_vnode(op, 0, dvnode);
817 afs_op_set_fid(op, 1, &cookie->fids[1]);
818
819 op->nr_files = cookie->nr_fids;
820 _debug("nr_files %u", op->nr_files);
821
822 /* Need space for examining all the selected files */
823 if (op->nr_files > 2) {
824 op->more_files = kvzalloc_objs(struct afs_vnode_param,
825 op->nr_files - 2);
826 if (!op->more_files) {
827 afs_op_nomem(op);
828 goto out_op;
829 }
830
831 for (i = 2; i < op->nr_files; i++) {
832 vp = &op->more_files[i - 2];
833 vp->fid = cookie->fids[i];
834
835 /* Find any inodes that already exist and get their
836 * callback counters.
837 */
838 ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
839 afs_ilookup5_test_by_fid, &vp->fid, &isnew);
840 if (!IS_ERR_OR_NULL(ti)) {
841 vnode = AFS_FS_I(ti);
842 vp->dv_before = vnode->status.data_version;
843 vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
844 vp->vnode = vnode;
845 vp->put_vnode = true;
846 vp->speculative = true; /* vnode not locked */
847 }
848 }
849 }
850
851 /* Try FS.InlineBulkStatus first. Abort codes for the individual
852 * lookups contained therein are stored in the reply without aborting
853 * the whole operation.
854 */
855 afs_op_set_error(op, -ENOTSUPP);
856 if (supports_ibulk) {
857 op->ops = &afs_inline_bulk_status_operation;
858 afs_begin_vnode_operation(op);
859 afs_wait_for_operation(op);
860 }
861
862 if (afs_op_error(op) == -ENOTSUPP) {
863 /* We could try FS.BulkStatus next, but this aborts the entire
864 * op if any of the lookups fails - so, for the moment, revert
865 * to FS.FetchStatus for op->file[1].
866 */
867 op->fetch_status.which = 1;
868 op->ops = &afs_lookup_fetch_status_operation;
869 afs_begin_vnode_operation(op);
870 afs_wait_for_operation(op);
871 }
872
873 out_op:
874 if (!afs_op_error(op)) {
875 if (op->file[1].scb.status.abort_code) {
876 afs_op_accumulate_error(op, -ECONNABORTED,
877 op->file[1].scb.status.abort_code);
878 } else {
879 inode = &op->file[1].vnode->netfs.inode;
880 op->file[1].vnode = NULL;
881 }
882 }
883
884 if (op->file[0].scb.have_status)
885 dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
886 else
887 dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
888 ret = afs_put_operation(op);
889 out:
890 kfree(cookie);
891 _leave("");
892 return inode ?: ERR_PTR(ret);
893 }
894
895 /*
896 * Look up an entry in a directory with @sys substitution.
897 */
afs_lookup_atsys(struct inode * dir,struct dentry * dentry)898 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
899 {
900 struct afs_sysnames *subs;
901 struct afs_net *net = afs_i2net(dir);
902 struct dentry *ret;
903 char *buf, *p, *name;
904 int len, i;
905
906 _enter("");
907
908 ret = ERR_PTR(-ENOMEM);
909 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
910 if (!buf)
911 goto out_p;
912 if (dentry->d_name.len > 4) {
913 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
914 p += dentry->d_name.len - 4;
915 }
916
917 /* There is an ordered list of substitutes that we have to try. */
918 read_lock(&net->sysnames_lock);
919 subs = net->sysnames;
920 refcount_inc(&subs->usage);
921 read_unlock(&net->sysnames_lock);
922
923 for (i = 0; i < subs->nr; i++) {
924 name = subs->subs[i];
925 len = dentry->d_name.len - 4 + strlen(name);
926 if (len >= AFSNAMEMAX) {
927 ret = ERR_PTR(-ENAMETOOLONG);
928 goto out_s;
929 }
930
931 strcpy(p, name);
932 ret = lookup_noperm(&QSTR(buf), dentry->d_parent);
933 if (IS_ERR(ret) || d_is_positive(ret))
934 goto out_s;
935 dput(ret);
936 }
937
938 /* We don't want to d_add() the @sys dentry here as we don't want to
939 * the cached dentry to hide changes to the sysnames list.
940 */
941 ret = NULL;
942 out_s:
943 afs_put_sysnames(subs);
944 kfree(buf);
945 out_p:
946 return ret;
947 }
948
949 /*
950 * look up an entry in a directory
951 */
afs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)952 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
953 unsigned int flags)
954 {
955 struct afs_vnode *dvnode = AFS_FS_I(dir);
956 struct afs_fid fid = {};
957 struct inode *inode;
958 struct dentry *d;
959 int ret;
960
961 _enter("{%llx:%llu},%p{%pd},",
962 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
963
964 ASSERTCMP(d_inode(dentry), ==, NULL);
965
966 if (dentry->d_name.len >= AFSNAMEMAX) {
967 _leave(" = -ENAMETOOLONG");
968 return ERR_PTR(-ENAMETOOLONG);
969 }
970
971 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
972 _leave(" = -ESTALE");
973 return ERR_PTR(-ESTALE);
974 }
975
976 ret = afs_validate(dvnode, NULL);
977 if (ret < 0) {
978 afs_dir_unuse_cookie(dvnode, ret);
979 _leave(" = %d [val]", ret);
980 return ERR_PTR(ret);
981 }
982
983 if (dentry->d_name.len >= 4 &&
984 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
985 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
986 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
987 dentry->d_name.name[dentry->d_name.len - 1] == 's')
988 return afs_lookup_atsys(dir, dentry);
989
990 afs_stat_v(dvnode, n_lookup);
991 inode = afs_do_lookup(dir, dentry);
992 if (inode == ERR_PTR(-ENOENT))
993 inode = NULL;
994 else if (!IS_ERR_OR_NULL(inode))
995 fid = AFS_FS_I(inode)->fid;
996
997 _debug("splice %p", dentry->d_inode);
998 d = d_splice_alias(inode, dentry);
999 if (!IS_ERR_OR_NULL(d)) {
1000 d->d_fsdata = dentry->d_fsdata;
1001 trace_afs_lookup(dvnode, &d->d_name, &fid);
1002 } else {
1003 trace_afs_lookup(dvnode, &dentry->d_name, &fid);
1004 }
1005 _leave("");
1006 return d;
1007 }
1008
1009 /*
1010 * Check the validity of a dentry under RCU conditions.
1011 */
afs_d_revalidate_rcu(struct afs_vnode * dvnode,struct dentry * dentry)1012 static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry)
1013 {
1014 long dir_version, de_version;
1015
1016 _enter("%p", dentry);
1017
1018 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
1019 return -ECHILD;
1020
1021 if (!afs_check_validity(dvnode))
1022 return -ECHILD;
1023
1024 /* We only need to invalidate a dentry if the server's copy changed
1025 * behind our back. If we made the change, it's no problem. Note that
1026 * on a 32-bit system, we only have 32 bits in the dentry to store the
1027 * version.
1028 */
1029 dir_version = (long)READ_ONCE(dvnode->status.data_version);
1030 de_version = (long)READ_ONCE(dentry->d_fsdata);
1031 if (de_version != dir_version) {
1032 dir_version = (long)READ_ONCE(dvnode->invalid_before);
1033 if (de_version - dir_version < 0)
1034 return -ECHILD;
1035 }
1036
1037 return 1; /* Still valid */
1038 }
1039
1040 /*
1041 * check that a dentry lookup hit has found a valid entry
1042 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
1043 * inode
1044 */
afs_d_revalidate(struct inode * parent_dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)1045 static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,
1046 struct dentry *dentry, unsigned int flags)
1047 {
1048 struct afs_vnode *vnode, *dir = AFS_FS_I(parent_dir);
1049 struct afs_fid fid;
1050 struct inode *inode;
1051 struct key *key;
1052 afs_dataversion_t dir_version, invalid_before;
1053 long de_version;
1054 int ret;
1055
1056 if (flags & LOOKUP_RCU)
1057 return afs_d_revalidate_rcu(dir, dentry);
1058
1059 if (d_really_is_positive(dentry)) {
1060 vnode = AFS_FS_I(d_inode(dentry));
1061 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
1062 vnode->fid.vid, vnode->fid.vnode, dentry,
1063 vnode->flags);
1064 } else {
1065 _enter("{neg n=%pd}", dentry);
1066 }
1067
1068 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
1069 if (IS_ERR(key))
1070 key = NULL;
1071
1072 /* validate the parent directory */
1073 ret = afs_validate(dir, key);
1074 if (ret == -ERESTARTSYS) {
1075 key_put(key);
1076 return ret;
1077 }
1078
1079 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1080 _debug("%pd: parent dir deleted", dentry);
1081 goto not_found;
1082 }
1083
1084 /* We only need to invalidate a dentry if the server's copy changed
1085 * behind our back. If we made the change, it's no problem. Note that
1086 * on a 32-bit system, we only have 32 bits in the dentry to store the
1087 * version.
1088 */
1089 dir_version = dir->status.data_version;
1090 de_version = (long)dentry->d_fsdata;
1091 if (de_version == (long)dir_version)
1092 goto out_valid_noupdate;
1093
1094 invalid_before = dir->invalid_before;
1095 if (de_version - (long)invalid_before >= 0)
1096 goto out_valid;
1097
1098 _debug("dir modified");
1099 afs_stat_v(dir, n_reval);
1100
1101 /* search the directory for this vnode */
1102 ret = afs_do_lookup_one(&dir->netfs.inode, name, &fid, &dir_version);
1103 switch (ret) {
1104 case 0:
1105 /* the filename maps to something */
1106 if (d_really_is_negative(dentry))
1107 goto not_found;
1108 inode = d_inode(dentry);
1109 if (is_bad_inode(inode)) {
1110 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1111 dentry);
1112 goto not_found;
1113 }
1114
1115 vnode = AFS_FS_I(inode);
1116
1117 /* if the vnode ID has changed, then the dirent points to a
1118 * different file */
1119 if (fid.vnode != vnode->fid.vnode) {
1120 _debug("%pd: dirent changed [%llu != %llu]",
1121 dentry, fid.vnode,
1122 vnode->fid.vnode);
1123 goto not_found;
1124 }
1125
1126 /* if the vnode ID uniqifier has changed, then the file has
1127 * been deleted and replaced, and the original vnode ID has
1128 * been reused */
1129 if (fid.unique != vnode->fid.unique) {
1130 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1131 dentry, fid.unique,
1132 vnode->fid.unique,
1133 vnode->netfs.inode.i_generation);
1134 goto not_found;
1135 }
1136 goto out_valid;
1137
1138 case -ENOENT:
1139 /* the filename is unknown */
1140 _debug("%pd: dirent not found", dentry);
1141 if (d_really_is_positive(dentry))
1142 goto not_found;
1143 goto out_valid;
1144
1145 default:
1146 _debug("failed to iterate parent %pd2: %d", dentry, ret);
1147 goto not_found;
1148 }
1149
1150 out_valid:
1151 dentry->d_fsdata = (void *)(unsigned long)dir_version;
1152 out_valid_noupdate:
1153 key_put(key);
1154 _leave(" = 1 [valid]");
1155 return 1;
1156
1157 not_found:
1158 _debug("dropping dentry %pd2", dentry);
1159 key_put(key);
1160
1161 _leave(" = 0 [bad]");
1162 return 0;
1163 }
1164
1165 /*
1166 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1167 * sleep)
1168 * - called from dput() when d_count is going to 0.
1169 * - return 1 to request dentry be unhashed, 0 otherwise
1170 */
afs_d_delete(const struct dentry * dentry)1171 static int afs_d_delete(const struct dentry *dentry)
1172 {
1173 _enter("%pd", dentry);
1174
1175 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1176 goto zap;
1177
1178 if (d_really_is_positive(dentry) &&
1179 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1180 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1181 goto zap;
1182
1183 _leave(" = 0 [keep]");
1184 return 0;
1185
1186 zap:
1187 _leave(" = 1 [zap]");
1188 return 1;
1189 }
1190
1191 /*
1192 * Clean up sillyrename files on dentry removal.
1193 */
afs_d_iput(struct dentry * dentry,struct inode * inode)1194 static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1195 {
1196 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1197 afs_silly_iput(dentry, inode);
1198 iput(inode);
1199 }
1200
1201 /*
1202 * handle dentry release
1203 */
afs_d_release(struct dentry * dentry)1204 void afs_d_release(struct dentry *dentry)
1205 {
1206 _enter("%pd", dentry);
1207 }
1208
afs_check_for_remote_deletion(struct afs_operation * op)1209 void afs_check_for_remote_deletion(struct afs_operation *op)
1210 {
1211 struct afs_vnode *vnode = op->file[0].vnode;
1212
1213 switch (afs_op_abort_code(op)) {
1214 case VNOVNODE:
1215 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1216 clear_nlink(&vnode->netfs.inode);
1217 afs_break_callback(vnode, afs_cb_break_for_deleted);
1218 }
1219 }
1220
1221 /*
1222 * Create a new inode for create/mkdir/symlink
1223 */
afs_vnode_new_inode(struct afs_operation * op)1224 static void afs_vnode_new_inode(struct afs_operation *op)
1225 {
1226 struct afs_vnode_param *dvp = &op->file[0];
1227 struct afs_vnode_param *vp = &op->file[1];
1228 struct afs_vnode *vnode;
1229 struct inode *inode;
1230
1231 _enter("");
1232
1233 ASSERTCMP(afs_op_error(op), ==, 0);
1234
1235 inode = afs_iget(op, vp);
1236 if (IS_ERR(inode)) {
1237 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1238 * the new directory on the server.
1239 */
1240 afs_op_accumulate_error(op, PTR_ERR(inode), 0);
1241 return;
1242 }
1243
1244 vnode = AFS_FS_I(inode);
1245 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1246 if (S_ISDIR(inode->i_mode))
1247 afs_mkdir_init_dir(vnode, dvp->vnode);
1248 else if (S_ISLNK(inode->i_mode))
1249 afs_init_new_symlink(vnode, op);
1250 if (!afs_op_error(op))
1251 afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
1252 d_instantiate(op->dentry, inode);
1253 }
1254
afs_create_success(struct afs_operation * op)1255 static void afs_create_success(struct afs_operation *op)
1256 {
1257 _enter("op=%08x", op->debug_id);
1258 op->ctime = op->file[0].scb.status.mtime_client;
1259 afs_vnode_commit_status(op, &op->file[0]);
1260 afs_update_dentry_version(op, &op->file[0], op->dentry);
1261 afs_vnode_new_inode(op);
1262 }
1263
afs_create_edit_dir(struct afs_operation * op)1264 static void afs_create_edit_dir(struct afs_operation *op)
1265 {
1266 struct netfs_cache_resources cres = {};
1267 struct afs_vnode_param *dvp = &op->file[0];
1268 struct afs_vnode_param *vp = &op->file[1];
1269 struct afs_vnode *dvnode = dvp->vnode;
1270
1271 _enter("op=%08x", op->debug_id);
1272
1273 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode));
1274 down_write(&dvnode->validate_lock);
1275 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1276 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1277 afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid,
1278 op->create.reason);
1279 up_write(&dvnode->validate_lock);
1280 fscache_end_operation(&cres);
1281 }
1282
afs_create_put(struct afs_operation * op)1283 static void afs_create_put(struct afs_operation *op)
1284 {
1285 _enter("op=%08x", op->debug_id);
1286
1287 if (afs_op_error(op))
1288 d_drop(op->dentry);
1289 }
1290
1291 static const struct afs_operation_ops afs_mkdir_operation = {
1292 .issue_afs_rpc = afs_fs_make_dir,
1293 .issue_yfs_rpc = yfs_fs_make_dir,
1294 .success = afs_create_success,
1295 .aborted = afs_check_for_remote_deletion,
1296 .edit_dir = afs_create_edit_dir,
1297 .put = afs_create_put,
1298 };
1299
1300 /*
1301 * create a directory on an AFS filesystem
1302 */
afs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)1303 static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1304 struct dentry *dentry, umode_t mode)
1305 {
1306 struct afs_operation *op;
1307 struct afs_vnode *dvnode = AFS_FS_I(dir);
1308 int ret;
1309
1310 _enter("{%llx:%llu},{%pd},%ho",
1311 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1312
1313 op = afs_alloc_operation(NULL, dvnode->volume);
1314 if (IS_ERR(op)) {
1315 d_drop(dentry);
1316 return ERR_CAST(op);
1317 }
1318
1319 fscache_use_cookie(afs_vnode_cache(dvnode), true);
1320
1321 afs_op_set_vnode(op, 0, dvnode);
1322 op->file[0].dv_delta = 1;
1323 op->file[0].modification = true;
1324 op->file[0].update_ctime = true;
1325 op->dentry = dentry;
1326 op->create.mode = S_IFDIR | mode;
1327 op->create.reason = afs_edit_dir_for_mkdir;
1328 op->mtime = current_time(dir);
1329 op->ops = &afs_mkdir_operation;
1330 ret = afs_do_sync_operation(op);
1331 afs_dir_unuse_cookie(dvnode, ret);
1332 return ERR_PTR(ret);
1333 }
1334
1335 /*
1336 * Remove a subdir from a directory.
1337 */
afs_dir_remove_subdir(struct dentry * dentry)1338 static void afs_dir_remove_subdir(struct dentry *dentry)
1339 {
1340 if (d_really_is_positive(dentry)) {
1341 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1342
1343 clear_nlink(&vnode->netfs.inode);
1344 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1345 afs_clear_cb_promise(vnode, afs_cb_promise_clear_rmdir);
1346 afs_invalidate_dir(vnode, afs_dir_invalid_subdir_removed);
1347 }
1348 }
1349
afs_rmdir_success(struct afs_operation * op)1350 static void afs_rmdir_success(struct afs_operation *op)
1351 {
1352 _enter("op=%08x", op->debug_id);
1353 op->ctime = op->file[0].scb.status.mtime_client;
1354 afs_vnode_commit_status(op, &op->file[0]);
1355 afs_update_dentry_version(op, &op->file[0], op->dentry);
1356 }
1357
afs_rmdir_edit_dir(struct afs_operation * op)1358 static void afs_rmdir_edit_dir(struct afs_operation *op)
1359 {
1360 struct netfs_cache_resources cres = {};
1361 struct afs_vnode_param *dvp = &op->file[0];
1362 struct afs_vnode *dvnode = dvp->vnode;
1363
1364 _enter("op=%08x", op->debug_id);
1365 afs_dir_remove_subdir(op->dentry);
1366
1367 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode));
1368 down_write(&dvnode->validate_lock);
1369 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1370 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1371 afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1372 afs_edit_dir_for_rmdir);
1373 up_write(&dvnode->validate_lock);
1374 fscache_end_operation(&cres);
1375 }
1376
afs_rmdir_put(struct afs_operation * op)1377 static void afs_rmdir_put(struct afs_operation *op)
1378 {
1379 _enter("op=%08x", op->debug_id);
1380 if (op->file[1].vnode)
1381 up_write(&op->file[1].vnode->rmdir_lock);
1382 }
1383
1384 static const struct afs_operation_ops afs_rmdir_operation = {
1385 .issue_afs_rpc = afs_fs_remove_dir,
1386 .issue_yfs_rpc = yfs_fs_remove_dir,
1387 .success = afs_rmdir_success,
1388 .aborted = afs_check_for_remote_deletion,
1389 .edit_dir = afs_rmdir_edit_dir,
1390 .put = afs_rmdir_put,
1391 };
1392
1393 /*
1394 * remove a directory from an AFS filesystem
1395 */
afs_rmdir(struct inode * dir,struct dentry * dentry)1396 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1397 {
1398 struct afs_operation *op;
1399 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1400 int ret;
1401
1402 _enter("{%llx:%llu},{%pd}",
1403 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1404
1405 op = afs_alloc_operation(NULL, dvnode->volume);
1406 if (IS_ERR(op))
1407 return PTR_ERR(op);
1408
1409 fscache_use_cookie(afs_vnode_cache(dvnode), true);
1410
1411 afs_op_set_vnode(op, 0, dvnode);
1412 op->file[0].dv_delta = 1;
1413 op->file[0].modification = true;
1414 op->file[0].update_ctime = true;
1415
1416 op->dentry = dentry;
1417 op->ops = &afs_rmdir_operation;
1418
1419 /* Try to make sure we have a callback promise on the victim. */
1420 if (d_really_is_positive(dentry)) {
1421 vnode = AFS_FS_I(d_inode(dentry));
1422 ret = afs_validate(vnode, op->key);
1423 if (ret < 0)
1424 goto error;
1425 }
1426
1427 if (vnode) {
1428 ret = down_write_killable(&vnode->rmdir_lock);
1429 if (ret < 0)
1430 goto error;
1431 op->file[1].vnode = vnode;
1432 }
1433
1434 ret = afs_do_sync_operation(op);
1435
1436 /* Not all systems that can host afs servers have ENOTEMPTY. */
1437 if (ret == -EEXIST)
1438 ret = -ENOTEMPTY;
1439 out:
1440 afs_dir_unuse_cookie(dvnode, ret);
1441 return ret;
1442
1443 error:
1444 ret = afs_put_operation(op);
1445 goto out;
1446 }
1447
1448 /*
1449 * Remove a link to a file or symlink from a directory.
1450 *
1451 * If the file was not deleted due to excess hard links, the fileserver will
1452 * break the callback promise on the file - if it had one - before it returns
1453 * to us, and if it was deleted, it won't
1454 *
1455 * However, if we didn't have a callback promise outstanding, or it was
1456 * outstanding on a different server, then it won't break it either...
1457 */
afs_dir_remove_link(struct afs_operation * op)1458 static void afs_dir_remove_link(struct afs_operation *op)
1459 {
1460 struct afs_vnode *dvnode = op->file[0].vnode;
1461 struct afs_vnode *vnode = op->file[1].vnode;
1462 struct dentry *dentry = op->dentry;
1463 int ret;
1464
1465 if (afs_op_error(op) ||
1466 (op->file[1].scb.have_status && op->file[1].scb.have_error))
1467 return;
1468 if (d_really_is_positive(dentry))
1469 return;
1470
1471 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1472 /* Already done */
1473 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1474 write_seqlock(&vnode->cb_lock);
1475 drop_nlink(&vnode->netfs.inode);
1476 if (vnode->netfs.inode.i_nlink == 0) {
1477 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1478 __afs_break_callback(vnode, afs_cb_break_for_unlink);
1479 }
1480 write_sequnlock(&vnode->cb_lock);
1481 } else {
1482 afs_break_callback(vnode, afs_cb_break_for_unlink);
1483
1484 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1485 _debug("AFS_VNODE_DELETED");
1486
1487 ret = afs_validate(vnode, op->key);
1488 if (ret != -ESTALE)
1489 afs_op_set_error(op, ret);
1490 }
1491
1492 _debug("nlink %d [val %d]", vnode->netfs.inode.i_nlink, afs_op_error(op));
1493 }
1494
afs_unlink_success(struct afs_operation * op)1495 static void afs_unlink_success(struct afs_operation *op)
1496 {
1497 _enter("op=%08x", op->debug_id);
1498 op->ctime = op->file[0].scb.status.mtime_client;
1499 afs_check_dir_conflict(op, &op->file[0]);
1500 afs_vnode_commit_status(op, &op->file[0]);
1501 afs_vnode_commit_status(op, &op->file[1]);
1502 afs_update_dentry_version(op, &op->file[0], op->dentry);
1503 afs_dir_remove_link(op);
1504 }
1505
afs_unlink_edit_dir(struct afs_operation * op)1506 static void afs_unlink_edit_dir(struct afs_operation *op)
1507 {
1508 struct netfs_cache_resources cres = {};
1509 struct afs_vnode_param *dvp = &op->file[0];
1510 struct afs_vnode *dvnode = dvp->vnode;
1511
1512 _enter("op=%08x", op->debug_id);
1513 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode));
1514 down_write(&dvnode->validate_lock);
1515 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1516 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1517 afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1518 afs_edit_dir_for_unlink);
1519 up_write(&dvnode->validate_lock);
1520 fscache_end_operation(&cres);
1521 }
1522
afs_unlink_put(struct afs_operation * op)1523 static void afs_unlink_put(struct afs_operation *op)
1524 {
1525 _enter("op=%08x", op->debug_id);
1526 if (op->unlink.need_rehash && afs_op_error(op) < 0 && afs_op_error(op) != -ENOENT)
1527 d_rehash(op->dentry);
1528 }
1529
1530 static const struct afs_operation_ops afs_unlink_operation = {
1531 .issue_afs_rpc = afs_fs_remove_file,
1532 .issue_yfs_rpc = yfs_fs_remove_file,
1533 .success = afs_unlink_success,
1534 .aborted = afs_check_for_remote_deletion,
1535 .edit_dir = afs_unlink_edit_dir,
1536 .put = afs_unlink_put,
1537 };
1538
1539 /*
1540 * Remove a file or symlink from an AFS filesystem.
1541 */
afs_unlink(struct inode * dir,struct dentry * dentry)1542 static int afs_unlink(struct inode *dir, struct dentry *dentry)
1543 {
1544 struct afs_operation *op;
1545 struct afs_vnode *dvnode = AFS_FS_I(dir);
1546 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1547 int ret;
1548
1549 _enter("{%llx:%llu},{%pd}",
1550 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1551
1552 if (dentry->d_name.len >= AFSNAMEMAX)
1553 return -ENAMETOOLONG;
1554
1555 op = afs_alloc_operation(NULL, dvnode->volume);
1556 if (IS_ERR(op))
1557 return PTR_ERR(op);
1558
1559 fscache_use_cookie(afs_vnode_cache(dvnode), true);
1560
1561 afs_op_set_vnode(op, 0, dvnode);
1562 op->file[0].dv_delta = 1;
1563 op->file[0].modification = true;
1564 op->file[0].update_ctime = true;
1565
1566 /* Try to make sure we have a callback promise on the victim. */
1567 ret = afs_validate(vnode, op->key);
1568 if (ret < 0) {
1569 afs_op_set_error(op, ret);
1570 goto error;
1571 }
1572
1573 spin_lock(&dentry->d_lock);
1574 if (d_count(dentry) > 1) {
1575 spin_unlock(&dentry->d_lock);
1576 /* Start asynchronous writeout of the inode */
1577 write_inode_now(d_inode(dentry), 0);
1578 afs_op_set_error(op, afs_sillyrename(dvnode, vnode, dentry, op->key));
1579 goto error;
1580 }
1581 if (!d_unhashed(dentry)) {
1582 /* Prevent a race with RCU lookup. */
1583 __d_drop(dentry);
1584 op->unlink.need_rehash = true;
1585 }
1586 spin_unlock(&dentry->d_lock);
1587
1588 op->file[1].vnode = vnode;
1589 op->file[1].update_ctime = true;
1590 op->file[1].op_unlinked = true;
1591 op->dentry = dentry;
1592 op->ops = &afs_unlink_operation;
1593 afs_begin_vnode_operation(op);
1594 afs_wait_for_operation(op);
1595
1596 /* If there was a conflict with a third party, check the status of the
1597 * unlinked vnode.
1598 */
1599 if (afs_op_error(op) == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
1600 op->file[1].update_ctime = false;
1601 op->fetch_status.which = 1;
1602 op->ops = &afs_fetch_status_operation;
1603 afs_begin_vnode_operation(op);
1604 afs_wait_for_operation(op);
1605 }
1606
1607 error:
1608 ret = afs_put_operation(op);
1609 afs_dir_unuse_cookie(dvnode, ret);
1610 return ret;
1611 }
1612
1613 static const struct afs_operation_ops afs_create_operation = {
1614 .issue_afs_rpc = afs_fs_create_file,
1615 .issue_yfs_rpc = yfs_fs_create_file,
1616 .success = afs_create_success,
1617 .aborted = afs_check_for_remote_deletion,
1618 .edit_dir = afs_create_edit_dir,
1619 .put = afs_create_put,
1620 };
1621
1622 /*
1623 * create a regular file on an AFS filesystem
1624 */
afs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)1625 static int afs_create(struct mnt_idmap *idmap, struct inode *dir,
1626 struct dentry *dentry, umode_t mode, bool excl)
1627 {
1628 struct afs_operation *op;
1629 struct afs_vnode *dvnode = AFS_FS_I(dir);
1630 int ret = -ENAMETOOLONG;
1631
1632 _enter("{%llx:%llu},{%pd},%ho",
1633 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1634
1635 if (dentry->d_name.len >= AFSNAMEMAX)
1636 goto error;
1637
1638 op = afs_alloc_operation(NULL, dvnode->volume);
1639 if (IS_ERR(op)) {
1640 ret = PTR_ERR(op);
1641 goto error;
1642 }
1643
1644 fscache_use_cookie(afs_vnode_cache(dvnode), true);
1645
1646 afs_op_set_vnode(op, 0, dvnode);
1647 op->file[0].dv_delta = 1;
1648 op->file[0].modification = true;
1649 op->file[0].update_ctime = true;
1650
1651 op->dentry = dentry;
1652 op->create.mode = S_IFREG | mode;
1653 op->create.reason = afs_edit_dir_for_create;
1654 op->mtime = current_time(dir);
1655 op->ops = &afs_create_operation;
1656 ret = afs_do_sync_operation(op);
1657 afs_dir_unuse_cookie(dvnode, ret);
1658 return ret;
1659
1660 error:
1661 d_drop(dentry);
1662 _leave(" = %d", ret);
1663 return ret;
1664 }
1665
afs_link_success(struct afs_operation * op)1666 static void afs_link_success(struct afs_operation *op)
1667 {
1668 struct afs_vnode_param *dvp = &op->file[0];
1669 struct afs_vnode_param *vp = &op->file[1];
1670
1671 _enter("op=%08x", op->debug_id);
1672 op->ctime = dvp->scb.status.mtime_client;
1673 afs_vnode_commit_status(op, dvp);
1674 afs_vnode_commit_status(op, vp);
1675 afs_update_dentry_version(op, dvp, op->dentry);
1676 if (op->dentry_2->d_parent == op->dentry->d_parent)
1677 afs_update_dentry_version(op, dvp, op->dentry_2);
1678 ihold(&vp->vnode->netfs.inode);
1679 d_instantiate(op->dentry, &vp->vnode->netfs.inode);
1680 }
1681
afs_link_put(struct afs_operation * op)1682 static void afs_link_put(struct afs_operation *op)
1683 {
1684 _enter("op=%08x", op->debug_id);
1685 if (afs_op_error(op))
1686 d_drop(op->dentry);
1687 }
1688
1689 static const struct afs_operation_ops afs_link_operation = {
1690 .issue_afs_rpc = afs_fs_link,
1691 .issue_yfs_rpc = yfs_fs_link,
1692 .success = afs_link_success,
1693 .aborted = afs_check_for_remote_deletion,
1694 .edit_dir = afs_create_edit_dir,
1695 .put = afs_link_put,
1696 };
1697
1698 /*
1699 * create a hard link between files in an AFS filesystem
1700 */
afs_link(struct dentry * from,struct inode * dir,struct dentry * dentry)1701 static int afs_link(struct dentry *from, struct inode *dir,
1702 struct dentry *dentry)
1703 {
1704 struct afs_operation *op;
1705 struct afs_vnode *dvnode = AFS_FS_I(dir);
1706 struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1707 int ret = -ENAMETOOLONG;
1708
1709 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1710 vnode->fid.vid, vnode->fid.vnode,
1711 dvnode->fid.vid, dvnode->fid.vnode,
1712 dentry);
1713
1714 if (dentry->d_name.len >= AFSNAMEMAX)
1715 goto error;
1716
1717 op = afs_alloc_operation(NULL, dvnode->volume);
1718 if (IS_ERR(op)) {
1719 ret = PTR_ERR(op);
1720 goto error;
1721 }
1722
1723 fscache_use_cookie(afs_vnode_cache(dvnode), true);
1724
1725 ret = afs_validate(vnode, op->key);
1726 if (ret < 0)
1727 goto error_op;
1728
1729 afs_op_set_vnode(op, 0, dvnode);
1730 afs_op_set_vnode(op, 1, vnode);
1731 op->file[0].dv_delta = 1;
1732 op->file[0].modification = true;
1733 op->file[0].update_ctime = true;
1734 op->file[1].update_ctime = true;
1735
1736 op->dentry = dentry;
1737 op->dentry_2 = from;
1738 op->ops = &afs_link_operation;
1739 op->create.reason = afs_edit_dir_for_link;
1740 ret = afs_do_sync_operation(op);
1741 afs_dir_unuse_cookie(dvnode, ret);
1742 return ret;
1743
1744 error_op:
1745 afs_put_operation(op);
1746 afs_dir_unuse_cookie(dvnode, ret);
1747 error:
1748 d_drop(dentry);
1749 _leave(" = %d", ret);
1750 return ret;
1751 }
1752
afs_symlink_put(struct afs_operation * op)1753 static void afs_symlink_put(struct afs_operation *op)
1754 {
1755 kfree(op->create.symlink);
1756 op->create.symlink = NULL;
1757 afs_create_put(op);
1758 }
1759
1760 static const struct afs_operation_ops afs_symlink_operation = {
1761 .issue_afs_rpc = afs_fs_symlink,
1762 .issue_yfs_rpc = yfs_fs_symlink,
1763 .success = afs_create_success,
1764 .aborted = afs_check_for_remote_deletion,
1765 .edit_dir = afs_create_edit_dir,
1766 .put = afs_symlink_put,
1767 };
1768
1769 /*
1770 * create a symlink in an AFS filesystem
1771 */
afs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * content)1772 static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1773 struct dentry *dentry, const char *content)
1774 {
1775 struct afs_operation *op;
1776 struct afs_symlink *symlink;
1777 struct afs_vnode *dvnode = AFS_FS_I(dir);
1778 size_t clen = strlen(content);
1779 int ret;
1780
1781 _enter("{%llx:%llu},{%pd},%s",
1782 dvnode->fid.vid, dvnode->fid.vnode, dentry,
1783 content);
1784
1785 ret = -ENAMETOOLONG;
1786 if (dentry->d_name.len >= AFSNAMEMAX)
1787 goto error;
1788
1789 ret = -EINVAL;
1790 if (clen >= AFSPATHMAX)
1791 goto error;
1792
1793 ret = -ENOMEM;
1794 symlink = kmalloc_flex(struct afs_symlink, content, clen + 1, GFP_KERNEL);
1795 if (!symlink)
1796 goto error;
1797 refcount_set(&symlink->ref, 1);
1798 memcpy(symlink->content, content, clen + 1);
1799
1800 op = afs_alloc_operation(NULL, dvnode->volume);
1801 if (IS_ERR(op)) {
1802 ret = PTR_ERR(op);
1803 kfree(symlink);
1804 goto error;
1805 }
1806
1807 fscache_use_cookie(afs_vnode_cache(dvnode), true);
1808
1809 afs_op_set_vnode(op, 0, dvnode);
1810 op->file[0].dv_delta = 1;
1811
1812 op->dentry = dentry;
1813 op->ops = &afs_symlink_operation;
1814 op->create.reason = afs_edit_dir_for_symlink;
1815 op->create.symlink = symlink;
1816 op->mtime = current_time(dir);
1817 ret = afs_do_sync_operation(op);
1818 afs_dir_unuse_cookie(dvnode, ret);
1819 return ret;
1820
1821 error:
1822 d_drop(dentry);
1823 _leave(" = %d", ret);
1824 return ret;
1825 }
1826
afs_rename_success(struct afs_operation * op)1827 static void afs_rename_success(struct afs_operation *op)
1828 {
1829 struct afs_vnode *vnode = op->more_files[0].vnode;
1830 struct afs_vnode *new_vnode = op->more_files[1].vnode;
1831
1832 _enter("op=%08x", op->debug_id);
1833
1834 op->ctime = op->file[0].scb.status.mtime_client;
1835 afs_check_dir_conflict(op, &op->file[1]);
1836 afs_vnode_commit_status(op, &op->file[0]);
1837 if (op->file[1].vnode != op->file[0].vnode) {
1838 op->ctime = op->file[1].scb.status.mtime_client;
1839 afs_vnode_commit_status(op, &op->file[1]);
1840 }
1841 if (op->more_files[0].scb.have_status)
1842 afs_vnode_commit_status(op, &op->more_files[0]);
1843 if (op->more_files[1].scb.have_status)
1844 afs_vnode_commit_status(op, &op->more_files[1]);
1845
1846 /* If we're moving a subdir between dirs, we need to update
1847 * its DV counter too as the ".." will be altered.
1848 */
1849 if (op->file[0].vnode != op->file[1].vnode) {
1850 if (S_ISDIR(vnode->netfs.inode.i_mode)) {
1851 u64 new_dv;
1852
1853 write_seqlock(&vnode->cb_lock);
1854
1855 new_dv = vnode->status.data_version + 1;
1856 trace_afs_set_dv(vnode, new_dv);
1857 vnode->status.data_version = new_dv;
1858 inode_set_iversion_raw(&vnode->netfs.inode, new_dv);
1859
1860 write_sequnlock(&vnode->cb_lock);
1861 }
1862
1863 if ((op->rename.rename_flags & RENAME_EXCHANGE) &&
1864 S_ISDIR(new_vnode->netfs.inode.i_mode)) {
1865 u64 new_dv;
1866
1867 write_seqlock(&new_vnode->cb_lock);
1868
1869 new_dv = new_vnode->status.data_version + 1;
1870 new_vnode->status.data_version = new_dv;
1871 inode_set_iversion_raw(&new_vnode->netfs.inode, new_dv);
1872
1873 write_sequnlock(&new_vnode->cb_lock);
1874 }
1875 }
1876 }
1877
afs_rename_edit_dir(struct afs_operation * op)1878 static void afs_rename_edit_dir(struct afs_operation *op)
1879 {
1880 struct netfs_cache_resources orig_cres = {}, new_cres = {};
1881 struct afs_vnode_param *orig_dvp = &op->file[0];
1882 struct afs_vnode_param *new_dvp = &op->file[1];
1883 struct afs_vnode *orig_dvnode = orig_dvp->vnode;
1884 struct afs_vnode *new_dvnode = new_dvp->vnode;
1885 struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
1886 struct dentry *old_dentry = op->dentry;
1887 struct dentry *new_dentry = op->dentry_2;
1888 struct inode *new_inode;
1889
1890 _enter("op=%08x", op->debug_id);
1891
1892 if (op->rename.rehash) {
1893 d_rehash(op->rename.rehash);
1894 op->rename.rehash = NULL;
1895 }
1896
1897 fscache_begin_write_operation(&orig_cres, afs_vnode_cache(orig_dvnode));
1898 if (new_dvnode != orig_dvnode)
1899 fscache_begin_write_operation(&new_cres, afs_vnode_cache(new_dvnode));
1900
1901 down_write(&orig_dvnode->validate_lock);
1902 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
1903 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
1904 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1905 afs_edit_dir_for_rename_0);
1906
1907 if (new_dvnode != orig_dvnode) {
1908 up_write(&orig_dvnode->validate_lock);
1909 down_write(&new_dvnode->validate_lock);
1910 }
1911
1912 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
1913 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) {
1914 if (!op->rename.new_negative)
1915 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1916 afs_edit_dir_for_rename_1);
1917
1918 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1919 &vnode->fid, afs_edit_dir_for_rename_2);
1920 }
1921
1922 if (S_ISDIR(vnode->netfs.inode.i_mode) &&
1923 new_dvnode != orig_dvnode &&
1924 test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
1925 afs_edit_dir_update(vnode, &dotdot_name, new_dvnode,
1926 afs_edit_dir_for_rename_sub);
1927
1928 new_inode = d_inode(new_dentry);
1929 if (new_inode) {
1930 spin_lock(&new_inode->i_lock);
1931 if (S_ISDIR(new_inode->i_mode))
1932 clear_nlink(new_inode);
1933 else if (new_inode->i_nlink > 0)
1934 drop_nlink(new_inode);
1935 spin_unlock(&new_inode->i_lock);
1936 }
1937
1938 /* Now we can update d_fsdata on the dentries to reflect their
1939 * new parent's data_version.
1940 */
1941 afs_update_dentry_version(op, new_dvp, op->dentry);
1942 afs_update_dentry_version(op, new_dvp, op->dentry_2);
1943
1944 d_move(old_dentry, new_dentry);
1945
1946 up_write(&new_dvnode->validate_lock);
1947 fscache_end_operation(&orig_cres);
1948 if (new_dvnode != orig_dvnode)
1949 fscache_end_operation(&new_cres);
1950 }
1951
afs_rename_exchange_edit_dir(struct afs_operation * op)1952 static void afs_rename_exchange_edit_dir(struct afs_operation *op)
1953 {
1954 struct afs_vnode_param *orig_dvp = &op->file[0];
1955 struct afs_vnode_param *new_dvp = &op->file[1];
1956 struct afs_vnode *orig_dvnode = orig_dvp->vnode;
1957 struct afs_vnode *new_dvnode = new_dvp->vnode;
1958 struct afs_vnode *old_vnode = op->more_files[0].vnode;
1959 struct afs_vnode *new_vnode = op->more_files[1].vnode;
1960 struct dentry *old_dentry = op->dentry;
1961 struct dentry *new_dentry = op->dentry_2;
1962
1963 _enter("op=%08x", op->debug_id);
1964
1965 if (new_dvnode == orig_dvnode) {
1966 down_write(&orig_dvnode->validate_lock);
1967 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
1968 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) {
1969 afs_edit_dir_update(orig_dvnode, &old_dentry->d_name,
1970 new_vnode, afs_edit_dir_for_rename_0);
1971 afs_edit_dir_update(orig_dvnode, &new_dentry->d_name,
1972 old_vnode, afs_edit_dir_for_rename_1);
1973 }
1974
1975 d_exchange(old_dentry, new_dentry);
1976 up_write(&orig_dvnode->validate_lock);
1977 } else {
1978 down_write(&orig_dvnode->validate_lock);
1979 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
1980 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
1981 afs_edit_dir_update(orig_dvnode, &old_dentry->d_name,
1982 new_vnode, afs_edit_dir_for_rename_0);
1983
1984 up_write(&orig_dvnode->validate_lock);
1985 down_write(&new_dvnode->validate_lock);
1986
1987 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
1988 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta)
1989 afs_edit_dir_update(new_dvnode, &new_dentry->d_name,
1990 old_vnode, afs_edit_dir_for_rename_1);
1991
1992 if (S_ISDIR(old_vnode->netfs.inode.i_mode) &&
1993 test_bit(AFS_VNODE_DIR_VALID, &old_vnode->flags))
1994 afs_edit_dir_update(old_vnode, &dotdot_name, new_dvnode,
1995 afs_edit_dir_for_rename_sub);
1996
1997 if (S_ISDIR(new_vnode->netfs.inode.i_mode) &&
1998 test_bit(AFS_VNODE_DIR_VALID, &new_vnode->flags))
1999 afs_edit_dir_update(new_vnode, &dotdot_name, orig_dvnode,
2000 afs_edit_dir_for_rename_sub);
2001
2002 /* Now we can update d_fsdata on the dentries to reflect their
2003 * new parents' data_version.
2004 */
2005 afs_update_dentry_version(op, new_dvp, old_dentry);
2006 afs_update_dentry_version(op, orig_dvp, new_dentry);
2007
2008 d_exchange(old_dentry, new_dentry);
2009 up_write(&new_dvnode->validate_lock);
2010 }
2011 }
2012
afs_rename_put(struct afs_operation * op)2013 static void afs_rename_put(struct afs_operation *op)
2014 {
2015 _enter("op=%08x", op->debug_id);
2016 if (op->rename.rehash)
2017 d_rehash(op->rename.rehash);
2018 dput(op->rename.tmp);
2019 if (afs_op_error(op))
2020 d_rehash(op->dentry);
2021 }
2022
2023 static const struct afs_operation_ops afs_rename_operation = {
2024 .issue_afs_rpc = afs_fs_rename,
2025 .issue_yfs_rpc = yfs_fs_rename,
2026 .success = afs_rename_success,
2027 .edit_dir = afs_rename_edit_dir,
2028 .put = afs_rename_put,
2029 };
2030
2031 #if 0 /* Autoswitched in yfs_fs_rename_replace(). */
2032 static const struct afs_operation_ops afs_rename_replace_operation = {
2033 .issue_afs_rpc = NULL,
2034 .issue_yfs_rpc = yfs_fs_rename_replace,
2035 .success = afs_rename_success,
2036 .edit_dir = afs_rename_edit_dir,
2037 .put = afs_rename_put,
2038 };
2039 #endif
2040
2041 static const struct afs_operation_ops afs_rename_noreplace_operation = {
2042 .issue_afs_rpc = NULL,
2043 .issue_yfs_rpc = yfs_fs_rename_noreplace,
2044 .success = afs_rename_success,
2045 .edit_dir = afs_rename_edit_dir,
2046 .put = afs_rename_put,
2047 };
2048
2049 static const struct afs_operation_ops afs_rename_exchange_operation = {
2050 .issue_afs_rpc = NULL,
2051 .issue_yfs_rpc = yfs_fs_rename_exchange,
2052 .success = afs_rename_success,
2053 .edit_dir = afs_rename_exchange_edit_dir,
2054 .put = afs_rename_put,
2055 };
2056
2057 /*
2058 * rename a file in an AFS filesystem and/or move it between directories
2059 */
afs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)2060 static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
2061 struct dentry *old_dentry, struct inode *new_dir,
2062 struct dentry *new_dentry, unsigned int flags)
2063 {
2064 struct afs_operation *op;
2065 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode, *new_vnode = NULL;
2066 int ret;
2067
2068 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
2069 return -EINVAL;
2070
2071 /* Don't allow silly-rename files be moved around. */
2072 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
2073 return -EINVAL;
2074
2075 vnode = AFS_FS_I(d_inode(old_dentry));
2076 orig_dvnode = AFS_FS_I(old_dir);
2077 new_dvnode = AFS_FS_I(new_dir);
2078 if (d_is_positive(new_dentry))
2079 new_vnode = AFS_FS_I(d_inode(new_dentry));
2080
2081 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
2082 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
2083 vnode->fid.vid, vnode->fid.vnode,
2084 new_dvnode->fid.vid, new_dvnode->fid.vnode,
2085 new_dentry);
2086
2087 op = afs_alloc_operation(NULL, orig_dvnode->volume);
2088 if (IS_ERR(op))
2089 return PTR_ERR(op);
2090
2091 fscache_use_cookie(afs_vnode_cache(orig_dvnode), true);
2092 if (new_dvnode != orig_dvnode)
2093 fscache_use_cookie(afs_vnode_cache(new_dvnode), true);
2094
2095 ret = afs_validate(vnode, op->key);
2096 afs_op_set_error(op, ret);
2097 if (ret < 0)
2098 goto error;
2099
2100 ret = -ENOMEM;
2101 op->more_files = kvzalloc_objs(struct afs_vnode_param, 2);
2102 if (!op->more_files)
2103 goto error;
2104
2105 afs_op_set_vnode(op, 0, orig_dvnode);
2106 afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */
2107 op->file[0].dv_delta = 1;
2108 op->file[1].dv_delta = 1;
2109 op->file[0].modification = true;
2110 op->file[1].modification = true;
2111 op->file[0].update_ctime = true;
2112 op->file[1].update_ctime = true;
2113 op->more_files[0].vnode = vnode;
2114 op->more_files[0].speculative = true;
2115 op->more_files[1].vnode = new_vnode;
2116 op->more_files[1].speculative = true;
2117 op->nr_files = 4;
2118
2119 op->dentry = old_dentry;
2120 op->dentry_2 = new_dentry;
2121 op->rename.rename_flags = flags;
2122 op->rename.new_negative = d_is_negative(new_dentry);
2123
2124 if (flags & RENAME_NOREPLACE) {
2125 op->ops = &afs_rename_noreplace_operation;
2126 } else if (flags & RENAME_EXCHANGE) {
2127 op->ops = &afs_rename_exchange_operation;
2128 d_drop(new_dentry);
2129 } else {
2130 /* If we might displace the target, we might need to do silly
2131 * rename.
2132 */
2133 op->ops = &afs_rename_operation;
2134
2135 /* For non-directories, check whether the target is busy and if
2136 * so, make a copy of the dentry and then do a silly-rename.
2137 * If the silly-rename succeeds, the copied dentry is hashed
2138 * and becomes the new target.
2139 */
2140 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
2141 /* To prevent any new references to the target during
2142 * the rename, we unhash the dentry in advance.
2143 */
2144 if (!d_unhashed(new_dentry)) {
2145 d_drop(new_dentry);
2146 op->rename.rehash = new_dentry;
2147 }
2148
2149 if (d_count(new_dentry) > 2) {
2150 /* copy the target dentry's name */
2151 op->rename.tmp = d_alloc(new_dentry->d_parent,
2152 &new_dentry->d_name);
2153 if (!op->rename.tmp) {
2154 afs_op_nomem(op);
2155 goto error;
2156 }
2157
2158 ret = afs_sillyrename(new_dvnode,
2159 AFS_FS_I(d_inode(new_dentry)),
2160 new_dentry, op->key);
2161 if (ret) {
2162 afs_op_set_error(op, ret);
2163 goto error;
2164 }
2165
2166 op->dentry_2 = op->rename.tmp;
2167 op->rename.rehash = NULL;
2168 op->rename.new_negative = true;
2169 }
2170 }
2171 }
2172
2173 /* This bit is potentially nasty as there's a potential race with
2174 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry
2175 * to reflect it's new parent's new data_version after the op, but
2176 * d_revalidate may see old_dentry between the op having taken place
2177 * and the version being updated.
2178 *
2179 * So drop the old_dentry for now to make other threads go through
2180 * lookup instead - which we hold a lock against.
2181 */
2182 d_drop(old_dentry);
2183
2184 ret = afs_do_sync_operation(op);
2185 if (ret == -ENOTSUPP)
2186 ret = -EINVAL;
2187 out:
2188 afs_dir_unuse_cookie(orig_dvnode, ret);
2189 if (new_dvnode != orig_dvnode)
2190 afs_dir_unuse_cookie(new_dvnode, ret);
2191 return ret;
2192
2193 error:
2194 ret = afs_put_operation(op);
2195 goto out;
2196 }
2197
2198 /*
2199 * Write the directory contents to the cache as a single blob.
2200 */
afs_dir_writepages(struct address_space * mapping,struct writeback_control * wbc)2201 static int afs_dir_writepages(struct address_space *mapping,
2202 struct writeback_control *wbc)
2203 {
2204 struct afs_vnode *dvnode = AFS_FS_I(mapping->host);
2205 struct iov_iter iter;
2206 int ret = 0;
2207
2208 /* Need to lock to prevent the folio queue and folios from being thrown
2209 * away.
2210 */
2211 if (!down_read_trylock(&dvnode->validate_lock)) {
2212 if (wbc->sync_mode == WB_SYNC_NONE) {
2213 /* The VFS will have undirtied the inode. */
2214 netfs_single_mark_inode_dirty(&dvnode->netfs.inode);
2215 return 0;
2216 }
2217 down_read(&dvnode->validate_lock);
2218 }
2219
2220 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
2221 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0,
2222 i_size_read(&dvnode->netfs.inode));
2223 ret = netfs_writeback_single(mapping, wbc, &iter);
2224 if (ret == 1)
2225 ret = 0; /* Skipped write due to lock conflict. */
2226 }
2227
2228 up_read(&dvnode->validate_lock);
2229 return ret;
2230 }
2231