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