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