afs: Implement sillyrename for unlink and rename
[linux-2.6-microblaze.git] / fs / afs / dir.c
1 /* dir.c: AFS filesystem directory handling
2  *
3  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/fs.h>
14 #include <linux/namei.h>
15 #include <linux/pagemap.h>
16 #include <linux/swap.h>
17 #include <linux/ctype.h>
18 #include <linux/sched.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include "internal.h"
21 #include "xdr_fs.h"
22
23 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
24                                  unsigned int flags);
25 static int afs_dir_open(struct inode *inode, struct file *file);
26 static int afs_readdir(struct file *file, struct dir_context *ctx);
27 static int afs_d_revalidate(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 int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
31                                   loff_t fpos, u64 ino, unsigned dtype);
32 static int 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 inode *dir, struct dentry *dentry, umode_t mode,
35                       bool excl);
36 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
37 static int afs_rmdir(struct inode *dir, struct dentry *dentry);
38 static int afs_unlink(struct inode *dir, struct dentry *dentry);
39 static int afs_link(struct dentry *from, struct inode *dir,
40                     struct dentry *dentry);
41 static int afs_symlink(struct inode *dir, struct dentry *dentry,
42                        const char *content);
43 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
44                       struct inode *new_dir, struct dentry *new_dentry,
45                       unsigned int flags);
46 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
47 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
48                                    unsigned int length);
49
50 static int afs_dir_set_page_dirty(struct page *page)
51 {
52         BUG(); /* This should never happen. */
53 }
54
55 const struct file_operations afs_dir_file_operations = {
56         .open           = afs_dir_open,
57         .release        = afs_release,
58         .iterate_shared = afs_readdir,
59         .lock           = afs_lock,
60         .llseek         = generic_file_llseek,
61 };
62
63 const struct inode_operations afs_dir_inode_operations = {
64         .create         = afs_create,
65         .lookup         = afs_lookup,
66         .link           = afs_link,
67         .unlink         = afs_unlink,
68         .symlink        = afs_symlink,
69         .mkdir          = afs_mkdir,
70         .rmdir          = afs_rmdir,
71         .rename         = afs_rename,
72         .permission     = afs_permission,
73         .getattr        = afs_getattr,
74         .setattr        = afs_setattr,
75         .listxattr      = afs_listxattr,
76 };
77
78 const struct address_space_operations afs_dir_aops = {
79         .set_page_dirty = afs_dir_set_page_dirty,
80         .releasepage    = afs_dir_releasepage,
81         .invalidatepage = afs_dir_invalidatepage,
82 };
83
84 const struct dentry_operations afs_fs_dentry_operations = {
85         .d_revalidate   = afs_d_revalidate,
86         .d_delete       = afs_d_delete,
87         .d_release      = afs_d_release,
88         .d_automount    = afs_d_automount,
89         .d_iput         = afs_d_iput,
90 };
91
92 struct afs_lookup_one_cookie {
93         struct dir_context      ctx;
94         struct qstr             name;
95         bool                    found;
96         struct afs_fid          fid;
97 };
98
99 struct afs_lookup_cookie {
100         struct dir_context      ctx;
101         struct qstr             name;
102         bool                    found;
103         bool                    one_only;
104         unsigned short          nr_fids;
105         struct afs_file_status  *statuses;
106         struct afs_callback     *callbacks;
107         struct afs_fid          fids[50];
108 };
109
110 /*
111  * check that a directory page is valid
112  */
113 static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
114                                loff_t i_size)
115 {
116         struct afs_xdr_dir_page *dbuf;
117         loff_t latter, off;
118         int tmp, qty;
119
120         /* Determine how many magic numbers there should be in this page, but
121          * we must take care because the directory may change size under us.
122          */
123         off = page_offset(page);
124         if (i_size <= off)
125                 goto checked;
126
127         latter = i_size - off;
128         if (latter >= PAGE_SIZE)
129                 qty = PAGE_SIZE;
130         else
131                 qty = latter;
132         qty /= sizeof(union afs_xdr_dir_block);
133
134         /* check them */
135         dbuf = kmap(page);
136         for (tmp = 0; tmp < qty; tmp++) {
137                 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
138                         printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
139                                __func__, dvnode->vfs_inode.i_ino, tmp, qty,
140                                ntohs(dbuf->blocks[tmp].hdr.magic));
141                         trace_afs_dir_check_failed(dvnode, off, i_size);
142                         kunmap(page);
143                         trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
144                         goto error;
145                 }
146
147                 /* Make sure each block is NUL terminated so we can reasonably
148                  * use string functions on it.  The filenames in the page
149                  * *should* be NUL-terminated anyway.
150                  */
151                 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
152         }
153
154         kunmap(page);
155
156 checked:
157         afs_stat_v(dvnode, n_read_dir);
158         return true;
159
160 error:
161         return false;
162 }
163
164 /*
165  * Check the contents of a directory that we've just read.
166  */
167 static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
168 {
169         struct afs_xdr_dir_page *dbuf;
170         unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
171
172         for (i = 0; i < req->nr_pages; i++)
173                 if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
174                         goto bad;
175         return true;
176
177 bad:
178         pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
179                 dvnode->fid.vid, dvnode->fid.vnode,
180                 req->file_size, req->len, req->actual_len, req->remain);
181         pr_warn("DIR %llx %x %x %x\n",
182                 req->pos, req->index, req->nr_pages, req->offset);
183
184         for (i = 0; i < req->nr_pages; i++) {
185                 dbuf = kmap(req->pages[i]);
186                 for (j = 0; j < qty; j++) {
187                         union afs_xdr_dir_block *block = &dbuf->blocks[j];
188
189                         pr_warn("[%02x] %32phN\n", i * qty + j, block);
190                 }
191                 kunmap(req->pages[i]);
192         }
193         return false;
194 }
195
196 /*
197  * open an AFS directory file
198  */
199 static int afs_dir_open(struct inode *inode, struct file *file)
200 {
201         _enter("{%lu}", inode->i_ino);
202
203         BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
204         BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
205
206         if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
207                 return -ENOENT;
208
209         return afs_open(inode, file);
210 }
211
212 /*
213  * Read the directory into the pagecache in one go, scrubbing the previous
214  * contents.  The list of pages is returned, pinning them so that they don't
215  * get reclaimed during the iteration.
216  */
217 static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
218         __acquires(&dvnode->validate_lock)
219 {
220         struct afs_read *req;
221         loff_t i_size;
222         int nr_pages, nr_inline, i, n;
223         int ret = -ENOMEM;
224
225 retry:
226         i_size = i_size_read(&dvnode->vfs_inode);
227         if (i_size < 2048)
228                 return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
229         if (i_size > 2048 * 1024) {
230                 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
231                 return ERR_PTR(-EFBIG);
232         }
233
234         _enter("%llu", i_size);
235
236         /* Get a request record to hold the page list.  We want to hold it
237          * inline if we can, but we don't want to make an order 1 allocation.
238          */
239         nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
240         nr_inline = nr_pages;
241         if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
242                 nr_inline = 0;
243
244         req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
245                       GFP_KERNEL);
246         if (!req)
247                 return ERR_PTR(-ENOMEM);
248
249         refcount_set(&req->usage, 1);
250         req->nr_pages = nr_pages;
251         req->actual_len = i_size; /* May change */
252         req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
253         req->data_version = dvnode->status.data_version; /* May change */
254         if (nr_inline > 0) {
255                 req->pages = req->array;
256         } else {
257                 req->pages = kcalloc(nr_pages, sizeof(struct page *),
258                                      GFP_KERNEL);
259                 if (!req->pages)
260                         goto error;
261         }
262
263         /* Get a list of all the pages that hold or will hold the directory
264          * content.  We need to fill in any gaps that we might find where the
265          * memory reclaimer has been at work.  If there are any gaps, we will
266          * need to reread the entire directory contents.
267          */
268         i = 0;
269         do {
270                 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
271                                           req->nr_pages - i,
272                                           req->pages + i);
273                 _debug("find %u at %u/%u", n, i, req->nr_pages);
274                 if (n == 0) {
275                         gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
276
277                         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
278                                 afs_stat_v(dvnode, n_inval);
279
280                         ret = -ENOMEM;
281                         req->pages[i] = __page_cache_alloc(gfp);
282                         if (!req->pages[i])
283                                 goto error;
284                         ret = add_to_page_cache_lru(req->pages[i],
285                                                     dvnode->vfs_inode.i_mapping,
286                                                     i, gfp);
287                         if (ret < 0)
288                                 goto error;
289
290                         set_page_private(req->pages[i], 1);
291                         SetPagePrivate(req->pages[i]);
292                         unlock_page(req->pages[i]);
293                         i++;
294                 } else {
295                         i += n;
296                 }
297         } while (i < req->nr_pages);
298
299         /* If we're going to reload, we need to lock all the pages to prevent
300          * races.
301          */
302         ret = -ERESTARTSYS;
303         if (down_read_killable(&dvnode->validate_lock) < 0)
304                 goto error;
305
306         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
307                 goto success;
308
309         up_read(&dvnode->validate_lock);
310         if (down_write_killable(&dvnode->validate_lock) < 0)
311                 goto error;
312
313         if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
314                 trace_afs_reload_dir(dvnode);
315                 ret = afs_fetch_data(dvnode, key, req);
316                 if (ret < 0)
317                         goto error_unlock;
318
319                 task_io_account_read(PAGE_SIZE * req->nr_pages);
320
321                 if (req->len < req->file_size)
322                         goto content_has_grown;
323
324                 /* Validate the data we just read. */
325                 ret = -EIO;
326                 if (!afs_dir_check_pages(dvnode, req))
327                         goto error_unlock;
328
329                 // TODO: Trim excess pages
330
331                 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
332         }
333
334         downgrade_write(&dvnode->validate_lock);
335 success:
336         return req;
337
338 error_unlock:
339         up_write(&dvnode->validate_lock);
340 error:
341         afs_put_read(req);
342         _leave(" = %d", ret);
343         return ERR_PTR(ret);
344
345 content_has_grown:
346         up_write(&dvnode->validate_lock);
347         afs_put_read(req);
348         goto retry;
349 }
350
351 /*
352  * deal with one block in an AFS directory
353  */
354 static int afs_dir_iterate_block(struct afs_vnode *dvnode,
355                                  struct dir_context *ctx,
356                                  union afs_xdr_dir_block *block,
357                                  unsigned blkoff)
358 {
359         union afs_xdr_dirent *dire;
360         unsigned offset, next, curr;
361         size_t nlen;
362         int tmp;
363
364         _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
365
366         curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
367
368         /* walk through the block, an entry at a time */
369         for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
370              offset < AFS_DIR_SLOTS_PER_BLOCK;
371              offset = next
372              ) {
373                 next = offset + 1;
374
375                 /* skip entries marked unused in the bitmap */
376                 if (!(block->hdr.bitmap[offset / 8] &
377                       (1 << (offset % 8)))) {
378                         _debug("ENT[%zu.%u]: unused",
379                                blkoff / sizeof(union afs_xdr_dir_block), offset);
380                         if (offset >= curr)
381                                 ctx->pos = blkoff +
382                                         next * sizeof(union afs_xdr_dirent);
383                         continue;
384                 }
385
386                 /* got a valid entry */
387                 dire = &block->dirents[offset];
388                 nlen = strnlen(dire->u.name,
389                                sizeof(*block) -
390                                offset * sizeof(union afs_xdr_dirent));
391
392                 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
393                        blkoff / sizeof(union afs_xdr_dir_block), offset,
394                        (offset < curr ? "skip" : "fill"),
395                        nlen, dire->u.name);
396
397                 /* work out where the next possible entry is */
398                 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
399                         if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
400                                 _debug("ENT[%zu.%u]:"
401                                        " %u travelled beyond end dir block"
402                                        " (len %u/%zu)",
403                                        blkoff / sizeof(union afs_xdr_dir_block),
404                                        offset, next, tmp, nlen);
405                                 return afs_bad(dvnode, afs_file_error_dir_over_end);
406                         }
407                         if (!(block->hdr.bitmap[next / 8] &
408                               (1 << (next % 8)))) {
409                                 _debug("ENT[%zu.%u]:"
410                                        " %u unmarked extension (len %u/%zu)",
411                                        blkoff / sizeof(union afs_xdr_dir_block),
412                                        offset, next, tmp, nlen);
413                                 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
414                         }
415
416                         _debug("ENT[%zu.%u]: ext %u/%zu",
417                                blkoff / sizeof(union afs_xdr_dir_block),
418                                next, tmp, nlen);
419                         next++;
420                 }
421
422                 /* skip if starts before the current position */
423                 if (offset < curr)
424                         continue;
425
426                 /* found the next entry */
427                 if (!dir_emit(ctx, dire->u.name, nlen,
428                               ntohl(dire->u.vnode),
429                               (ctx->actor == afs_lookup_filldir ||
430                                ctx->actor == afs_lookup_one_filldir)?
431                               ntohl(dire->u.unique) : DT_UNKNOWN)) {
432                         _leave(" = 0 [full]");
433                         return 0;
434                 }
435
436                 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
437         }
438
439         _leave(" = 1 [more]");
440         return 1;
441 }
442
443 /*
444  * iterate through the data blob that lists the contents of an AFS directory
445  */
446 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
447                            struct key *key)
448 {
449         struct afs_vnode *dvnode = AFS_FS_I(dir);
450         struct afs_xdr_dir_page *dbuf;
451         union afs_xdr_dir_block *dblock;
452         struct afs_read *req;
453         struct page *page;
454         unsigned blkoff, limit;
455         int ret;
456
457         _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
458
459         if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
460                 _leave(" = -ESTALE");
461                 return -ESTALE;
462         }
463
464         req = afs_read_dir(dvnode, key);
465         if (IS_ERR(req))
466                 return PTR_ERR(req);
467
468         /* round the file position up to the next entry boundary */
469         ctx->pos += sizeof(union afs_xdr_dirent) - 1;
470         ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
471
472         /* walk through the blocks in sequence */
473         ret = 0;
474         while (ctx->pos < req->actual_len) {
475                 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
476
477                 /* Fetch the appropriate page from the directory and re-add it
478                  * to the LRU.
479                  */
480                 page = req->pages[blkoff / PAGE_SIZE];
481                 if (!page) {
482                         ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
483                         break;
484                 }
485                 mark_page_accessed(page);
486
487                 limit = blkoff & ~(PAGE_SIZE - 1);
488
489                 dbuf = kmap(page);
490
491                 /* deal with the individual blocks stashed on this page */
492                 do {
493                         dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
494                                                sizeof(union afs_xdr_dir_block)];
495                         ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
496                         if (ret != 1) {
497                                 kunmap(page);
498                                 goto out;
499                         }
500
501                         blkoff += sizeof(union afs_xdr_dir_block);
502
503                 } while (ctx->pos < dir->i_size && blkoff < limit);
504
505                 kunmap(page);
506                 ret = 0;
507         }
508
509 out:
510         up_read(&dvnode->validate_lock);
511         afs_put_read(req);
512         _leave(" = %d", ret);
513         return ret;
514 }
515
516 /*
517  * read an AFS directory
518  */
519 static int afs_readdir(struct file *file, struct dir_context *ctx)
520 {
521         return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
522 }
523
524 /*
525  * Search the directory for a single name
526  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
527  *   uniquifier through dtype
528  */
529 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530                                   int nlen, loff_t fpos, u64 ino, unsigned dtype)
531 {
532         struct afs_lookup_one_cookie *cookie =
533                 container_of(ctx, struct afs_lookup_one_cookie, ctx);
534
535         _enter("{%s,%u},%s,%u,,%llu,%u",
536                cookie->name.name, cookie->name.len, name, nlen,
537                (unsigned long long) ino, dtype);
538
539         /* insanity checks first */
540         BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
541         BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
542
543         if (cookie->name.len != nlen ||
544             memcmp(cookie->name.name, name, nlen) != 0) {
545                 _leave(" = 0 [no]");
546                 return 0;
547         }
548
549         cookie->fid.vnode = ino;
550         cookie->fid.unique = dtype;
551         cookie->found = 1;
552
553         _leave(" = -1 [found]");
554         return -1;
555 }
556
557 /*
558  * Do a lookup of a single name in a directory
559  * - just returns the FID the dentry name maps to if found
560  */
561 static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
562                              struct afs_fid *fid, struct key *key)
563 {
564         struct afs_super_info *as = dir->i_sb->s_fs_info;
565         struct afs_lookup_one_cookie cookie = {
566                 .ctx.actor = afs_lookup_one_filldir,
567                 .name = dentry->d_name,
568                 .fid.vid = as->volume->vid
569         };
570         int ret;
571
572         _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
573
574         /* search the directory */
575         ret = afs_dir_iterate(dir, &cookie.ctx, key);
576         if (ret < 0) {
577                 _leave(" = %d [iter]", ret);
578                 return ret;
579         }
580
581         ret = -ENOENT;
582         if (!cookie.found) {
583                 _leave(" = -ENOENT [not found]");
584                 return -ENOENT;
585         }
586
587         *fid = cookie.fid;
588         _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
589         return 0;
590 }
591
592 /*
593  * search the directory for a name
594  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
595  *   uniquifier through dtype
596  */
597 static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
598                               int nlen, loff_t fpos, u64 ino, unsigned dtype)
599 {
600         struct afs_lookup_cookie *cookie =
601                 container_of(ctx, struct afs_lookup_cookie, ctx);
602         int ret;
603
604         _enter("{%s,%u},%s,%u,,%llu,%u",
605                cookie->name.name, cookie->name.len, name, nlen,
606                (unsigned long long) ino, dtype);
607
608         /* insanity checks first */
609         BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
610         BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
611
612         if (cookie->found) {
613                 if (cookie->nr_fids < 50) {
614                         cookie->fids[cookie->nr_fids].vnode     = ino;
615                         cookie->fids[cookie->nr_fids].unique    = dtype;
616                         cookie->nr_fids++;
617                 }
618         } else if (cookie->name.len == nlen &&
619                    memcmp(cookie->name.name, name, nlen) == 0) {
620                 cookie->fids[0].vnode   = ino;
621                 cookie->fids[0].unique  = dtype;
622                 cookie->found = 1;
623                 if (cookie->one_only)
624                         return -1;
625         }
626
627         ret = cookie->nr_fids >= 50 ? -1 : 0;
628         _leave(" = %d", ret);
629         return ret;
630 }
631
632 /*
633  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
634  * files in one go and create inodes for them.  The inode of the file we were
635  * asked for is returned.
636  */
637 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
638                                    struct key *key)
639 {
640         struct afs_lookup_cookie *cookie;
641         struct afs_cb_interest *cbi = NULL;
642         struct afs_super_info *as = dir->i_sb->s_fs_info;
643         struct afs_iget_data data;
644         struct afs_fs_cursor fc;
645         struct afs_vnode *dvnode = AFS_FS_I(dir);
646         struct inode *inode = NULL;
647         int ret, i;
648
649         _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
650
651         cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
652         if (!cookie)
653                 return ERR_PTR(-ENOMEM);
654
655         cookie->ctx.actor = afs_lookup_filldir;
656         cookie->name = dentry->d_name;
657         cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
658
659         read_seqlock_excl(&dvnode->cb_lock);
660         if (dvnode->cb_interest &&
661             dvnode->cb_interest->server &&
662             test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
663                 cookie->one_only = true;
664         read_sequnlock_excl(&dvnode->cb_lock);
665
666         for (i = 0; i < 50; i++)
667                 cookie->fids[i].vid = as->volume->vid;
668
669         /* search the directory */
670         ret = afs_dir_iterate(dir, &cookie->ctx, key);
671         if (ret < 0) {
672                 inode = ERR_PTR(ret);
673                 goto out;
674         }
675
676         inode = ERR_PTR(-ENOENT);
677         if (!cookie->found)
678                 goto out;
679
680         /* Check to see if we already have an inode for the primary fid. */
681         data.volume = dvnode->volume;
682         data.fid = cookie->fids[0];
683         inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
684         if (inode)
685                 goto out;
686
687         /* Need space for examining all the selected files */
688         inode = ERR_PTR(-ENOMEM);
689         cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
690                                    GFP_KERNEL);
691         if (!cookie->statuses)
692                 goto out;
693
694         cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
695                                     GFP_KERNEL);
696         if (!cookie->callbacks)
697                 goto out_s;
698
699         /* Try FS.InlineBulkStatus first.  Abort codes for the individual
700          * lookups contained therein are stored in the reply without aborting
701          * the whole operation.
702          */
703         if (cookie->one_only)
704                 goto no_inline_bulk_status;
705
706         inode = ERR_PTR(-ERESTARTSYS);
707         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
708                 while (afs_select_fileserver(&fc)) {
709                         if (test_bit(AFS_SERVER_FL_NO_IBULK,
710                                       &fc.cbi->server->flags)) {
711                                 fc.ac.abort_code = RX_INVALID_OPERATION;
712                                 fc.ac.error = -ECONNABORTED;
713                                 break;
714                         }
715                         afs_fs_inline_bulk_status(&fc,
716                                                   afs_v2net(dvnode),
717                                                   cookie->fids,
718                                                   cookie->statuses,
719                                                   cookie->callbacks,
720                                                   cookie->nr_fids, NULL);
721                 }
722
723                 if (fc.ac.error == 0)
724                         cbi = afs_get_cb_interest(fc.cbi);
725                 if (fc.ac.abort_code == RX_INVALID_OPERATION)
726                         set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
727                 inode = ERR_PTR(afs_end_vnode_operation(&fc));
728         }
729
730         if (!IS_ERR(inode))
731                 goto success;
732         if (fc.ac.abort_code != RX_INVALID_OPERATION)
733                 goto out_c;
734
735 no_inline_bulk_status:
736         /* We could try FS.BulkStatus next, but this aborts the entire op if
737          * any of the lookups fails - so, for the moment, revert to
738          * FS.FetchStatus for just the primary fid.
739          */
740         cookie->nr_fids = 1;
741         inode = ERR_PTR(-ERESTARTSYS);
742         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
743                 while (afs_select_fileserver(&fc)) {
744                         afs_fs_fetch_status(&fc,
745                                             afs_v2net(dvnode),
746                                             cookie->fids,
747                                             cookie->statuses,
748                                             cookie->callbacks,
749                                             NULL);
750                 }
751
752                 if (fc.ac.error == 0)
753                         cbi = afs_get_cb_interest(fc.cbi);
754                 inode = ERR_PTR(afs_end_vnode_operation(&fc));
755         }
756
757         if (IS_ERR(inode))
758                 goto out_c;
759
760         for (i = 0; i < cookie->nr_fids; i++)
761                 cookie->statuses[i].abort_code = 0;
762
763 success:
764         /* Turn all the files into inodes and save the first one - which is the
765          * one we actually want.
766          */
767         if (cookie->statuses[0].abort_code != 0)
768                 inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
769
770         for (i = 0; i < cookie->nr_fids; i++) {
771                 struct inode *ti;
772
773                 if (cookie->statuses[i].abort_code != 0)
774                         continue;
775
776                 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
777                               &cookie->statuses[i],
778                               &cookie->callbacks[i],
779                               cbi);
780                 if (i == 0) {
781                         inode = ti;
782                 } else {
783                         if (!IS_ERR(ti))
784                                 iput(ti);
785                 }
786         }
787
788 out_c:
789         afs_put_cb_interest(afs_v2net(dvnode), cbi);
790         kfree(cookie->callbacks);
791 out_s:
792         kfree(cookie->statuses);
793 out:
794         kfree(cookie);
795         return inode;
796 }
797
798 /*
799  * Look up an entry in a directory with @sys substitution.
800  */
801 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
802                                        struct key *key)
803 {
804         struct afs_sysnames *subs;
805         struct afs_net *net = afs_i2net(dir);
806         struct dentry *ret;
807         char *buf, *p, *name;
808         int len, i;
809
810         _enter("");
811
812         ret = ERR_PTR(-ENOMEM);
813         p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
814         if (!buf)
815                 goto out_p;
816         if (dentry->d_name.len > 4) {
817                 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
818                 p += dentry->d_name.len - 4;
819         }
820
821         /* There is an ordered list of substitutes that we have to try. */
822         read_lock(&net->sysnames_lock);
823         subs = net->sysnames;
824         refcount_inc(&subs->usage);
825         read_unlock(&net->sysnames_lock);
826
827         for (i = 0; i < subs->nr; i++) {
828                 name = subs->subs[i];
829                 len = dentry->d_name.len - 4 + strlen(name);
830                 if (len >= AFSNAMEMAX) {
831                         ret = ERR_PTR(-ENAMETOOLONG);
832                         goto out_s;
833                 }
834
835                 strcpy(p, name);
836                 ret = lookup_one_len(buf, dentry->d_parent, len);
837                 if (IS_ERR(ret) || d_is_positive(ret))
838                         goto out_s;
839                 dput(ret);
840         }
841
842         /* We don't want to d_add() the @sys dentry here as we don't want to
843          * the cached dentry to hide changes to the sysnames list.
844          */
845         ret = NULL;
846 out_s:
847         afs_put_sysnames(subs);
848         kfree(buf);
849 out_p:
850         key_put(key);
851         return ret;
852 }
853
854 /*
855  * look up an entry in a directory
856  */
857 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
858                                  unsigned int flags)
859 {
860         struct afs_vnode *dvnode = AFS_FS_I(dir);
861         struct inode *inode;
862         struct dentry *d;
863         struct key *key;
864         int ret;
865
866         _enter("{%llx:%llu},%p{%pd},",
867                dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
868
869         ASSERTCMP(d_inode(dentry), ==, NULL);
870
871         if (dentry->d_name.len >= AFSNAMEMAX) {
872                 _leave(" = -ENAMETOOLONG");
873                 return ERR_PTR(-ENAMETOOLONG);
874         }
875
876         if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
877                 _leave(" = -ESTALE");
878                 return ERR_PTR(-ESTALE);
879         }
880
881         key = afs_request_key(dvnode->volume->cell);
882         if (IS_ERR(key)) {
883                 _leave(" = %ld [key]", PTR_ERR(key));
884                 return ERR_CAST(key);
885         }
886
887         ret = afs_validate(dvnode, key);
888         if (ret < 0) {
889                 key_put(key);
890                 _leave(" = %d [val]", ret);
891                 return ERR_PTR(ret);
892         }
893
894         if (dentry->d_name.len >= 4 &&
895             dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
896             dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
897             dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
898             dentry->d_name.name[dentry->d_name.len - 1] == 's')
899                 return afs_lookup_atsys(dir, dentry, key);
900
901         afs_stat_v(dvnode, n_lookup);
902         inode = afs_do_lookup(dir, dentry, key);
903         key_put(key);
904         if (inode == ERR_PTR(-ENOENT)) {
905                 inode = afs_try_auto_mntpt(dentry, dir);
906         } else {
907                 dentry->d_fsdata =
908                         (void *)(unsigned long)dvnode->status.data_version;
909         }
910         d = d_splice_alias(inode, dentry);
911         if (!IS_ERR_OR_NULL(d))
912                 d->d_fsdata = dentry->d_fsdata;
913         return d;
914 }
915
916 /*
917  * check that a dentry lookup hit has found a valid entry
918  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
919  *   inode
920  */
921 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
922 {
923         struct afs_vnode *vnode, *dir;
924         struct afs_fid uninitialized_var(fid);
925         struct dentry *parent;
926         struct inode *inode;
927         struct key *key;
928         long dir_version, de_version;
929         int ret;
930
931         if (flags & LOOKUP_RCU)
932                 return -ECHILD;
933
934         if (d_really_is_positive(dentry)) {
935                 vnode = AFS_FS_I(d_inode(dentry));
936                 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
937                        vnode->fid.vid, vnode->fid.vnode, dentry,
938                        vnode->flags);
939         } else {
940                 _enter("{neg n=%pd}", dentry);
941         }
942
943         key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
944         if (IS_ERR(key))
945                 key = NULL;
946
947         if (d_really_is_positive(dentry)) {
948                 inode = d_inode(dentry);
949                 if (inode) {
950                         vnode = AFS_FS_I(inode);
951                         afs_validate(vnode, key);
952                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
953                                 goto out_bad;
954                 }
955         }
956
957         /* lock down the parent dentry so we can peer at it */
958         parent = dget_parent(dentry);
959         dir = AFS_FS_I(d_inode(parent));
960
961         /* validate the parent directory */
962         afs_validate(dir, key);
963
964         if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
965                 _debug("%pd: parent dir deleted", dentry);
966                 goto out_bad_parent;
967         }
968
969         /* We only need to invalidate a dentry if the server's copy changed
970          * behind our back.  If we made the change, it's no problem.  Note that
971          * on a 32-bit system, we only have 32 bits in the dentry to store the
972          * version.
973          */
974         dir_version = (long)dir->status.data_version;
975         de_version = (long)dentry->d_fsdata;
976         if (de_version == dir_version)
977                 goto out_valid;
978
979         dir_version = (long)dir->invalid_before;
980         if (de_version - dir_version >= 0)
981                 goto out_valid;
982
983         _debug("dir modified");
984         afs_stat_v(dir, n_reval);
985
986         /* search the directory for this vnode */
987         ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
988         switch (ret) {
989         case 0:
990                 /* the filename maps to something */
991                 if (d_really_is_negative(dentry))
992                         goto out_bad_parent;
993                 inode = d_inode(dentry);
994                 if (is_bad_inode(inode)) {
995                         printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
996                                dentry);
997                         goto out_bad_parent;
998                 }
999
1000                 vnode = AFS_FS_I(inode);
1001
1002                 /* if the vnode ID has changed, then the dirent points to a
1003                  * different file */
1004                 if (fid.vnode != vnode->fid.vnode) {
1005                         _debug("%pd: dirent changed [%llu != %llu]",
1006                                dentry, fid.vnode,
1007                                vnode->fid.vnode);
1008                         goto not_found;
1009                 }
1010
1011                 /* if the vnode ID uniqifier has changed, then the file has
1012                  * been deleted and replaced, and the original vnode ID has
1013                  * been reused */
1014                 if (fid.unique != vnode->fid.unique) {
1015                         _debug("%pd: file deleted (uq %u -> %u I:%u)",
1016                                dentry, fid.unique,
1017                                vnode->fid.unique,
1018                                vnode->vfs_inode.i_generation);
1019                         write_seqlock(&vnode->cb_lock);
1020                         set_bit(AFS_VNODE_DELETED, &vnode->flags);
1021                         write_sequnlock(&vnode->cb_lock);
1022                         goto not_found;
1023                 }
1024                 goto out_valid;
1025
1026         case -ENOENT:
1027                 /* the filename is unknown */
1028                 _debug("%pd: dirent not found", dentry);
1029                 if (d_really_is_positive(dentry))
1030                         goto not_found;
1031                 goto out_valid;
1032
1033         default:
1034                 _debug("failed to iterate dir %pd: %d",
1035                        parent, ret);
1036                 goto out_bad_parent;
1037         }
1038
1039 out_valid:
1040         dentry->d_fsdata = (void *)dir_version;
1041         dput(parent);
1042         key_put(key);
1043         _leave(" = 1 [valid]");
1044         return 1;
1045
1046         /* the dirent, if it exists, now points to a different vnode */
1047 not_found:
1048         spin_lock(&dentry->d_lock);
1049         dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1050         spin_unlock(&dentry->d_lock);
1051
1052 out_bad_parent:
1053         _debug("dropping dentry %pd2", dentry);
1054         dput(parent);
1055 out_bad:
1056         key_put(key);
1057
1058         _leave(" = 0 [bad]");
1059         return 0;
1060 }
1061
1062 /*
1063  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1064  * sleep)
1065  * - called from dput() when d_count is going to 0.
1066  * - return 1 to request dentry be unhashed, 0 otherwise
1067  */
1068 static int afs_d_delete(const struct dentry *dentry)
1069 {
1070         _enter("%pd", dentry);
1071
1072         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1073                 goto zap;
1074
1075         if (d_really_is_positive(dentry) &&
1076             (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
1077              test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1078                 goto zap;
1079
1080         _leave(" = 0 [keep]");
1081         return 0;
1082
1083 zap:
1084         _leave(" = 1 [zap]");
1085         return 1;
1086 }
1087
1088 /*
1089  * Clean up sillyrename files on dentry removal.
1090  */
1091 static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1092 {
1093         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1094                 afs_silly_iput(dentry, inode);
1095         iput(inode);
1096 }
1097
1098 /*
1099  * handle dentry release
1100  */
1101 void afs_d_release(struct dentry *dentry)
1102 {
1103         _enter("%pd", dentry);
1104 }
1105
1106 /*
1107  * Create a new inode for create/mkdir/symlink
1108  */
1109 static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1110                                 struct dentry *new_dentry,
1111                                 struct afs_fid *newfid,
1112                                 struct afs_file_status *newstatus,
1113                                 struct afs_callback *newcb)
1114 {
1115         struct afs_vnode *vnode;
1116         struct inode *inode;
1117
1118         if (fc->ac.error < 0)
1119                 return;
1120
1121         inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1122                          newfid, newstatus, newcb, fc->cbi);
1123         if (IS_ERR(inode)) {
1124                 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1125                  * the new directory on the server.
1126                  */
1127                 fc->ac.error = PTR_ERR(inode);
1128                 return;
1129         }
1130
1131         vnode = AFS_FS_I(inode);
1132         set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1133         afs_vnode_commit_status(fc, vnode, 0);
1134         d_instantiate(new_dentry, inode);
1135 }
1136
1137 /*
1138  * create a directory on an AFS filesystem
1139  */
1140 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1141 {
1142         struct afs_file_status newstatus;
1143         struct afs_fs_cursor fc;
1144         struct afs_callback newcb;
1145         struct afs_vnode *dvnode = AFS_FS_I(dir);
1146         struct afs_fid newfid;
1147         struct key *key;
1148         u64 data_version = dvnode->status.data_version;
1149         int ret;
1150
1151         mode |= S_IFDIR;
1152
1153         _enter("{%llx:%llu},{%pd},%ho",
1154                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1155
1156         key = afs_request_key(dvnode->volume->cell);
1157         if (IS_ERR(key)) {
1158                 ret = PTR_ERR(key);
1159                 goto error;
1160         }
1161
1162         ret = -ERESTARTSYS;
1163         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1164                 while (afs_select_fileserver(&fc)) {
1165                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1166                         afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
1167                                       &newfid, &newstatus, &newcb);
1168                 }
1169
1170                 afs_check_for_remote_deletion(&fc, fc.vnode);
1171                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1172                 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1173                 ret = afs_end_vnode_operation(&fc);
1174                 if (ret < 0)
1175                         goto error_key;
1176         } else {
1177                 goto error_key;
1178         }
1179
1180         if (ret == 0 &&
1181             test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1182                 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1183                                  afs_edit_dir_for_create);
1184
1185         key_put(key);
1186         _leave(" = 0");
1187         return 0;
1188
1189 error_key:
1190         key_put(key);
1191 error:
1192         d_drop(dentry);
1193         _leave(" = %d", ret);
1194         return ret;
1195 }
1196
1197 /*
1198  * Remove a subdir from a directory.
1199  */
1200 static void afs_dir_remove_subdir(struct dentry *dentry)
1201 {
1202         if (d_really_is_positive(dentry)) {
1203                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1204
1205                 clear_nlink(&vnode->vfs_inode);
1206                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1207                 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1208                 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1209         }
1210 }
1211
1212 /*
1213  * remove a directory from an AFS filesystem
1214  */
1215 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1216 {
1217         struct afs_fs_cursor fc;
1218         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1219         struct key *key;
1220         u64 data_version = dvnode->status.data_version;
1221         int ret;
1222
1223         _enter("{%llx:%llu},{%pd}",
1224                dvnode->fid.vid, dvnode->fid.vnode, dentry);
1225
1226         key = afs_request_key(dvnode->volume->cell);
1227         if (IS_ERR(key)) {
1228                 ret = PTR_ERR(key);
1229                 goto error;
1230         }
1231
1232         /* Try to make sure we have a callback promise on the victim. */
1233         if (d_really_is_positive(dentry)) {
1234                 vnode = AFS_FS_I(d_inode(dentry));
1235                 ret = afs_validate(vnode, key);
1236                 if (ret < 0)
1237                         goto error_key;
1238         }
1239
1240         if (vnode) {
1241                 ret = down_write_killable(&vnode->rmdir_lock);
1242                 if (ret < 0)
1243                         goto error_key;
1244         }
1245
1246         ret = -ERESTARTSYS;
1247         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1248                 while (afs_select_fileserver(&fc)) {
1249                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1250                         afs_fs_remove(&fc, vnode, dentry->d_name.name, true,
1251                                       data_version);
1252                 }
1253
1254                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1255                 ret = afs_end_vnode_operation(&fc);
1256                 if (ret == 0) {
1257                         afs_dir_remove_subdir(dentry);
1258                         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1259                                 afs_edit_dir_remove(dvnode, &dentry->d_name,
1260                                                     afs_edit_dir_for_rmdir);
1261                 }
1262         }
1263
1264         if (vnode)
1265                 up_write(&vnode->rmdir_lock);
1266 error_key:
1267         key_put(key);
1268 error:
1269         return ret;
1270 }
1271
1272 /*
1273  * Remove a link to a file or symlink from a directory.
1274  *
1275  * If the file was not deleted due to excess hard links, the fileserver will
1276  * break the callback promise on the file - if it had one - before it returns
1277  * to us, and if it was deleted, it won't
1278  *
1279  * However, if we didn't have a callback promise outstanding, or it was
1280  * outstanding on a different server, then it won't break it either...
1281  */
1282 int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1283                         unsigned long d_version_before,
1284                         unsigned long d_version_after)
1285 {
1286         bool dir_valid;
1287         int ret = 0;
1288
1289         /* There were no intervening changes on the server if the version
1290          * number we got back was incremented by exactly 1.
1291          */
1292         dir_valid = (d_version_after == d_version_before + 1);
1293
1294         if (d_really_is_positive(dentry)) {
1295                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1296
1297                 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1298                         /* Already done */
1299                 } else if (dir_valid) {
1300                         drop_nlink(&vnode->vfs_inode);
1301                         if (vnode->vfs_inode.i_nlink == 0) {
1302                                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1303                                 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1304                         }
1305                         ret = 0;
1306                 } else {
1307                         clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1308
1309                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1310                                 kdebug("AFS_VNODE_DELETED");
1311
1312                         ret = afs_validate(vnode, key);
1313                         if (ret == -ESTALE)
1314                                 ret = 0;
1315                 }
1316                 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1317         }
1318
1319         return ret;
1320 }
1321
1322 /*
1323  * Remove a file or symlink from an AFS filesystem.
1324  */
1325 static int afs_unlink(struct inode *dir, struct dentry *dentry)
1326 {
1327         struct afs_fs_cursor fc;
1328         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1329         struct key *key;
1330         unsigned long d_version = (unsigned long)dentry->d_fsdata;
1331         bool need_rehash = false;
1332         u64 data_version = dvnode->status.data_version;
1333         int ret;
1334
1335         _enter("{%llx:%llu},{%pd}",
1336                dvnode->fid.vid, dvnode->fid.vnode, dentry);
1337
1338         if (dentry->d_name.len >= AFSNAMEMAX)
1339                 return -ENAMETOOLONG;
1340
1341         key = afs_request_key(dvnode->volume->cell);
1342         if (IS_ERR(key)) {
1343                 ret = PTR_ERR(key);
1344                 goto error;
1345         }
1346
1347         /* Try to make sure we have a callback promise on the victim. */
1348         if (d_really_is_positive(dentry)) {
1349                 vnode = AFS_FS_I(d_inode(dentry));
1350                 ret = afs_validate(vnode, key);
1351                 if (ret < 0)
1352                         goto error_key;
1353         }
1354
1355         spin_lock(&dentry->d_lock);
1356         if (vnode && d_count(dentry) > 1) {
1357                 spin_unlock(&dentry->d_lock);
1358                 /* Start asynchronous writeout of the inode */
1359                 write_inode_now(d_inode(dentry), 0);
1360                 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1361                 goto error_key;
1362         }
1363         if (!d_unhashed(dentry)) {
1364                 /* Prevent a race with RCU lookup. */
1365                 __d_drop(dentry);
1366                 need_rehash = true;
1367         }
1368         spin_unlock(&dentry->d_lock);
1369
1370         ret = -ERESTARTSYS;
1371         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1372                 while (afs_select_fileserver(&fc)) {
1373                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1374
1375                         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1376                             !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1377                                 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1378                                                     data_version);
1379                                 if (fc.ac.error != -ECONNABORTED ||
1380                                     fc.ac.abort_code != RXGEN_OPCODE)
1381                                         continue;
1382                                 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1383                         }
1384
1385                         afs_fs_remove(&fc, vnode, dentry->d_name.name, false,
1386                                       data_version);
1387                 }
1388
1389                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1390                 ret = afs_end_vnode_operation(&fc);
1391                 if (ret == 0)
1392                         ret = afs_dir_remove_link(
1393                                 dentry, key, d_version,
1394                                 (unsigned long)dvnode->status.data_version);
1395                 if (ret == 0 &&
1396                     test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1397                         afs_edit_dir_remove(dvnode, &dentry->d_name,
1398                                             afs_edit_dir_for_unlink);
1399         }
1400
1401         if (need_rehash && ret < 0 && ret != -ENOENT)
1402                 d_rehash(dentry);
1403
1404 error_key:
1405         key_put(key);
1406 error:
1407         _leave(" = %d", ret);
1408         return ret;
1409 }
1410
1411 /*
1412  * create a regular file on an AFS filesystem
1413  */
1414 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1415                       bool excl)
1416 {
1417         struct afs_fs_cursor fc;
1418         struct afs_file_status newstatus;
1419         struct afs_callback newcb;
1420         struct afs_vnode *dvnode = AFS_FS_I(dir);
1421         struct afs_fid newfid;
1422         struct key *key;
1423         u64 data_version = dvnode->status.data_version;
1424         int ret;
1425
1426         mode |= S_IFREG;
1427
1428         _enter("{%llx:%llu},{%pd},%ho,",
1429                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1430
1431         ret = -ENAMETOOLONG;
1432         if (dentry->d_name.len >= AFSNAMEMAX)
1433                 goto error;
1434
1435         key = afs_request_key(dvnode->volume->cell);
1436         if (IS_ERR(key)) {
1437                 ret = PTR_ERR(key);
1438                 goto error;
1439         }
1440
1441         ret = -ERESTARTSYS;
1442         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1443                 while (afs_select_fileserver(&fc)) {
1444                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1445                         afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
1446                                       &newfid, &newstatus, &newcb);
1447                 }
1448
1449                 afs_check_for_remote_deletion(&fc, fc.vnode);
1450                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1451                 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1452                 ret = afs_end_vnode_operation(&fc);
1453                 if (ret < 0)
1454                         goto error_key;
1455         } else {
1456                 goto error_key;
1457         }
1458
1459         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1460                 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1461                                  afs_edit_dir_for_create);
1462
1463         key_put(key);
1464         _leave(" = 0");
1465         return 0;
1466
1467 error_key:
1468         key_put(key);
1469 error:
1470         d_drop(dentry);
1471         _leave(" = %d", ret);
1472         return ret;
1473 }
1474
1475 /*
1476  * create a hard link between files in an AFS filesystem
1477  */
1478 static int afs_link(struct dentry *from, struct inode *dir,
1479                     struct dentry *dentry)
1480 {
1481         struct afs_fs_cursor fc;
1482         struct afs_vnode *dvnode, *vnode;
1483         struct key *key;
1484         u64 data_version;
1485         int ret;
1486
1487         vnode = AFS_FS_I(d_inode(from));
1488         dvnode = AFS_FS_I(dir);
1489         data_version = dvnode->status.data_version;
1490
1491         _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1492                vnode->fid.vid, vnode->fid.vnode,
1493                dvnode->fid.vid, dvnode->fid.vnode,
1494                dentry);
1495
1496         ret = -ENAMETOOLONG;
1497         if (dentry->d_name.len >= AFSNAMEMAX)
1498                 goto error;
1499
1500         key = afs_request_key(dvnode->volume->cell);
1501         if (IS_ERR(key)) {
1502                 ret = PTR_ERR(key);
1503                 goto error;
1504         }
1505
1506         ret = -ERESTARTSYS;
1507         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1508                 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1509                         afs_end_vnode_operation(&fc);
1510                         goto error_key;
1511                 }
1512
1513                 while (afs_select_fileserver(&fc)) {
1514                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1515                         fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1516                         afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
1517                 }
1518
1519                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1520                 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1521                 ihold(&vnode->vfs_inode);
1522                 d_instantiate(dentry, &vnode->vfs_inode);
1523
1524                 mutex_unlock(&vnode->io_lock);
1525                 ret = afs_end_vnode_operation(&fc);
1526                 if (ret < 0)
1527                         goto error_key;
1528         } else {
1529                 goto error_key;
1530         }
1531
1532         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1533                 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1534                                  afs_edit_dir_for_link);
1535
1536         key_put(key);
1537         _leave(" = 0");
1538         return 0;
1539
1540 error_key:
1541         key_put(key);
1542 error:
1543         d_drop(dentry);
1544         _leave(" = %d", ret);
1545         return ret;
1546 }
1547
1548 /*
1549  * create a symlink in an AFS filesystem
1550  */
1551 static int afs_symlink(struct inode *dir, struct dentry *dentry,
1552                        const char *content)
1553 {
1554         struct afs_fs_cursor fc;
1555         struct afs_file_status newstatus;
1556         struct afs_vnode *dvnode = AFS_FS_I(dir);
1557         struct afs_fid newfid;
1558         struct key *key;
1559         u64 data_version = dvnode->status.data_version;
1560         int ret;
1561
1562         _enter("{%llx:%llu},{%pd},%s",
1563                dvnode->fid.vid, dvnode->fid.vnode, dentry,
1564                content);
1565
1566         ret = -ENAMETOOLONG;
1567         if (dentry->d_name.len >= AFSNAMEMAX)
1568                 goto error;
1569
1570         ret = -EINVAL;
1571         if (strlen(content) >= AFSPATHMAX)
1572                 goto error;
1573
1574         key = afs_request_key(dvnode->volume->cell);
1575         if (IS_ERR(key)) {
1576                 ret = PTR_ERR(key);
1577                 goto error;
1578         }
1579
1580         ret = -ERESTARTSYS;
1581         if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1582                 while (afs_select_fileserver(&fc)) {
1583                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1584                         afs_fs_symlink(&fc, dentry->d_name.name,
1585                                        content, data_version,
1586                                        &newfid, &newstatus);
1587                 }
1588
1589                 afs_check_for_remote_deletion(&fc, fc.vnode);
1590                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1591                 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1592                 ret = afs_end_vnode_operation(&fc);
1593                 if (ret < 0)
1594                         goto error_key;
1595         } else {
1596                 goto error_key;
1597         }
1598
1599         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1600                 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1601                                  afs_edit_dir_for_symlink);
1602
1603         key_put(key);
1604         _leave(" = 0");
1605         return 0;
1606
1607 error_key:
1608         key_put(key);
1609 error:
1610         d_drop(dentry);
1611         _leave(" = %d", ret);
1612         return ret;
1613 }
1614
1615 /*
1616  * rename a file in an AFS filesystem and/or move it between directories
1617  */
1618 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1619                       struct inode *new_dir, struct dentry *new_dentry,
1620                       unsigned int flags)
1621 {
1622         struct afs_fs_cursor fc;
1623         struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1624         struct dentry *tmp = NULL, *rehash = NULL;
1625         struct inode *new_inode;
1626         struct key *key;
1627         u64 orig_data_version, new_data_version;
1628         bool new_negative = d_is_negative(new_dentry);
1629         int ret;
1630
1631         if (flags)
1632                 return -EINVAL;
1633
1634         /* Don't allow silly-rename files be moved around. */
1635         if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1636                 return -EINVAL;
1637
1638         vnode = AFS_FS_I(d_inode(old_dentry));
1639         orig_dvnode = AFS_FS_I(old_dir);
1640         new_dvnode = AFS_FS_I(new_dir);
1641         orig_data_version = orig_dvnode->status.data_version;
1642         new_data_version = new_dvnode->status.data_version;
1643
1644         _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1645                orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1646                vnode->fid.vid, vnode->fid.vnode,
1647                new_dvnode->fid.vid, new_dvnode->fid.vnode,
1648                new_dentry);
1649
1650         key = afs_request_key(orig_dvnode->volume->cell);
1651         if (IS_ERR(key)) {
1652                 ret = PTR_ERR(key);
1653                 goto error;
1654         }
1655
1656         /* For non-directories, check whether the target is busy and if so,
1657          * make a copy of the dentry and then do a silly-rename.  If the
1658          * silly-rename succeeds, the copied dentry is hashed and becomes the
1659          * new target.
1660          */
1661         if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1662                 /* To prevent any new references to the target during the
1663                  * rename, we unhash the dentry in advance.
1664                  */
1665                 if (!d_unhashed(new_dentry)) {
1666                         d_drop(new_dentry);
1667                         rehash = new_dentry;
1668                 }
1669
1670                 if (d_count(new_dentry) > 2) {
1671                         /* copy the target dentry's name */
1672                         ret = -ENOMEM;
1673                         tmp = d_alloc(new_dentry->d_parent,
1674                                       &new_dentry->d_name);
1675                         if (!tmp)
1676                                 goto error_rehash;
1677
1678                         ret = afs_sillyrename(new_dvnode,
1679                                               AFS_FS_I(d_inode(new_dentry)),
1680                                               new_dentry, key);
1681                         if (ret)
1682                                 goto error_rehash;
1683
1684                         new_dentry = tmp;
1685                         rehash = NULL;
1686                         new_negative = true;
1687                         orig_data_version = orig_dvnode->status.data_version;
1688                         new_data_version = new_dvnode->status.data_version;
1689                 }
1690         }
1691
1692         ret = -ERESTARTSYS;
1693         if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1694                 if (orig_dvnode != new_dvnode) {
1695                         if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1696                                 afs_end_vnode_operation(&fc);
1697                                 goto error_rehash;
1698                         }
1699                 }
1700                 while (afs_select_fileserver(&fc)) {
1701                         fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1702                         fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1703                         afs_fs_rename(&fc, old_dentry->d_name.name,
1704                                       new_dvnode, new_dentry->d_name.name,
1705                                       orig_data_version, new_data_version);
1706                 }
1707
1708                 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1709                 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1710                 if (orig_dvnode != new_dvnode)
1711                         mutex_unlock(&new_dvnode->io_lock);
1712                 ret = afs_end_vnode_operation(&fc);
1713                 if (ret < 0)
1714                         goto error_rehash;
1715         }
1716
1717         if (ret == 0) {
1718                 if (rehash)
1719                         d_rehash(rehash);
1720                 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1721                     afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1722                                         afs_edit_dir_for_rename_0);
1723
1724                 if (!new_negative &&
1725                     test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1726                         afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1727                                             afs_edit_dir_for_rename_1);
1728
1729                 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1730                         afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1731                                          &vnode->fid, afs_edit_dir_for_rename_2);
1732
1733                 new_inode = d_inode(new_dentry);
1734                 if (new_inode) {
1735                         spin_lock(&new_inode->i_lock);
1736                         if (new_inode->i_nlink > 0)
1737                                 drop_nlink(new_inode);
1738                         spin_unlock(&new_inode->i_lock);
1739                 }
1740                 d_move(old_dentry, new_dentry);
1741                 goto error_tmp;
1742         }
1743
1744 error_rehash:
1745         if (rehash)
1746                 d_rehash(rehash);
1747 error_tmp:
1748         if (tmp)
1749                 dput(tmp);
1750         key_put(key);
1751 error:
1752         _leave(" = %d", ret);
1753         return ret;
1754 }
1755
1756 /*
1757  * Release a directory page and clean up its private state if it's not busy
1758  * - return true if the page can now be released, false if not
1759  */
1760 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1761 {
1762         struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1763
1764         _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1765
1766         set_page_private(page, 0);
1767         ClearPagePrivate(page);
1768
1769         /* The directory will need reloading. */
1770         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1771                 afs_stat_v(dvnode, n_relpg);
1772         return 1;
1773 }
1774
1775 /*
1776  * invalidate part or all of a page
1777  * - release a page and clean up its private data if offset is 0 (indicating
1778  *   the entire page)
1779  */
1780 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1781                                    unsigned int length)
1782 {
1783         struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1784
1785         _enter("{%lu},%u,%u", page->index, offset, length);
1786
1787         BUG_ON(!PageLocked(page));
1788
1789         /* The directory will need reloading. */
1790         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1791                 afs_stat_v(dvnode, n_inval);
1792
1793         /* we clean up only if the entire page is being invalidated */
1794         if (offset == 0 && length == PAGE_SIZE) {
1795                 set_page_private(page, 0);
1796                 ClearPagePrivate(page);
1797         }
1798 }