d1b3736a3bbded2a93281ca4f47e5e6fd9fd42be
[sfrench/cifs-2.6.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 "afs_fs.h"
22 #include "xdr_fs.h"
23
24 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
25                                  unsigned int flags);
26 static int afs_dir_open(struct inode *inode, struct file *file);
27 static int afs_readdir(struct file *file, struct dir_context *ctx);
28 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
29 static int afs_d_delete(const struct dentry *dentry);
30 static void afs_d_iput(struct dentry *dentry, struct inode *inode);
31 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
32                                   loff_t fpos, u64 ino, unsigned dtype);
33 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
34                               loff_t fpos, u64 ino, unsigned dtype);
35 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
36                       bool excl);
37 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
38 static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39 static int afs_unlink(struct inode *dir, struct dentry *dentry);
40 static int afs_link(struct dentry *from, struct inode *dir,
41                     struct dentry *dentry);
42 static int afs_symlink(struct inode *dir, struct dentry *dentry,
43                        const char *content);
44 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
45                       struct inode *new_dir, struct dentry *new_dentry,
46                       unsigned int flags);
47 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
48 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
49                                    unsigned int length);
50
51 static int afs_dir_set_page_dirty(struct page *page)
52 {
53         BUG(); /* This should never happen. */
54 }
55
56 const struct file_operations afs_dir_file_operations = {
57         .open           = afs_dir_open,
58         .release        = afs_release,
59         .iterate_shared = afs_readdir,
60         .lock           = afs_lock,
61         .llseek         = generic_file_llseek,
62 };
63
64 const struct inode_operations afs_dir_inode_operations = {
65         .create         = afs_create,
66         .lookup         = afs_lookup,
67         .link           = afs_link,
68         .unlink         = afs_unlink,
69         .symlink        = afs_symlink,
70         .mkdir          = afs_mkdir,
71         .rmdir          = afs_rmdir,
72         .rename         = afs_rename,
73         .permission     = afs_permission,
74         .getattr        = afs_getattr,
75         .setattr        = afs_setattr,
76         .listxattr      = afs_listxattr,
77 };
78
79 const struct address_space_operations afs_dir_aops = {
80         .set_page_dirty = afs_dir_set_page_dirty,
81         .releasepage    = afs_dir_releasepage,
82         .invalidatepage = afs_dir_invalidatepage,
83 };
84
85 const struct dentry_operations afs_fs_dentry_operations = {
86         .d_revalidate   = afs_d_revalidate,
87         .d_delete       = afs_d_delete,
88         .d_release      = afs_d_release,
89         .d_automount    = afs_d_automount,
90         .d_iput         = afs_d_iput,
91 };
92
93 struct afs_lookup_one_cookie {
94         struct dir_context      ctx;
95         struct qstr             name;
96         bool                    found;
97         struct afs_fid          fid;
98 };
99
100 struct afs_lookup_cookie {
101         struct dir_context      ctx;
102         struct qstr             name;
103         bool                    found;
104         bool                    one_only;
105         unsigned short          nr_fids;
106         struct afs_status_cb    *statuses;
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 *dcbi, *cbi = NULL;
642         struct afs_super_info *as = dir->i_sb->s_fs_info;
643         struct afs_status_cb *scb;
644         struct afs_iget_data data;
645         struct afs_fs_cursor fc;
646         struct afs_server *server;
647         struct afs_vnode *dvnode = AFS_FS_I(dir);
648         struct inode *inode = NULL;
649         int ret, i;
650
651         _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
652
653         cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
654         if (!cookie)
655                 return ERR_PTR(-ENOMEM);
656
657         cookie->ctx.actor = afs_lookup_filldir;
658         cookie->name = dentry->d_name;
659         cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
660
661         read_seqlock_excl(&dvnode->cb_lock);
662         dcbi = rcu_dereference_protected(dvnode->cb_interest,
663                                          lockdep_is_held(&dvnode->cb_lock.lock));
664         if (dcbi) {
665                 server = dcbi->server;
666                 if (server &&
667                     test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
668                         cookie->one_only = true;
669         }
670         read_sequnlock_excl(&dvnode->cb_lock);
671
672         for (i = 0; i < 50; i++)
673                 cookie->fids[i].vid = as->volume->vid;
674
675         /* search the directory */
676         ret = afs_dir_iterate(dir, &cookie->ctx, key);
677         if (ret < 0) {
678                 inode = ERR_PTR(ret);
679                 goto out;
680         }
681
682         inode = ERR_PTR(-ENOENT);
683         if (!cookie->found)
684                 goto out;
685
686         /* Check to see if we already have an inode for the primary fid. */
687         data.volume = dvnode->volume;
688         data.fid = cookie->fids[0];
689         inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
690         if (inode)
691                 goto out;
692
693         /* Need space for examining all the selected files */
694         inode = ERR_PTR(-ENOMEM);
695         cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
696                                     GFP_KERNEL);
697         if (!cookie->statuses)
698                 goto out;
699
700         /* Try FS.InlineBulkStatus first.  Abort codes for the individual
701          * lookups contained therein are stored in the reply without aborting
702          * the whole operation.
703          */
704         if (cookie->one_only)
705                 goto no_inline_bulk_status;
706
707         inode = ERR_PTR(-ERESTARTSYS);
708         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
709                 while (afs_select_fileserver(&fc)) {
710                         if (test_bit(AFS_SERVER_FL_NO_IBULK,
711                                       &fc.cbi->server->flags)) {
712                                 fc.ac.abort_code = RX_INVALID_OPERATION;
713                                 fc.ac.error = -ECONNABORTED;
714                                 break;
715                         }
716                         afs_fs_inline_bulk_status(&fc,
717                                                   afs_v2net(dvnode),
718                                                   cookie->fids,
719                                                   cookie->statuses,
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, true)) {
743                 while (afs_select_fileserver(&fc)) {
744                         scb = &cookie->statuses[0];
745                         afs_fs_fetch_status(&fc,
746                                             afs_v2net(dvnode),
747                                             cookie->fids,
748                                             scb,
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].status.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         scb = &cookie->statuses[0];
768         if (scb->status.abort_code != 0)
769                 inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
770
771         for (i = 0; i < cookie->nr_fids; i++) {
772                 struct afs_status_cb *scb = &cookie->statuses[i];
773                 struct inode *ti;
774
775                 if (scb->status.abort_code != 0)
776                         continue;
777
778                 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
779                               scb, cbi, dvnode);
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         kvfree(cookie->statuses);
791 out:
792         kfree(cookie);
793         return inode;
794 }
795
796 /*
797  * Look up an entry in a directory with @sys substitution.
798  */
799 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
800                                        struct key *key)
801 {
802         struct afs_sysnames *subs;
803         struct afs_net *net = afs_i2net(dir);
804         struct dentry *ret;
805         char *buf, *p, *name;
806         int len, i;
807
808         _enter("");
809
810         ret = ERR_PTR(-ENOMEM);
811         p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
812         if (!buf)
813                 goto out_p;
814         if (dentry->d_name.len > 4) {
815                 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
816                 p += dentry->d_name.len - 4;
817         }
818
819         /* There is an ordered list of substitutes that we have to try. */
820         read_lock(&net->sysnames_lock);
821         subs = net->sysnames;
822         refcount_inc(&subs->usage);
823         read_unlock(&net->sysnames_lock);
824
825         for (i = 0; i < subs->nr; i++) {
826                 name = subs->subs[i];
827                 len = dentry->d_name.len - 4 + strlen(name);
828                 if (len >= AFSNAMEMAX) {
829                         ret = ERR_PTR(-ENAMETOOLONG);
830                         goto out_s;
831                 }
832
833                 strcpy(p, name);
834                 ret = lookup_one_len(buf, dentry->d_parent, len);
835                 if (IS_ERR(ret) || d_is_positive(ret))
836                         goto out_s;
837                 dput(ret);
838         }
839
840         /* We don't want to d_add() the @sys dentry here as we don't want to
841          * the cached dentry to hide changes to the sysnames list.
842          */
843         ret = NULL;
844 out_s:
845         afs_put_sysnames(subs);
846         kfree(buf);
847 out_p:
848         key_put(key);
849         return ret;
850 }
851
852 /*
853  * look up an entry in a directory
854  */
855 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
856                                  unsigned int flags)
857 {
858         struct afs_vnode *dvnode = AFS_FS_I(dir);
859         struct inode *inode;
860         struct dentry *d;
861         struct key *key;
862         int ret;
863
864         _enter("{%llx:%llu},%p{%pd},",
865                dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
866
867         ASSERTCMP(d_inode(dentry), ==, NULL);
868
869         if (dentry->d_name.len >= AFSNAMEMAX) {
870                 _leave(" = -ENAMETOOLONG");
871                 return ERR_PTR(-ENAMETOOLONG);
872         }
873
874         if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
875                 _leave(" = -ESTALE");
876                 return ERR_PTR(-ESTALE);
877         }
878
879         key = afs_request_key(dvnode->volume->cell);
880         if (IS_ERR(key)) {
881                 _leave(" = %ld [key]", PTR_ERR(key));
882                 return ERR_CAST(key);
883         }
884
885         ret = afs_validate(dvnode, key);
886         if (ret < 0) {
887                 key_put(key);
888                 _leave(" = %d [val]", ret);
889                 return ERR_PTR(ret);
890         }
891
892         if (dentry->d_name.len >= 4 &&
893             dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
894             dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
895             dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
896             dentry->d_name.name[dentry->d_name.len - 1] == 's')
897                 return afs_lookup_atsys(dir, dentry, key);
898
899         afs_stat_v(dvnode, n_lookup);
900         inode = afs_do_lookup(dir, dentry, key);
901         key_put(key);
902         if (inode == ERR_PTR(-ENOENT)) {
903                 inode = afs_try_auto_mntpt(dentry, dir);
904         } else {
905                 dentry->d_fsdata =
906                         (void *)(unsigned long)dvnode->status.data_version;
907         }
908         d = d_splice_alias(inode, dentry);
909         if (!IS_ERR_OR_NULL(d)) {
910                 d->d_fsdata = dentry->d_fsdata;
911                 trace_afs_lookup(dvnode, &d->d_name,
912                                  inode ? AFS_FS_I(inode) : NULL);
913         } else {
914                 trace_afs_lookup(dvnode, &dentry->d_name,
915                                  inode ? AFS_FS_I(inode) : NULL);
916         }
917         return d;
918 }
919
920 /*
921  * check that a dentry lookup hit has found a valid entry
922  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
923  *   inode
924  */
925 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
926 {
927         struct afs_vnode *vnode, *dir;
928         struct afs_fid uninitialized_var(fid);
929         struct dentry *parent;
930         struct inode *inode;
931         struct key *key;
932         long dir_version, de_version;
933         int ret;
934
935         if (flags & LOOKUP_RCU)
936                 return -ECHILD;
937
938         if (d_really_is_positive(dentry)) {
939                 vnode = AFS_FS_I(d_inode(dentry));
940                 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
941                        vnode->fid.vid, vnode->fid.vnode, dentry,
942                        vnode->flags);
943         } else {
944                 _enter("{neg n=%pd}", dentry);
945         }
946
947         key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
948         if (IS_ERR(key))
949                 key = NULL;
950
951         if (d_really_is_positive(dentry)) {
952                 inode = d_inode(dentry);
953                 if (inode) {
954                         vnode = AFS_FS_I(inode);
955                         afs_validate(vnode, key);
956                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
957                                 goto out_bad;
958                 }
959         }
960
961         /* lock down the parent dentry so we can peer at it */
962         parent = dget_parent(dentry);
963         dir = AFS_FS_I(d_inode(parent));
964
965         /* validate the parent directory */
966         afs_validate(dir, key);
967
968         if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
969                 _debug("%pd: parent dir deleted", dentry);
970                 goto out_bad_parent;
971         }
972
973         /* We only need to invalidate a dentry if the server's copy changed
974          * behind our back.  If we made the change, it's no problem.  Note that
975          * on a 32-bit system, we only have 32 bits in the dentry to store the
976          * version.
977          */
978         dir_version = (long)dir->status.data_version;
979         de_version = (long)dentry->d_fsdata;
980         if (de_version == dir_version)
981                 goto out_valid;
982
983         dir_version = (long)dir->invalid_before;
984         if (de_version - dir_version >= 0)
985                 goto out_valid;
986
987         _debug("dir modified");
988         afs_stat_v(dir, n_reval);
989
990         /* search the directory for this vnode */
991         ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
992         switch (ret) {
993         case 0:
994                 /* the filename maps to something */
995                 if (d_really_is_negative(dentry))
996                         goto out_bad_parent;
997                 inode = d_inode(dentry);
998                 if (is_bad_inode(inode)) {
999                         printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1000                                dentry);
1001                         goto out_bad_parent;
1002                 }
1003
1004                 vnode = AFS_FS_I(inode);
1005
1006                 /* if the vnode ID has changed, then the dirent points to a
1007                  * different file */
1008                 if (fid.vnode != vnode->fid.vnode) {
1009                         _debug("%pd: dirent changed [%llu != %llu]",
1010                                dentry, fid.vnode,
1011                                vnode->fid.vnode);
1012                         goto not_found;
1013                 }
1014
1015                 /* if the vnode ID uniqifier has changed, then the file has
1016                  * been deleted and replaced, and the original vnode ID has
1017                  * been reused */
1018                 if (fid.unique != vnode->fid.unique) {
1019                         _debug("%pd: file deleted (uq %u -> %u I:%u)",
1020                                dentry, fid.unique,
1021                                vnode->fid.unique,
1022                                vnode->vfs_inode.i_generation);
1023                         write_seqlock(&vnode->cb_lock);
1024                         set_bit(AFS_VNODE_DELETED, &vnode->flags);
1025                         write_sequnlock(&vnode->cb_lock);
1026                         goto not_found;
1027                 }
1028                 goto out_valid;
1029
1030         case -ENOENT:
1031                 /* the filename is unknown */
1032                 _debug("%pd: dirent not found", dentry);
1033                 if (d_really_is_positive(dentry))
1034                         goto not_found;
1035                 goto out_valid;
1036
1037         default:
1038                 _debug("failed to iterate dir %pd: %d",
1039                        parent, ret);
1040                 goto out_bad_parent;
1041         }
1042
1043 out_valid:
1044         dentry->d_fsdata = (void *)dir_version;
1045         dput(parent);
1046         key_put(key);
1047         _leave(" = 1 [valid]");
1048         return 1;
1049
1050         /* the dirent, if it exists, now points to a different vnode */
1051 not_found:
1052         spin_lock(&dentry->d_lock);
1053         dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1054         spin_unlock(&dentry->d_lock);
1055
1056 out_bad_parent:
1057         _debug("dropping dentry %pd2", dentry);
1058         dput(parent);
1059 out_bad:
1060         key_put(key);
1061
1062         _leave(" = 0 [bad]");
1063         return 0;
1064 }
1065
1066 /*
1067  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1068  * sleep)
1069  * - called from dput() when d_count is going to 0.
1070  * - return 1 to request dentry be unhashed, 0 otherwise
1071  */
1072 static int afs_d_delete(const struct dentry *dentry)
1073 {
1074         _enter("%pd", dentry);
1075
1076         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1077                 goto zap;
1078
1079         if (d_really_is_positive(dentry) &&
1080             (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
1081              test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1082                 goto zap;
1083
1084         _leave(" = 0 [keep]");
1085         return 0;
1086
1087 zap:
1088         _leave(" = 1 [zap]");
1089         return 1;
1090 }
1091
1092 /*
1093  * Clean up sillyrename files on dentry removal.
1094  */
1095 static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1096 {
1097         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1098                 afs_silly_iput(dentry, inode);
1099         iput(inode);
1100 }
1101
1102 /*
1103  * handle dentry release
1104  */
1105 void afs_d_release(struct dentry *dentry)
1106 {
1107         _enter("%pd", dentry);
1108 }
1109
1110 /*
1111  * Create a new inode for create/mkdir/symlink
1112  */
1113 static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1114                                 struct dentry *new_dentry,
1115                                 struct afs_fid *newfid,
1116                                 struct afs_status_cb *new_scb)
1117 {
1118         struct afs_vnode *vnode;
1119         struct inode *inode;
1120
1121         if (fc->ac.error < 0)
1122                 return;
1123
1124         inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1125                          newfid, new_scb, fc->cbi, fc->vnode);
1126         if (IS_ERR(inode)) {
1127                 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1128                  * the new directory on the server.
1129                  */
1130                 fc->ac.error = PTR_ERR(inode);
1131                 return;
1132         }
1133
1134         vnode = AFS_FS_I(inode);
1135         set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1136         if (fc->ac.error == 0)
1137                 afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
1138         d_instantiate(new_dentry, inode);
1139 }
1140
1141 /*
1142  * create a directory on an AFS filesystem
1143  */
1144 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1145 {
1146         struct afs_status_cb *scb;
1147         struct afs_fs_cursor fc;
1148         struct afs_vnode *dvnode = AFS_FS_I(dir);
1149         struct afs_fid newfid;
1150         struct key *key;
1151         int ret;
1152
1153         mode |= S_IFDIR;
1154
1155         _enter("{%llx:%llu},{%pd},%ho",
1156                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1157
1158         ret = -ENOMEM;
1159         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1160         if (!scb)
1161                 goto error;
1162
1163         key = afs_request_key(dvnode->volume->cell);
1164         if (IS_ERR(key)) {
1165                 ret = PTR_ERR(key);
1166                 goto error_scb;
1167         }
1168
1169         ret = -ERESTARTSYS;
1170         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1171                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1172
1173                 while (afs_select_fileserver(&fc)) {
1174                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1175                         afs_fs_create(&fc, dentry->d_name.name, mode,
1176                                       &scb[0], &newfid, &scb[1]);
1177                 }
1178
1179                 afs_check_for_remote_deletion(&fc, dvnode);
1180                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1181                                         &data_version, &scb[0]);
1182                 afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1183                 ret = afs_end_vnode_operation(&fc);
1184                 if (ret < 0)
1185                         goto error_key;
1186         } else {
1187                 goto error_key;
1188         }
1189
1190         if (ret == 0 &&
1191             test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1192                 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1193                                  afs_edit_dir_for_create);
1194
1195         key_put(key);
1196         kfree(scb);
1197         _leave(" = 0");
1198         return 0;
1199
1200 error_key:
1201         key_put(key);
1202 error_scb:
1203         kfree(scb);
1204 error:
1205         d_drop(dentry);
1206         _leave(" = %d", ret);
1207         return ret;
1208 }
1209
1210 /*
1211  * Remove a subdir from a directory.
1212  */
1213 static void afs_dir_remove_subdir(struct dentry *dentry)
1214 {
1215         if (d_really_is_positive(dentry)) {
1216                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1217
1218                 clear_nlink(&vnode->vfs_inode);
1219                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1220                 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1221                 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1222         }
1223 }
1224
1225 /*
1226  * remove a directory from an AFS filesystem
1227  */
1228 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1229 {
1230         struct afs_status_cb *scb;
1231         struct afs_fs_cursor fc;
1232         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1233         struct key *key;
1234         int ret;
1235
1236         _enter("{%llx:%llu},{%pd}",
1237                dvnode->fid.vid, dvnode->fid.vnode, dentry);
1238
1239         scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1240         if (!scb)
1241                 return -ENOMEM;
1242
1243         key = afs_request_key(dvnode->volume->cell);
1244         if (IS_ERR(key)) {
1245                 ret = PTR_ERR(key);
1246                 goto error;
1247         }
1248
1249         /* Try to make sure we have a callback promise on the victim. */
1250         if (d_really_is_positive(dentry)) {
1251                 vnode = AFS_FS_I(d_inode(dentry));
1252                 ret = afs_validate(vnode, key);
1253                 if (ret < 0)
1254                         goto error_key;
1255         }
1256
1257         if (vnode) {
1258                 ret = down_write_killable(&vnode->rmdir_lock);
1259                 if (ret < 0)
1260                         goto error_key;
1261         }
1262
1263         ret = -ERESTARTSYS;
1264         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1265                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1266
1267                 while (afs_select_fileserver(&fc)) {
1268                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1269                         afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1270                 }
1271
1272                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1273                                         &data_version, scb);
1274                 ret = afs_end_vnode_operation(&fc);
1275                 if (ret == 0) {
1276                         afs_dir_remove_subdir(dentry);
1277                         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1278                                 afs_edit_dir_remove(dvnode, &dentry->d_name,
1279                                                     afs_edit_dir_for_rmdir);
1280                 }
1281         }
1282
1283         if (vnode)
1284                 up_write(&vnode->rmdir_lock);
1285 error_key:
1286         key_put(key);
1287 error:
1288         kfree(scb);
1289         return ret;
1290 }
1291
1292 /*
1293  * Remove a link to a file or symlink from a directory.
1294  *
1295  * If the file was not deleted due to excess hard links, the fileserver will
1296  * break the callback promise on the file - if it had one - before it returns
1297  * to us, and if it was deleted, it won't
1298  *
1299  * However, if we didn't have a callback promise outstanding, or it was
1300  * outstanding on a different server, then it won't break it either...
1301  */
1302 static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1303                                struct key *key)
1304 {
1305         int ret = 0;
1306
1307         if (d_really_is_positive(dentry)) {
1308                 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1309
1310                 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1311                         /* Already done */
1312                 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1313                         write_seqlock(&vnode->cb_lock);
1314                         drop_nlink(&vnode->vfs_inode);
1315                         if (vnode->vfs_inode.i_nlink == 0) {
1316                                 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1317                                 __afs_break_callback(vnode);
1318                         }
1319                         write_sequnlock(&vnode->cb_lock);
1320                         ret = 0;
1321                 } else {
1322                         afs_break_callback(vnode);
1323
1324                         if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1325                                 kdebug("AFS_VNODE_DELETED");
1326
1327                         ret = afs_validate(vnode, key);
1328                         if (ret == -ESTALE)
1329                                 ret = 0;
1330                 }
1331                 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1332         }
1333
1334         return ret;
1335 }
1336
1337 /*
1338  * Remove a file or symlink from an AFS filesystem.
1339  */
1340 static int afs_unlink(struct inode *dir, struct dentry *dentry)
1341 {
1342         struct afs_fs_cursor fc;
1343         struct afs_status_cb *scb;
1344         struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1345         struct key *key;
1346         bool need_rehash = false;
1347         int ret;
1348
1349         _enter("{%llx:%llu},{%pd}",
1350                dvnode->fid.vid, dvnode->fid.vnode, dentry);
1351
1352         if (dentry->d_name.len >= AFSNAMEMAX)
1353                 return -ENAMETOOLONG;
1354
1355         ret = -ENOMEM;
1356         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1357         if (!scb)
1358                 goto error;
1359
1360         key = afs_request_key(dvnode->volume->cell);
1361         if (IS_ERR(key)) {
1362                 ret = PTR_ERR(key);
1363                 goto error_scb;
1364         }
1365
1366         /* Try to make sure we have a callback promise on the victim. */
1367         if (d_really_is_positive(dentry)) {
1368                 vnode = AFS_FS_I(d_inode(dentry));
1369                 ret = afs_validate(vnode, key);
1370                 if (ret < 0)
1371                         goto error_key;
1372         }
1373
1374         spin_lock(&dentry->d_lock);
1375         if (vnode && d_count(dentry) > 1) {
1376                 spin_unlock(&dentry->d_lock);
1377                 /* Start asynchronous writeout of the inode */
1378                 write_inode_now(d_inode(dentry), 0);
1379                 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1380                 goto error_key;
1381         }
1382         if (!d_unhashed(dentry)) {
1383                 /* Prevent a race with RCU lookup. */
1384                 __d_drop(dentry);
1385                 need_rehash = true;
1386         }
1387         spin_unlock(&dentry->d_lock);
1388
1389         ret = -ERESTARTSYS;
1390         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1391                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1392                 afs_dataversion_t data_version_2 = vnode->status.data_version;
1393
1394                 while (afs_select_fileserver(&fc)) {
1395                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1396                         fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1397
1398                         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1399                             !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1400                                 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1401                                                     &scb[0], &scb[1]);
1402                                 if (fc.ac.error != -ECONNABORTED ||
1403                                     fc.ac.abort_code != RXGEN_OPCODE)
1404                                         continue;
1405                                 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1406                         }
1407
1408                         afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1409                 }
1410
1411                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1412                                         &data_version, &scb[0]);
1413                 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1414                                         &data_version_2, &scb[1]);
1415                 ret = afs_end_vnode_operation(&fc);
1416                 if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1417                         ret = afs_dir_remove_link(dvnode, dentry, key);
1418                 if (ret == 0 &&
1419                     test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1420                         afs_edit_dir_remove(dvnode, &dentry->d_name,
1421                                             afs_edit_dir_for_unlink);
1422         }
1423
1424         if (need_rehash && ret < 0 && ret != -ENOENT)
1425                 d_rehash(dentry);
1426
1427 error_key:
1428         key_put(key);
1429 error_scb:
1430         kfree(scb);
1431 error:
1432         _leave(" = %d", ret);
1433         return ret;
1434 }
1435
1436 /*
1437  * create a regular file on an AFS filesystem
1438  */
1439 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1440                       bool excl)
1441 {
1442         struct afs_fs_cursor fc;
1443         struct afs_status_cb *scb;
1444         struct afs_vnode *dvnode = AFS_FS_I(dir);
1445         struct afs_fid newfid;
1446         struct key *key;
1447         int ret;
1448
1449         mode |= S_IFREG;
1450
1451         _enter("{%llx:%llu},{%pd},%ho,",
1452                dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1453
1454         ret = -ENAMETOOLONG;
1455         if (dentry->d_name.len >= AFSNAMEMAX)
1456                 goto error;
1457
1458         key = afs_request_key(dvnode->volume->cell);
1459         if (IS_ERR(key)) {
1460                 ret = PTR_ERR(key);
1461                 goto error;
1462         }
1463
1464         ret = -ENOMEM;
1465         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1466         if (!scb)
1467                 goto error_scb;
1468
1469         ret = -ERESTARTSYS;
1470         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1471                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1472
1473                 while (afs_select_fileserver(&fc)) {
1474                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1475                         afs_fs_create(&fc, dentry->d_name.name, mode,
1476                                       &scb[0], &newfid, &scb[1]);
1477                 }
1478
1479                 afs_check_for_remote_deletion(&fc, dvnode);
1480                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1481                                         &data_version, &scb[0]);
1482                 afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1483                 ret = afs_end_vnode_operation(&fc);
1484                 if (ret < 0)
1485                         goto error_key;
1486         } else {
1487                 goto error_key;
1488         }
1489
1490         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1491                 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1492                                  afs_edit_dir_for_create);
1493
1494         kfree(scb);
1495         key_put(key);
1496         _leave(" = 0");
1497         return 0;
1498
1499 error_scb:
1500         kfree(scb);
1501 error_key:
1502         key_put(key);
1503 error:
1504         d_drop(dentry);
1505         _leave(" = %d", ret);
1506         return ret;
1507 }
1508
1509 /*
1510  * create a hard link between files in an AFS filesystem
1511  */
1512 static int afs_link(struct dentry *from, struct inode *dir,
1513                     struct dentry *dentry)
1514 {
1515         struct afs_fs_cursor fc;
1516         struct afs_status_cb *scb;
1517         struct afs_vnode *dvnode = AFS_FS_I(dir);
1518         struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1519         struct key *key;
1520         int ret;
1521
1522         _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1523                vnode->fid.vid, vnode->fid.vnode,
1524                dvnode->fid.vid, dvnode->fid.vnode,
1525                dentry);
1526
1527         ret = -ENAMETOOLONG;
1528         if (dentry->d_name.len >= AFSNAMEMAX)
1529                 goto error;
1530
1531         ret = -ENOMEM;
1532         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1533         if (!scb)
1534                 goto error;
1535
1536         key = afs_request_key(dvnode->volume->cell);
1537         if (IS_ERR(key)) {
1538                 ret = PTR_ERR(key);
1539                 goto error_scb;
1540         }
1541
1542         ret = -ERESTARTSYS;
1543         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1544                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1545
1546                 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1547                         afs_end_vnode_operation(&fc);
1548                         goto error_key;
1549                 }
1550
1551                 while (afs_select_fileserver(&fc)) {
1552                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1553                         fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1554                         afs_fs_link(&fc, vnode, dentry->d_name.name,
1555                                     &scb[0], &scb[1]);
1556                 }
1557
1558                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1559                                         &data_version, &scb[0]);
1560                 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1561                                         NULL, &scb[1]);
1562                 ihold(&vnode->vfs_inode);
1563                 d_instantiate(dentry, &vnode->vfs_inode);
1564
1565                 mutex_unlock(&vnode->io_lock);
1566                 ret = afs_end_vnode_operation(&fc);
1567                 if (ret < 0)
1568                         goto error_key;
1569         } else {
1570                 goto error_key;
1571         }
1572
1573         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1574                 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1575                                  afs_edit_dir_for_link);
1576
1577         key_put(key);
1578         kfree(scb);
1579         _leave(" = 0");
1580         return 0;
1581
1582 error_key:
1583         key_put(key);
1584 error_scb:
1585         kfree(scb);
1586 error:
1587         d_drop(dentry);
1588         _leave(" = %d", ret);
1589         return ret;
1590 }
1591
1592 /*
1593  * create a symlink in an AFS filesystem
1594  */
1595 static int afs_symlink(struct inode *dir, struct dentry *dentry,
1596                        const char *content)
1597 {
1598         struct afs_fs_cursor fc;
1599         struct afs_status_cb *scb;
1600         struct afs_vnode *dvnode = AFS_FS_I(dir);
1601         struct afs_fid newfid;
1602         struct key *key;
1603         int ret;
1604
1605         _enter("{%llx:%llu},{%pd},%s",
1606                dvnode->fid.vid, dvnode->fid.vnode, dentry,
1607                content);
1608
1609         ret = -ENAMETOOLONG;
1610         if (dentry->d_name.len >= AFSNAMEMAX)
1611                 goto error;
1612
1613         ret = -EINVAL;
1614         if (strlen(content) >= AFSPATHMAX)
1615                 goto error;
1616
1617         ret = -ENOMEM;
1618         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1619         if (!scb)
1620                 goto error;
1621
1622         key = afs_request_key(dvnode->volume->cell);
1623         if (IS_ERR(key)) {
1624                 ret = PTR_ERR(key);
1625                 goto error_scb;
1626         }
1627
1628         ret = -ERESTARTSYS;
1629         if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1630                 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1631
1632                 while (afs_select_fileserver(&fc)) {
1633                         fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1634                         afs_fs_symlink(&fc, dentry->d_name.name, content,
1635                                        &scb[0], &newfid, &scb[1]);
1636                 }
1637
1638                 afs_check_for_remote_deletion(&fc, dvnode);
1639                 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1640                                         &data_version, &scb[0]);
1641                 afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1642                 ret = afs_end_vnode_operation(&fc);
1643                 if (ret < 0)
1644                         goto error_key;
1645         } else {
1646                 goto error_key;
1647         }
1648
1649         if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1650                 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1651                                  afs_edit_dir_for_symlink);
1652
1653         key_put(key);
1654         kfree(scb);
1655         _leave(" = 0");
1656         return 0;
1657
1658 error_key:
1659         key_put(key);
1660 error_scb:
1661         kfree(scb);
1662 error:
1663         d_drop(dentry);
1664         _leave(" = %d", ret);
1665         return ret;
1666 }
1667
1668 /*
1669  * rename a file in an AFS filesystem and/or move it between directories
1670  */
1671 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1672                       struct inode *new_dir, struct dentry *new_dentry,
1673                       unsigned int flags)
1674 {
1675         struct afs_fs_cursor fc;
1676         struct afs_status_cb *scb;
1677         struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1678         struct dentry *tmp = NULL, *rehash = NULL;
1679         struct inode *new_inode;
1680         struct key *key;
1681         bool new_negative = d_is_negative(new_dentry);
1682         int ret;
1683
1684         if (flags)
1685                 return -EINVAL;
1686
1687         /* Don't allow silly-rename files be moved around. */
1688         if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1689                 return -EINVAL;
1690
1691         vnode = AFS_FS_I(d_inode(old_dentry));
1692         orig_dvnode = AFS_FS_I(old_dir);
1693         new_dvnode = AFS_FS_I(new_dir);
1694
1695         _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1696                orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1697                vnode->fid.vid, vnode->fid.vnode,
1698                new_dvnode->fid.vid, new_dvnode->fid.vnode,
1699                new_dentry);
1700
1701         ret = -ENOMEM;
1702         scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1703         if (!scb)
1704                 goto error;
1705
1706         key = afs_request_key(orig_dvnode->volume->cell);
1707         if (IS_ERR(key)) {
1708                 ret = PTR_ERR(key);
1709                 goto error_scb;
1710         }
1711
1712         /* For non-directories, check whether the target is busy and if so,
1713          * make a copy of the dentry and then do a silly-rename.  If the
1714          * silly-rename succeeds, the copied dentry is hashed and becomes the
1715          * new target.
1716          */
1717         if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1718                 /* To prevent any new references to the target during the
1719                  * rename, we unhash the dentry in advance.
1720                  */
1721                 if (!d_unhashed(new_dentry)) {
1722                         d_drop(new_dentry);
1723                         rehash = new_dentry;
1724                 }
1725
1726                 if (d_count(new_dentry) > 2) {
1727                         /* copy the target dentry's name */
1728                         ret = -ENOMEM;
1729                         tmp = d_alloc(new_dentry->d_parent,
1730                                       &new_dentry->d_name);
1731                         if (!tmp)
1732                                 goto error_rehash;
1733
1734                         ret = afs_sillyrename(new_dvnode,
1735                                               AFS_FS_I(d_inode(new_dentry)),
1736                                               new_dentry, key);
1737                         if (ret)
1738                                 goto error_rehash;
1739
1740                         new_dentry = tmp;
1741                         rehash = NULL;
1742                         new_negative = true;
1743                 }
1744         }
1745
1746         ret = -ERESTARTSYS;
1747         if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1748                 afs_dataversion_t orig_data_version;
1749                 afs_dataversion_t new_data_version;
1750                 struct afs_status_cb *new_scb = &scb[1];
1751
1752                 orig_data_version = orig_dvnode->status.data_version + 1;
1753
1754                 if (orig_dvnode != new_dvnode) {
1755                         if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1756                                 afs_end_vnode_operation(&fc);
1757                                 goto error_rehash;
1758                         }
1759                         new_data_version = new_dvnode->status.data_version;
1760                 } else {
1761                         new_data_version = orig_data_version;
1762                         new_scb = &scb[0];
1763                 }
1764
1765                 while (afs_select_fileserver(&fc)) {
1766                         fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1767                         fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1768                         afs_fs_rename(&fc, old_dentry->d_name.name,
1769                                       new_dvnode, new_dentry->d_name.name,
1770                                       &scb[0], new_scb);
1771                 }
1772
1773                 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1774                                         &orig_data_version, &scb[0]);
1775                 if (new_dvnode != orig_dvnode) {
1776                         afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1777                                                 &new_data_version, &scb[1]);
1778                         mutex_unlock(&new_dvnode->io_lock);
1779                 }
1780                 ret = afs_end_vnode_operation(&fc);
1781                 if (ret < 0)
1782                         goto error_rehash;
1783         }
1784
1785         if (ret == 0) {
1786                 if (rehash)
1787                         d_rehash(rehash);
1788                 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1789                     afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1790                                         afs_edit_dir_for_rename_0);
1791
1792                 if (!new_negative &&
1793                     test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1794                         afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1795                                             afs_edit_dir_for_rename_1);
1796
1797                 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1798                         afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1799                                          &vnode->fid, afs_edit_dir_for_rename_2);
1800
1801                 new_inode = d_inode(new_dentry);
1802                 if (new_inode) {
1803                         spin_lock(&new_inode->i_lock);
1804                         if (new_inode->i_nlink > 0)
1805                                 drop_nlink(new_inode);
1806                         spin_unlock(&new_inode->i_lock);
1807                 }
1808                 d_move(old_dentry, new_dentry);
1809                 goto error_tmp;
1810         }
1811
1812 error_rehash:
1813         if (rehash)
1814                 d_rehash(rehash);
1815 error_tmp:
1816         if (tmp)
1817                 dput(tmp);
1818         key_put(key);
1819 error_scb:
1820         kfree(scb);
1821 error:
1822         _leave(" = %d", ret);
1823         return ret;
1824 }
1825
1826 /*
1827  * Release a directory page and clean up its private state if it's not busy
1828  * - return true if the page can now be released, false if not
1829  */
1830 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1831 {
1832         struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1833
1834         _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1835
1836         set_page_private(page, 0);
1837         ClearPagePrivate(page);
1838
1839         /* The directory will need reloading. */
1840         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1841                 afs_stat_v(dvnode, n_relpg);
1842         return 1;
1843 }
1844
1845 /*
1846  * invalidate part or all of a page
1847  * - release a page and clean up its private data if offset is 0 (indicating
1848  *   the entire page)
1849  */
1850 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1851                                    unsigned int length)
1852 {
1853         struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1854
1855         _enter("{%lu},%u,%u", page->index, offset, length);
1856
1857         BUG_ON(!PageLocked(page));
1858
1859         /* The directory will need reloading. */
1860         if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1861                 afs_stat_v(dvnode, n_inval);
1862
1863         /* we clean up only if the entire page is being invalidated */
1864         if (offset == 0 && length == PAGE_SIZE) {
1865                 set_page_private(page, 0);
1866                 ClearPagePrivate(page);
1867         }
1868 }