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