blob: 43bb3b23a87940c1aba72ac4f03d54fa132b6a48 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* dir.c: AFS filesystem directory handling
2 *
David Howellsf3ddee82018-04-06 14:17:25 +01003 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs.h>
Nick Piggin34286d62011-01-07 17:49:57 +110014#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pagemap.h>
David Howellsf3ddee82018-04-06 14:17:25 +010016#include <linux/swap.h>
David Howells00d3b7a2007-04-26 15:57:07 -070017#include <linux/ctype.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
David Howellsf3ddee82018-04-06 14:17:25 +010019#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
David Howells4ea219a2018-04-06 14:17:25 +010021#include "xdr_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
David Howells260a9802007-04-26 15:59:35 -070023static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040024 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int afs_dir_open(struct inode *inode, struct file *file);
Al Viro1bbae9f2013-05-22 16:31:14 -040026static int afs_readdir(struct file *file, struct dir_context *ctx);
Al Viro0b728e12012-06-10 16:03:43 -040027static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
Nick Pigginfe15ce42011-01-07 17:49:23 +110028static int afs_d_delete(const struct dentry *dentry);
David Howells5cf9dd52018-04-09 21:12:31 +010029static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
David Howellsafefdbb2006-10-03 01:13:46 -070030 loff_t fpos, u64 ino, unsigned dtype);
David Howells5cf9dd52018-04-09 21:12:31 +010031static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
32 loff_t fpos, u64 ino, unsigned dtype);
Al Viro4acdaf22011-07-26 01:42:34 -040033static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040034 bool excl);
Al Viro18bb1db2011-07-26 01:41:39 -040035static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
David Howells260a9802007-04-26 15:59:35 -070036static 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,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +020043 struct inode *new_dir, struct dentry *new_dentry,
44 unsigned int flags);
David Howellsf3ddee82018-04-06 14:17:25 +010045static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080054const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .open = afs_dir_open,
David Howells00d3b7a2007-04-26 15:57:07 -070056 .release = afs_release,
Al Viro29884ef2016-05-10 14:27:44 -040057 .iterate_shared = afs_readdir,
David Howellse8d6c552007-07-15 23:40:12 -070058 .lock = afs_lock,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020059 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Arjan van de Ven754661f2007-02-12 00:55:38 -080062const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070063 .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,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020070 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070071 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070072 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070073 .setattr = afs_setattr,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010074 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
David Howellsf3ddee82018-04-06 14:17:25 +010077const 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
Al Virod61dcce2011-01-12 20:04:20 -050083const struct dentry_operations afs_fs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .d_revalidate = afs_d_revalidate,
85 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070086 .d_release = afs_d_release,
David Howellsd18610b2011-01-14 19:04:05 +000087 .d_automount = afs_d_automount,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
David Howells5cf9dd52018-04-09 21:12:31 +010090struct afs_lookup_one_cookie {
91 struct dir_context ctx;
92 struct qstr name;
93 bool found;
94 struct afs_fid fid;
95};
96
David Howells260a9802007-04-26 15:59:35 -070097struct afs_lookup_cookie {
David Howells5cf9dd52018-04-09 21:12:31 +010098 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * check that a directory page is valid
110 */
David Howellsf3ddee82018-04-06 14:17:25 +0100111static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
112 loff_t i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
David Howells00317632018-04-06 14:17:25 +0100114 struct afs_xdr_dir_page *dbuf;
David Howellsf3ddee82018-04-06 14:17:25 +0100115 loff_t latter, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int tmp, qty;
117
David Howellsdab17c12017-11-02 15:27:52 +0000118 /* 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);
David Howellsdab17c12017-11-02 15:27:52 +0000122 if (i_size <= off)
123 goto checked;
124
125 latter = i_size - off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (latter >= PAGE_SIZE)
127 qty = PAGE_SIZE;
128 else
129 qty = latter;
David Howells00317632018-04-06 14:17:25 +0100130 qty /= sizeof(union afs_xdr_dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* check them */
David Howells63a46812018-04-06 14:17:25 +0100133 dbuf = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 for (tmp = 0; tmp < qty; tmp++) {
David Howells00317632018-04-06 14:17:25 +0100135 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
David Howellsdab17c12017-11-02 15:27:52 +0000136 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
David Howellsf3ddee82018-04-06 14:17:25 +0100137 __func__, dvnode->vfs_inode.i_ino, tmp, qty,
David Howells00317632018-04-06 14:17:25 +0100138 ntohs(dbuf->blocks[tmp].hdr.magic));
David Howellsf3ddee82018-04-06 14:17:25 +0100139 trace_afs_dir_check_failed(dvnode, off, i_size);
David Howells63a46812018-04-06 14:17:25 +0100140 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto error;
142 }
David Howells63a46812018-04-06 14:17:25 +0100143
144 /* Make sure each block is NUL terminated so we can reasonably
145 * use string functions on it. The filenames in the page
146 * *should* be NUL-terminated anyway.
147 */
148 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
David Howells63a46812018-04-06 14:17:25 +0100151 kunmap(page);
152
David Howellsdab17c12017-11-02 15:27:52 +0000153checked:
David Howellsf3ddee82018-04-06 14:17:25 +0100154 afs_stat_v(dvnode, n_read_dir);
Al Virobe5b82d2016-04-22 15:06:44 -0400155 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
David Howellsec268152007-04-26 15:49:28 -0700157error:
Al Virobe5b82d2016-04-22 15:06:44 -0400158 return false;
David Howellsec268152007-04-26 15:49:28 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * open an AFS directory file
163 */
164static int afs_dir_open(struct inode *inode, struct file *file)
165{
166 _enter("{%lu}", inode->i_ino);
167
David Howells00317632018-04-06 14:17:25 +0100168 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
169 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David Howells08e0e7c2007-04-26 15:55:03 -0700171 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -ENOENT;
173
David Howells00d3b7a2007-04-26 15:57:07 -0700174 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
David Howellsf3ddee82018-04-06 14:17:25 +0100178 * Read the directory into the pagecache in one go, scrubbing the previous
179 * contents. The list of pages is returned, pinning them so that they don't
180 * get reclaimed during the iteration.
181 */
182static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
183{
184 struct afs_read *req;
185 loff_t i_size;
186 int nr_pages, nr_inline, i, n;
187 int ret = -ENOMEM;
188
189retry:
190 i_size = i_size_read(&dvnode->vfs_inode);
191 if (i_size < 2048)
192 return ERR_PTR(-EIO);
193 if (i_size > 2048 * 1024)
194 return ERR_PTR(-EFBIG);
195
196 _enter("%llu", i_size);
197
198 /* Get a request record to hold the page list. We want to hold it
199 * inline if we can, but we don't want to make an order 1 allocation.
200 */
201 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
202 nr_inline = nr_pages;
203 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
204 nr_inline = 0;
205
206 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
207 GFP_KERNEL);
208 if (!req)
209 return ERR_PTR(-ENOMEM);
210
211 refcount_set(&req->usage, 1);
212 req->nr_pages = nr_pages;
213 req->actual_len = i_size; /* May change */
214 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
215 req->data_version = dvnode->status.data_version; /* May change */
216 if (nr_inline > 0) {
217 req->pages = req->array;
218 } else {
219 req->pages = kcalloc(nr_pages, sizeof(struct page *),
220 GFP_KERNEL);
221 if (!req->pages)
222 goto error;
223 }
224
225 /* Get a list of all the pages that hold or will hold the directory
226 * content. We need to fill in any gaps that we might find where the
227 * memory reclaimer has been at work. If there are any gaps, we will
228 * need to reread the entire directory contents.
229 */
230 i = 0;
231 do {
232 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
233 req->nr_pages - i,
234 req->pages + i);
235 _debug("find %u at %u/%u", n, i, req->nr_pages);
236 if (n == 0) {
237 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
238
239 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
240 afs_stat_v(dvnode, n_inval);
241
242 ret = -ENOMEM;
243 req->pages[i] = __page_cache_alloc(gfp);
244 if (!req->pages[i])
245 goto error;
246 ret = add_to_page_cache_lru(req->pages[i],
247 dvnode->vfs_inode.i_mapping,
248 i, gfp);
249 if (ret < 0)
250 goto error;
251
252 set_page_private(req->pages[i], 1);
253 SetPagePrivate(req->pages[i]);
254 unlock_page(req->pages[i]);
255 i++;
256 } else {
257 i += n;
258 }
259 } while (i < req->nr_pages);
260
261 /* If we're going to reload, we need to lock all the pages to prevent
262 * races.
263 */
264 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
265 ret = -ERESTARTSYS;
266 for (i = 0; i < req->nr_pages; i++)
267 if (lock_page_killable(req->pages[i]) < 0)
268 goto error_unlock;
269
270 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
271 goto success;
272
273 ret = afs_fetch_data(dvnode, key, req);
274 if (ret < 0)
275 goto error_unlock_all;
276
277 task_io_account_read(PAGE_SIZE * req->nr_pages);
278
279 if (req->len < req->file_size)
280 goto content_has_grown;
281
282 /* Validate the data we just read. */
283 ret = -EIO;
284 for (i = 0; i < req->nr_pages; i++)
285 if (!afs_dir_check_page(dvnode, req->pages[i],
286 req->actual_len))
287 goto error_unlock_all;
288
289 // TODO: Trim excess pages
290
291 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
292 }
293
294success:
295 i = req->nr_pages;
296 while (i > 0)
297 unlock_page(req->pages[--i]);
298 return req;
299
300error_unlock_all:
301 i = req->nr_pages;
302error_unlock:
303 while (i > 0)
304 unlock_page(req->pages[--i]);
305error:
306 afs_put_read(req);
307 _leave(" = %d", ret);
308 return ERR_PTR(ret);
309
310content_has_grown:
311 i = req->nr_pages;
312 while (i > 0)
313 unlock_page(req->pages[--i]);
314 afs_put_read(req);
315 goto retry;
316}
317
318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * deal with one block in an AFS directory
320 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400321static int afs_dir_iterate_block(struct dir_context *ctx,
David Howells00317632018-04-06 14:17:25 +0100322 union afs_xdr_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400323 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
David Howells00317632018-04-06 14:17:25 +0100325 union afs_xdr_dirent *dire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned offset, next, curr;
327 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400328 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Al Viro1bbae9f2013-05-22 16:31:14 -0400330 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howells00317632018-04-06 14:17:25 +0100332 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* walk through the block, an entry at a time */
David Howells4ea219a2018-04-06 14:17:25 +0100335 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
336 offset < AFS_DIR_SLOTS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 offset = next
338 ) {
339 next = offset + 1;
340
341 /* skip entries marked unused in the bitmap */
David Howells00317632018-04-06 14:17:25 +0100342 if (!(block->hdr.bitmap[offset / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 (1 << (offset % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800344 _debug("ENT[%zu.%u]: unused",
David Howells00317632018-04-06 14:17:25 +0100345 blkoff / sizeof(union afs_xdr_dir_block), offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (offset >= curr)
Al Viro1bbae9f2013-05-22 16:31:14 -0400347 ctx->pos = blkoff +
David Howells00317632018-04-06 14:17:25 +0100348 next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 continue;
350 }
351
352 /* got a valid entry */
353 dire = &block->dirents[offset];
354 nlen = strnlen(dire->u.name,
355 sizeof(*block) -
David Howells00317632018-04-06 14:17:25 +0100356 offset * sizeof(union afs_xdr_dirent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800358 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
David Howells00317632018-04-06 14:17:25 +0100359 blkoff / sizeof(union afs_xdr_dir_block), offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 (offset < curr ? "skip" : "fill"),
361 nlen, dire->u.name);
362
363 /* work out where the next possible entry is */
David Howells00317632018-04-06 14:17:25 +0100364 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
David Howells4ea219a2018-04-06 14:17:25 +0100365 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800366 _debug("ENT[%zu.%u]:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 " %u travelled beyond end dir block"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800368 " (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100369 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 offset, next, tmp, nlen);
371 return -EIO;
372 }
David Howells00317632018-04-06 14:17:25 +0100373 if (!(block->hdr.bitmap[next / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 (1 << (next % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800375 _debug("ENT[%zu.%u]:"
376 " %u unmarked extension (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100377 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 offset, next, tmp, nlen);
379 return -EIO;
380 }
381
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800382 _debug("ENT[%zu.%u]: ext %u/%zu",
David Howells00317632018-04-06 14:17:25 +0100383 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 next, tmp, nlen);
385 next++;
386 }
387
388 /* skip if starts before the current position */
389 if (offset < curr)
390 continue;
391
392 /* found the next entry */
Al Viro1bbae9f2013-05-22 16:31:14 -0400393 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ntohl(dire->u.vnode),
David Howells5cf9dd52018-04-09 21:12:31 +0100395 (ctx->actor == afs_lookup_filldir ||
396 ctx->actor == afs_lookup_one_filldir)?
Al Viro1bbae9f2013-05-22 16:31:14 -0400397 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 _leave(" = 0 [full]");
399 return 0;
400 }
401
David Howells00317632018-04-06 14:17:25 +0100402 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 _leave(" = 1 [more]");
406 return 1;
David Howellsec268152007-04-26 15:49:28 -0700407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
David Howells08e0e7c2007-04-26 15:55:03 -0700410 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400412static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
413 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
David Howellsf3ddee82018-04-06 14:17:25 +0100415 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells00317632018-04-06 14:17:25 +0100416 struct afs_xdr_dir_page *dbuf;
417 union afs_xdr_dir_block *dblock;
David Howellsf3ddee82018-04-06 14:17:25 +0100418 struct afs_read *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct page *page;
420 unsigned blkoff, limit;
421 int ret;
422
Al Viro1bbae9f2013-05-22 16:31:14 -0400423 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Howells08e0e7c2007-04-26 15:55:03 -0700425 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 _leave(" = -ESTALE");
427 return -ESTALE;
428 }
429
David Howellsf3ddee82018-04-06 14:17:25 +0100430 req = afs_read_dir(dvnode, key);
431 if (IS_ERR(req))
432 return PTR_ERR(req);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* round the file position up to the next entry boundary */
David Howells00317632018-04-06 14:17:25 +0100435 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
436 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* walk through the blocks in sequence */
439 ret = 0;
David Howellsf3ddee82018-04-06 14:17:25 +0100440 while (ctx->pos < req->actual_len) {
David Howells00317632018-04-06 14:17:25 +0100441 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Howellsf3ddee82018-04-06 14:17:25 +0100443 /* Fetch the appropriate page from the directory and re-add it
444 * to the LRU.
445 */
446 page = req->pages[blkoff / PAGE_SIZE];
447 if (!page) {
448 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 }
David Howellsf3ddee82018-04-06 14:17:25 +0100451 mark_page_accessed(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 limit = blkoff & ~(PAGE_SIZE - 1);
454
David Howellsf3ddee82018-04-06 14:17:25 +0100455 dbuf = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* deal with the individual blocks stashed on this page */
458 do {
459 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
David Howells00317632018-04-06 14:17:25 +0100460 sizeof(union afs_xdr_dir_block)];
Al Viro1bbae9f2013-05-22 16:31:14 -0400461 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (ret != 1) {
David Howellsf3ddee82018-04-06 14:17:25 +0100463 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
465 }
466
David Howells00317632018-04-06 14:17:25 +0100467 blkoff += sizeof(union afs_xdr_dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Al Viro1bbae9f2013-05-22 16:31:14 -0400469 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David Howellsf3ddee82018-04-06 14:17:25 +0100471 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 ret = 0;
473 }
474
David Howellsec268152007-04-26 15:49:28 -0700475out:
David Howellsf3ddee82018-04-06 14:17:25 +0100476 afs_put_read(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 _leave(" = %d", ret);
478 return ret;
David Howellsec268152007-04-26 15:49:28 -0700479}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * read an AFS directory
483 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400484static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
David Howells215804a2017-11-02 15:27:52 +0000486 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
David Howellsec268152007-04-26 15:49:28 -0700487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
David Howells5cf9dd52018-04-09 21:12:31 +0100490 * Search the directory for a single name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
492 * uniquifier through dtype
493 */
David Howells5cf9dd52018-04-09 21:12:31 +0100494static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
495 int nlen, loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
David Howells5cf9dd52018-04-09 21:12:31 +0100497 struct afs_lookup_one_cookie *cookie =
498 container_of(ctx, struct afs_lookup_one_cookie, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Al Viro1bbae9f2013-05-22 16:31:14 -0400500 _enter("{%s,%u},%s,%u,,%llu,%u",
501 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700502 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
David Howells08e0e7c2007-04-26 15:55:03 -0700504 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100505 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
506 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells08e0e7c2007-04-26 15:55:03 -0700507
Al Viro1bbae9f2013-05-22 16:31:14 -0400508 if (cookie->name.len != nlen ||
509 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 _leave(" = 0 [no]");
511 return 0;
512 }
513
514 cookie->fid.vnode = ino;
515 cookie->fid.unique = dtype;
516 cookie->found = 1;
517
518 _leave(" = -1 [found]");
519 return -1;
David Howellsec268152007-04-26 15:49:28 -0700520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
David Howells5cf9dd52018-04-09 21:12:31 +0100523 * Do a lookup of a single name in a directory
David Howells260a9802007-04-26 15:59:35 -0700524 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
David Howells5cf9dd52018-04-09 21:12:31 +0100526static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
527 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Al Viro1bbae9f2013-05-22 16:31:14 -0400529 struct afs_super_info *as = dir->i_sb->s_fs_info;
David Howells5cf9dd52018-04-09 21:12:31 +0100530 struct afs_lookup_one_cookie cookie = {
531 .ctx.actor = afs_lookup_one_filldir,
Al Viro1bbae9f2013-05-22 16:31:14 -0400532 .name = dentry->d_name,
533 .fid.vid = as->volume->vid
534 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int ret;
536
Al Viroa4555892014-10-21 20:11:25 -0400537 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400540 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700542 _leave(" = %d [iter]", ret);
543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 ret = -ENOENT;
547 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700548 _leave(" = -ENOENT [not found]");
549 return -ENOENT;
550 }
551
552 *fid = cookie.fid;
553 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
554 return 0;
555}
556
557/*
David Howells5cf9dd52018-04-09 21:12:31 +0100558 * search the directory for a name
559 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
560 * uniquifier through dtype
561 */
562static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
563 int nlen, loff_t fpos, u64 ino, unsigned dtype)
564{
565 struct afs_lookup_cookie *cookie =
566 container_of(ctx, struct afs_lookup_cookie, ctx);
567 int ret;
568
569 _enter("{%s,%u},%s,%u,,%llu,%u",
570 cookie->name.name, cookie->name.len, name, nlen,
571 (unsigned long long) ino, dtype);
572
573 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100574 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
575 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells5cf9dd52018-04-09 21:12:31 +0100576
577 if (cookie->found) {
578 if (cookie->nr_fids < 50) {
579 cookie->fids[cookie->nr_fids].vnode = ino;
580 cookie->fids[cookie->nr_fids].unique = dtype;
581 cookie->nr_fids++;
582 }
583 } else if (cookie->name.len == nlen &&
584 memcmp(cookie->name.name, name, nlen) == 0) {
585 cookie->fids[0].vnode = ino;
586 cookie->fids[0].unique = dtype;
587 cookie->found = 1;
588 if (cookie->one_only)
589 return -1;
590 }
591
592 ret = cookie->nr_fids >= 50 ? -1 : 0;
593 _leave(" = %d", ret);
594 return ret;
595}
596
597/*
598 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
599 * files in one go and create inodes for them. The inode of the file we were
600 * asked for is returned.
601 */
602static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
603 struct key *key)
604{
605 struct afs_lookup_cookie *cookie;
606 struct afs_cb_interest *cbi = NULL;
607 struct afs_super_info *as = dir->i_sb->s_fs_info;
608 struct afs_iget_data data;
609 struct afs_fs_cursor fc;
610 struct afs_vnode *dvnode = AFS_FS_I(dir);
611 struct inode *inode = NULL;
612 int ret, i;
613
614 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
615
616 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
617 if (!cookie)
618 return ERR_PTR(-ENOMEM);
619
620 cookie->ctx.actor = afs_lookup_filldir;
621 cookie->name = dentry->d_name;
622 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
623
624 read_seqlock_excl(&dvnode->cb_lock);
625 if (dvnode->cb_interest &&
626 dvnode->cb_interest->server &&
627 test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
628 cookie->one_only = true;
629 read_sequnlock_excl(&dvnode->cb_lock);
630
631 for (i = 0; i < 50; i++)
632 cookie->fids[i].vid = as->volume->vid;
633
634 /* search the directory */
635 ret = afs_dir_iterate(dir, &cookie->ctx, key);
636 if (ret < 0) {
637 inode = ERR_PTR(ret);
638 goto out;
639 }
640
641 inode = ERR_PTR(-ENOENT);
642 if (!cookie->found)
643 goto out;
644
645 /* Check to see if we already have an inode for the primary fid. */
646 data.volume = dvnode->volume;
647 data.fid = cookie->fids[0];
648 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
649 if (inode)
650 goto out;
651
652 /* Need space for examining all the selected files */
653 inode = ERR_PTR(-ENOMEM);
654 cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
655 GFP_KERNEL);
656 if (!cookie->statuses)
657 goto out;
658
659 cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
660 GFP_KERNEL);
661 if (!cookie->callbacks)
662 goto out_s;
663
664 /* Try FS.InlineBulkStatus first. Abort codes for the individual
665 * lookups contained therein are stored in the reply without aborting
666 * the whole operation.
667 */
668 if (cookie->one_only)
669 goto no_inline_bulk_status;
670
671 inode = ERR_PTR(-ERESTARTSYS);
672 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
673 while (afs_select_fileserver(&fc)) {
674 if (test_bit(AFS_SERVER_FL_NO_IBULK,
675 &fc.cbi->server->flags)) {
676 fc.ac.abort_code = RX_INVALID_OPERATION;
677 fc.ac.error = -ECONNABORTED;
678 break;
679 }
680 afs_fs_inline_bulk_status(&fc,
681 afs_v2net(dvnode),
682 cookie->fids,
683 cookie->statuses,
684 cookie->callbacks,
685 cookie->nr_fids, NULL);
686 }
687
688 if (fc.ac.error == 0)
689 cbi = afs_get_cb_interest(fc.cbi);
690 if (fc.ac.abort_code == RX_INVALID_OPERATION)
691 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
692 inode = ERR_PTR(afs_end_vnode_operation(&fc));
693 }
694
695 if (!IS_ERR(inode))
696 goto success;
697 if (fc.ac.abort_code != RX_INVALID_OPERATION)
698 goto out_c;
699
700no_inline_bulk_status:
701 /* We could try FS.BulkStatus next, but this aborts the entire op if
702 * any of the lookups fails - so, for the moment, revert to
703 * FS.FetchStatus for just the primary fid.
704 */
705 cookie->nr_fids = 1;
706 inode = ERR_PTR(-ERESTARTSYS);
707 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
708 while (afs_select_fileserver(&fc)) {
709 afs_fs_fetch_status(&fc,
710 afs_v2net(dvnode),
711 cookie->fids,
712 cookie->statuses,
713 cookie->callbacks,
714 NULL);
715 }
716
717 if (fc.ac.error == 0)
718 cbi = afs_get_cb_interest(fc.cbi);
719 inode = ERR_PTR(afs_end_vnode_operation(&fc));
720 }
721
722 if (IS_ERR(inode))
723 goto out_c;
724
725 for (i = 0; i < cookie->nr_fids; i++)
726 cookie->statuses[i].abort_code = 0;
727
728success:
729 /* Turn all the files into inodes and save the first one - which is the
730 * one we actually want.
731 */
732 if (cookie->statuses[0].abort_code != 0)
733 inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
734
735 for (i = 0; i < cookie->nr_fids; i++) {
736 struct inode *ti;
737
738 if (cookie->statuses[i].abort_code != 0)
739 continue;
740
741 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
742 &cookie->statuses[i],
743 &cookie->callbacks[i],
744 cbi);
745 if (i == 0) {
746 inode = ti;
747 } else {
748 if (!IS_ERR(ti))
749 iput(ti);
750 }
751 }
752
753out_c:
754 afs_put_cb_interest(afs_v2net(dvnode), cbi);
755 kfree(cookie->callbacks);
756out_s:
757 kfree(cookie->statuses);
758out:
759 kfree(cookie);
760 return inode;
761}
762
763/*
David Howells6f8880d2018-04-09 21:12:31 +0100764 * Look up an entry in a directory with @sys substitution.
765 */
766static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
767 struct key *key)
768{
769 struct afs_sysnames *subs;
770 struct afs_net *net = afs_i2net(dir);
771 struct dentry *ret;
772 char *buf, *p, *name;
773 int len, i;
774
775 _enter("");
776
777 ret = ERR_PTR(-ENOMEM);
778 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
779 if (!buf)
780 goto out_p;
781 if (dentry->d_name.len > 4) {
782 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
783 p += dentry->d_name.len - 4;
784 }
785
786 /* There is an ordered list of substitutes that we have to try. */
787 read_lock(&net->sysnames_lock);
788 subs = net->sysnames;
789 refcount_inc(&subs->usage);
790 read_unlock(&net->sysnames_lock);
791
792 for (i = 0; i < subs->nr; i++) {
793 name = subs->subs[i];
794 len = dentry->d_name.len - 4 + strlen(name);
795 if (len >= AFSNAMEMAX) {
796 ret = ERR_PTR(-ENAMETOOLONG);
797 goto out_s;
798 }
799
800 strcpy(p, name);
801 ret = lookup_one_len(buf, dentry->d_parent, len);
802 if (IS_ERR(ret) || d_is_positive(ret))
803 goto out_s;
804 dput(ret);
805 }
806
807 /* We don't want to d_add() the @sys dentry here as we don't want to
808 * the cached dentry to hide changes to the sysnames list.
809 */
810 ret = NULL;
811out_s:
812 afs_put_sysnames(subs);
813 kfree(buf);
814out_p:
815 key_put(key);
816 return ret;
817}
818
819/*
David Howells08e0e7c2007-04-26 15:55:03 -0700820 * look up an entry in a directory
821 */
David Howells260a9802007-04-26 15:59:35 -0700822static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400823 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700824{
David Howells5cf9dd52018-04-09 21:12:31 +0100825 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells08e0e7c2007-04-26 15:55:03 -0700826 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700827 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700828 int ret;
829
Al Viroa4555892014-10-21 20:11:25 -0400830 _enter("{%x:%u},%p{%pd},",
David Howells5cf9dd52018-04-09 21:12:31 +0100831 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
David Howells260a9802007-04-26 15:59:35 -0700832
David Howells2b0143b2015-03-17 22:25:59 +0000833 ASSERTCMP(d_inode(dentry), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700834
David Howells45222b92007-05-10 22:22:20 -0700835 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700836 _leave(" = -ENAMETOOLONG");
837 return ERR_PTR(-ENAMETOOLONG);
838 }
839
David Howells5cf9dd52018-04-09 21:12:31 +0100840 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700841 _leave(" = -ESTALE");
842 return ERR_PTR(-ESTALE);
843 }
844
David Howells5cf9dd52018-04-09 21:12:31 +0100845 key = afs_request_key(dvnode->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700846 if (IS_ERR(key)) {
847 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800848 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700849 }
850
David Howells5cf9dd52018-04-09 21:12:31 +0100851 ret = afs_validate(dvnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700852 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700853 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700854 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return ERR_PTR(ret);
856 }
857
David Howells6f8880d2018-04-09 21:12:31 +0100858 if (dentry->d_name.len >= 4 &&
859 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
860 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
861 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
862 dentry->d_name.name[dentry->d_name.len - 1] == 's')
863 return afs_lookup_atsys(dir, dentry, key);
864
David Howellsd55b4da2018-04-06 14:17:24 +0100865 afs_stat_v(dvnode, n_lookup);
David Howells5cf9dd52018-04-09 21:12:31 +0100866 inode = afs_do_lookup(dir, dentry, key);
867 if (IS_ERR(inode)) {
868 ret = PTR_ERR(inode);
David Howells4d673da2018-02-06 06:26:30 +0000869 if (ret == -ENOENT) {
David Howells5cf9dd52018-04-09 21:12:31 +0100870 inode = afs_try_auto_mntpt(dentry, dir);
David Howells4d673da2018-02-06 06:26:30 +0000871 if (!IS_ERR(inode)) {
872 key_put(key);
873 goto success;
874 }
875
876 ret = PTR_ERR(inode);
wangleibec5eb62010-08-11 09:38:04 +0100877 }
878
David Howells260a9802007-04-26 15:59:35 -0700879 key_put(key);
880 if (ret == -ENOENT) {
881 d_add(dentry, NULL);
882 _leave(" = NULL [negative]");
883 return NULL;
884 }
885 _leave(" = %d [do]", ret);
886 return ERR_PTR(ret);
887 }
David Howells5cf9dd52018-04-09 21:12:31 +0100888 dentry->d_fsdata = (void *)(unsigned long)dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* instantiate the dentry */
David Howells00d3b7a2007-04-26 15:57:07 -0700891 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700892 if (IS_ERR(inode)) {
893 _leave(" = %ld", PTR_ERR(inode));
David Howellse231c2e2008-02-07 00:15:26 -0800894 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
wangleibec5eb62010-08-11 09:38:04 +0100897success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 d_add(dentry, inode);
David Howells5cf9dd52018-04-09 21:12:31 +0100899 _leave(" = 0 { ino=%lu v=%u }",
David Howells2b0143b2015-03-17 22:25:59 +0000900 d_inode(dentry)->i_ino,
901 d_inode(dentry)->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700904}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/*
907 * check that a dentry lookup hit has found a valid entry
908 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
909 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Al Viro0b728e12012-06-10 16:03:43 -0400911static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
David Howells260a9802007-04-26 15:59:35 -0700913 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100914 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct dentry *parent;
David Howellsc435ee32017-11-02 15:27:49 +0000916 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700917 struct key *key;
David Howellsa4ff7402018-04-06 14:17:24 +0100918 long dir_version, de_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int ret;
920
Al Viro0b728e12012-06-10 16:03:43 -0400921 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100922 return -ECHILD;
923
David Howellsc435ee32017-11-02 15:27:49 +0000924 if (d_really_is_positive(dentry)) {
925 vnode = AFS_FS_I(d_inode(dentry));
Al Viroa4555892014-10-21 20:11:25 -0400926 _enter("{v={%x:%u} n=%pd fl=%lx},",
927 vnode->fid.vid, vnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -0700928 vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000929 } else {
Al Viroa4555892014-10-21 20:11:25 -0400930 _enter("{neg n=%pd}", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
David Howells260a9802007-04-26 15:59:35 -0700933 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700934 if (IS_ERR(key))
935 key = NULL;
936
David Howellsc435ee32017-11-02 15:27:49 +0000937 if (d_really_is_positive(dentry)) {
938 inode = d_inode(dentry);
939 if (inode) {
940 vnode = AFS_FS_I(inode);
941 afs_validate(vnode, key);
942 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
943 goto out_bad;
944 }
945 }
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700948 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000949 dir = AFS_FS_I(d_inode(parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
David Howells260a9802007-04-26 15:59:35 -0700951 /* validate the parent directory */
David Howellsc435ee32017-11-02 15:27:49 +0000952 afs_validate(dir, key);
David Howells260a9802007-04-26 15:59:35 -0700953
954 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Al Viroa4555892014-10-21 20:11:25 -0400955 _debug("%pd: parent dir deleted", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000956 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
David Howellsa4ff7402018-04-06 14:17:24 +0100959 /* We only need to invalidate a dentry if the server's copy changed
960 * behind our back. If we made the change, it's no problem. Note that
961 * on a 32-bit system, we only have 32 bits in the dentry to store the
962 * version.
963 */
964 dir_version = (long)dir->status.data_version;
965 de_version = (long)dentry->d_fsdata;
966 if (de_version == dir_version)
967 goto out_valid;
968
969 dir_version = (long)dir->invalid_before;
970 if (de_version - dir_version >= 0)
971 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
David Howells260a9802007-04-26 15:59:35 -0700973 _debug("dir modified");
David Howellsd55b4da2018-04-06 14:17:24 +0100974 afs_stat_v(dir, n_reval);
David Howells260a9802007-04-26 15:59:35 -0700975
976 /* search the directory for this vnode */
David Howells5cf9dd52018-04-09 21:12:31 +0100977 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
David Howells260a9802007-04-26 15:59:35 -0700978 switch (ret) {
979 case 0:
980 /* the filename maps to something */
David Howells2b0143b2015-03-17 22:25:59 +0000981 if (d_really_is_negative(dentry))
David Howellsc435ee32017-11-02 15:27:49 +0000982 goto out_bad_parent;
983 inode = d_inode(dentry);
984 if (is_bad_inode(inode)) {
Al Viroa4555892014-10-21 20:11:25 -0400985 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
986 dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000987 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
David Howellsc435ee32017-11-02 15:27:49 +0000990 vnode = AFS_FS_I(inode);
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* if the vnode ID has changed, then the dirent points to a
993 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700994 if (fid.vnode != vnode->fid.vnode) {
Al Viroa4555892014-10-21 20:11:25 -0400995 _debug("%pd: dirent changed [%u != %u]",
996 dentry, fid.vnode,
David Howells08e0e7c2007-04-26 15:55:03 -0700997 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto not_found;
999 }
1000
1001 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -07001002 * been deleted and replaced, and the original vnode ID has
1003 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -07001004 if (fid.unique != vnode->fid.unique) {
Al Viroa4555892014-10-21 20:11:25 -04001005 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1006 dentry, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001007 vnode->fid.unique,
David Howellsc435ee32017-11-02 15:27:49 +00001008 vnode->vfs_inode.i_generation);
1009 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -07001010 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +00001011 write_sequnlock(&vnode->cb_lock);
David Howells260a9802007-04-26 15:59:35 -07001012 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
David Howells260a9802007-04-26 15:59:35 -07001014 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
David Howells260a9802007-04-26 15:59:35 -07001016 case -ENOENT:
1017 /* the filename is unknown */
Al Viroa4555892014-10-21 20:11:25 -04001018 _debug("%pd: dirent not found", dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001019 if (d_really_is_positive(dentry))
David Howells260a9802007-04-26 15:59:35 -07001020 goto not_found;
1021 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -07001022
David Howells260a9802007-04-26 15:59:35 -07001023 default:
Al Viroa4555892014-10-21 20:11:25 -04001024 _debug("failed to iterate dir %pd: %d",
1025 parent, ret);
David Howellsc435ee32017-11-02 15:27:49 +00001026 goto out_bad_parent;
David Howells08e0e7c2007-04-26 15:55:03 -07001027 }
1028
David Howellsec268152007-04-26 15:49:28 -07001029out_valid:
David Howellsa4ff7402018-04-06 14:17:24 +01001030 dentry->d_fsdata = (void *)dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -07001032 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 _leave(" = 1 [valid]");
1034 return 1;
1035
1036 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -07001037not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 spin_lock(&dentry->d_lock);
1039 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1040 spin_unlock(&dentry->d_lock);
1041
David Howellsc435ee32017-11-02 15:27:49 +00001042out_bad_parent:
Al Viroa4555892014-10-21 20:11:25 -04001043 _debug("dropping dentry %pd2", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dput(parent);
David Howellsc435ee32017-11-02 15:27:49 +00001045out_bad:
David Howells00d3b7a2007-04-26 15:57:07 -07001046 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 _leave(" = 0 [bad]");
1049 return 0;
David Howellsec268152007-04-26 15:49:28 -07001050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1054 * sleep)
1055 * - called from dput() when d_count is going to 0.
1056 * - return 1 to request dentry be unhashed, 0 otherwise
1057 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001058static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Al Viroa4555892014-10-21 20:11:25 -04001060 _enter("%pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1063 goto zap;
1064
David Howells2b0143b2015-03-17 22:25:59 +00001065 if (d_really_is_positive(dentry) &&
1066 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1067 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
wangleibec5eb62010-08-11 09:38:04 +01001068 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 _leave(" = 0 [keep]");
1071 return 0;
1072
David Howellsec268152007-04-26 15:49:28 -07001073zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 _leave(" = 1 [zap]");
1075 return 1;
David Howellsec268152007-04-26 15:49:28 -07001076}
David Howells260a9802007-04-26 15:59:35 -07001077
1078/*
1079 * handle dentry release
1080 */
David Howells66c7e1d2018-04-06 14:17:25 +01001081void afs_d_release(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001082{
Al Viroa4555892014-10-21 20:11:25 -04001083 _enter("%pd", dentry);
David Howells260a9802007-04-26 15:59:35 -07001084}
1085
1086/*
David Howellsd2ddc772017-11-02 15:27:50 +00001087 * Create a new inode for create/mkdir/symlink
1088 */
1089static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1090 struct dentry *new_dentry,
1091 struct afs_fid *newfid,
1092 struct afs_file_status *newstatus,
1093 struct afs_callback *newcb)
1094{
1095 struct inode *inode;
1096
1097 if (fc->ac.error < 0)
1098 return;
1099
David Howellsbc1527d2017-11-20 23:04:08 +00001100 d_drop(new_dentry);
1101
David Howellsd2ddc772017-11-02 15:27:50 +00001102 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1103 newfid, newstatus, newcb, fc->cbi);
1104 if (IS_ERR(inode)) {
1105 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1106 * the new directory on the server.
1107 */
1108 fc->ac.error = PTR_ERR(inode);
1109 return;
1110 }
1111
David Howellsbc1527d2017-11-20 23:04:08 +00001112 d_add(new_dentry, inode);
David Howellsd2ddc772017-11-02 15:27:50 +00001113}
1114
1115/*
David Howells260a9802007-04-26 15:59:35 -07001116 * create a directory on an AFS filesystem
1117 */
Al Viro18bb1db2011-07-26 01:41:39 -04001118static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -07001119{
David Howellsd2ddc772017-11-02 15:27:50 +00001120 struct afs_file_status newstatus;
1121 struct afs_fs_cursor fc;
1122 struct afs_callback newcb;
1123 struct afs_vnode *dvnode = AFS_FS_I(dir);
1124 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001125 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001126 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001127 int ret;
1128
David Howellsd2ddc772017-11-02 15:27:50 +00001129 mode |= S_IFDIR;
David Howells260a9802007-04-26 15:59:35 -07001130
Al Viroa4555892014-10-21 20:11:25 -04001131 _enter("{%x:%u},{%pd},%ho",
1132 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001133
David Howells260a9802007-04-26 15:59:35 -07001134 key = afs_request_key(dvnode->volume->cell);
1135 if (IS_ERR(key)) {
1136 ret = PTR_ERR(key);
1137 goto error;
1138 }
1139
David Howellsd2ddc772017-11-02 15:27:50 +00001140 ret = -ERESTARTSYS;
1141 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1142 while (afs_select_fileserver(&fc)) {
1143 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
David Howells63a46812018-04-06 14:17:25 +01001144 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001145 &newfid, &newstatus, &newcb);
1146 }
David Howells260a9802007-04-26 15:59:35 -07001147
David Howellsd2ddc772017-11-02 15:27:50 +00001148 afs_check_for_remote_deletion(&fc, fc.vnode);
1149 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1150 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1151 ret = afs_end_vnode_operation(&fc);
1152 if (ret < 0)
1153 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001154 } else {
1155 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001156 }
1157
David Howells63a46812018-04-06 14:17:25 +01001158 if (ret == 0 &&
1159 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1160 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1161 afs_edit_dir_for_create);
1162
David Howells260a9802007-04-26 15:59:35 -07001163 key_put(key);
1164 _leave(" = 0");
1165 return 0;
1166
David Howellsd2ddc772017-11-02 15:27:50 +00001167error_key:
David Howells260a9802007-04-26 15:59:35 -07001168 key_put(key);
1169error:
1170 d_drop(dentry);
1171 _leave(" = %d", ret);
1172 return ret;
1173}
1174
1175/*
David Howellsd2ddc772017-11-02 15:27:50 +00001176 * Remove a subdir from a directory.
David Howells260a9802007-04-26 15:59:35 -07001177 */
David Howellsd2ddc772017-11-02 15:27:50 +00001178static void afs_dir_remove_subdir(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001179{
David Howells2b0143b2015-03-17 22:25:59 +00001180 if (d_really_is_positive(dentry)) {
David Howellsd2ddc772017-11-02 15:27:50 +00001181 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1182
David Howells260a9802007-04-26 15:59:35 -07001183 clear_nlink(&vnode->vfs_inode);
1184 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +00001185 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
David Howells63a46812018-04-06 14:17:25 +01001186 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
David Howells260a9802007-04-26 15:59:35 -07001187 }
David Howells260a9802007-04-26 15:59:35 -07001188}
1189
1190/*
David Howellsd2ddc772017-11-02 15:27:50 +00001191 * remove a directory from an AFS filesystem
David Howells260a9802007-04-26 15:59:35 -07001192 */
David Howellsd2ddc772017-11-02 15:27:50 +00001193static int afs_rmdir(struct inode *dir, struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001194{
David Howellsd2ddc772017-11-02 15:27:50 +00001195 struct afs_fs_cursor fc;
1196 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells260a9802007-04-26 15:59:35 -07001197 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001198 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001199 int ret;
1200
Al Viroa4555892014-10-21 20:11:25 -04001201 _enter("{%x:%u},{%pd}",
1202 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -07001203
David Howells260a9802007-04-26 15:59:35 -07001204 key = afs_request_key(dvnode->volume->cell);
1205 if (IS_ERR(key)) {
1206 ret = PTR_ERR(key);
1207 goto error;
1208 }
1209
David Howellsd2ddc772017-11-02 15:27:50 +00001210 ret = -ERESTARTSYS;
1211 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1212 while (afs_select_fileserver(&fc)) {
1213 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
David Howells63a46812018-04-06 14:17:25 +01001214 afs_fs_remove(&fc, dentry->d_name.name, true,
1215 data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001216 }
David Howells260a9802007-04-26 15:59:35 -07001217
David Howellsd2ddc772017-11-02 15:27:50 +00001218 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1219 ret = afs_end_vnode_operation(&fc);
David Howells63a46812018-04-06 14:17:25 +01001220 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +00001221 afs_dir_remove_subdir(dentry);
David Howells63a46812018-04-06 14:17:25 +01001222 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1223 afs_edit_dir_remove(dvnode, &dentry->d_name,
1224 afs_edit_dir_for_rmdir);
1225 }
David Howells260a9802007-04-26 15:59:35 -07001226 }
1227
1228 key_put(key);
David Howellsd2ddc772017-11-02 15:27:50 +00001229error:
1230 return ret;
1231}
David Howells260a9802007-04-26 15:59:35 -07001232
David Howellsd2ddc772017-11-02 15:27:50 +00001233/*
1234 * Remove a link to a file or symlink from a directory.
1235 *
1236 * If the file was not deleted due to excess hard links, the fileserver will
1237 * break the callback promise on the file - if it had one - before it returns
1238 * to us, and if it was deleted, it won't
1239 *
1240 * However, if we didn't have a callback promise outstanding, or it was
1241 * outstanding on a different server, then it won't break it either...
1242 */
David Howells440fbc32018-01-02 10:02:19 +00001243static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1244 unsigned long d_version_before,
1245 unsigned long d_version_after)
David Howellsd2ddc772017-11-02 15:27:50 +00001246{
David Howells440fbc32018-01-02 10:02:19 +00001247 bool dir_valid;
David Howellsd2ddc772017-11-02 15:27:50 +00001248 int ret = 0;
1249
David Howells440fbc32018-01-02 10:02:19 +00001250 /* There were no intervening changes on the server if the version
1251 * number we got back was incremented by exactly 1.
1252 */
1253 dir_valid = (d_version_after == d_version_before + 1);
1254
David Howellsd2ddc772017-11-02 15:27:50 +00001255 if (d_really_is_positive(dentry)) {
1256 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1257
David Howells440fbc32018-01-02 10:02:19 +00001258 if (dir_valid) {
1259 drop_nlink(&vnode->vfs_inode);
1260 if (vnode->vfs_inode.i_nlink == 0) {
1261 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1262 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1263 }
David Howellsd2ddc772017-11-02 15:27:50 +00001264 ret = 0;
David Howells440fbc32018-01-02 10:02:19 +00001265 } else {
1266 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1267
1268 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1269 kdebug("AFS_VNODE_DELETED");
1270
1271 ret = afs_validate(vnode, key);
1272 if (ret == -ESTALE)
1273 ret = 0;
1274 }
David Howellsd2ddc772017-11-02 15:27:50 +00001275 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1276 }
1277
1278 return ret;
1279}
1280
1281/*
1282 * Remove a file or symlink from an AFS filesystem.
1283 */
1284static int afs_unlink(struct inode *dir, struct dentry *dentry)
1285{
1286 struct afs_fs_cursor fc;
1287 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
1288 struct key *key;
David Howells440fbc32018-01-02 10:02:19 +00001289 unsigned long d_version = (unsigned long)dentry->d_fsdata;
David Howells63a46812018-04-06 14:17:25 +01001290 u64 data_version = dvnode->status.data_version;
David Howellsd2ddc772017-11-02 15:27:50 +00001291 int ret;
1292
1293 _enter("{%x:%u},{%pd}",
1294 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1295
1296 if (dentry->d_name.len >= AFSNAMEMAX)
1297 return -ENAMETOOLONG;
1298
1299 key = afs_request_key(dvnode->volume->cell);
1300 if (IS_ERR(key)) {
1301 ret = PTR_ERR(key);
1302 goto error;
1303 }
1304
1305 /* Try to make sure we have a callback promise on the victim. */
1306 if (d_really_is_positive(dentry)) {
1307 vnode = AFS_FS_I(d_inode(dentry));
1308 ret = afs_validate(vnode, key);
1309 if (ret < 0)
1310 goto error_key;
1311 }
1312
1313 ret = -ERESTARTSYS;
1314 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1315 while (afs_select_fileserver(&fc)) {
1316 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
David Howells63a46812018-04-06 14:17:25 +01001317 afs_fs_remove(&fc, dentry->d_name.name, false,
1318 data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001319 }
1320
1321 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1322 ret = afs_end_vnode_operation(&fc);
1323 if (ret == 0)
David Howells440fbc32018-01-02 10:02:19 +00001324 ret = afs_dir_remove_link(
1325 dentry, key, d_version,
1326 (unsigned long)dvnode->status.data_version);
David Howells63a46812018-04-06 14:17:25 +01001327 if (ret == 0 &&
1328 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1329 afs_edit_dir_remove(dvnode, &dentry->d_name,
1330 afs_edit_dir_for_unlink);
David Howellsd2ddc772017-11-02 15:27:50 +00001331 }
1332
1333error_key:
David Howells260a9802007-04-26 15:59:35 -07001334 key_put(key);
1335error:
1336 _leave(" = %d", ret);
1337 return ret;
1338}
1339
1340/*
1341 * create a regular file on an AFS filesystem
1342 */
Al Viro4acdaf22011-07-26 01:42:34 -04001343static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04001344 bool excl)
David Howells260a9802007-04-26 15:59:35 -07001345{
David Howellsd2ddc772017-11-02 15:27:50 +00001346 struct afs_fs_cursor fc;
1347 struct afs_file_status newstatus;
1348 struct afs_callback newcb;
Colin Ian King43dd3882017-11-20 13:58:20 +00001349 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howellsd2ddc772017-11-02 15:27:50 +00001350 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001351 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001352 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001353 int ret;
1354
David Howellsd2ddc772017-11-02 15:27:50 +00001355 mode |= S_IFREG;
David Howells260a9802007-04-26 15:59:35 -07001356
Al Viroa4555892014-10-21 20:11:25 -04001357 _enter("{%x:%u},{%pd},%ho,",
1358 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001359
David Howellsd2ddc772017-11-02 15:27:50 +00001360 ret = -ENAMETOOLONG;
1361 if (dentry->d_name.len >= AFSNAMEMAX)
1362 goto error;
1363
David Howells260a9802007-04-26 15:59:35 -07001364 key = afs_request_key(dvnode->volume->cell);
1365 if (IS_ERR(key)) {
1366 ret = PTR_ERR(key);
1367 goto error;
1368 }
1369
David Howellsd2ddc772017-11-02 15:27:50 +00001370 ret = -ERESTARTSYS;
1371 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1372 while (afs_select_fileserver(&fc)) {
1373 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
David Howells63a46812018-04-06 14:17:25 +01001374 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001375 &newfid, &newstatus, &newcb);
1376 }
David Howells260a9802007-04-26 15:59:35 -07001377
David Howellsd2ddc772017-11-02 15:27:50 +00001378 afs_check_for_remote_deletion(&fc, fc.vnode);
1379 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1380 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1381 ret = afs_end_vnode_operation(&fc);
1382 if (ret < 0)
1383 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001384 } else {
1385 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001386 }
1387
David Howells63a46812018-04-06 14:17:25 +01001388 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1389 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1390 afs_edit_dir_for_create);
1391
David Howells260a9802007-04-26 15:59:35 -07001392 key_put(key);
1393 _leave(" = 0");
1394 return 0;
1395
David Howellsd2ddc772017-11-02 15:27:50 +00001396error_key:
David Howells260a9802007-04-26 15:59:35 -07001397 key_put(key);
1398error:
1399 d_drop(dentry);
1400 _leave(" = %d", ret);
1401 return ret;
1402}
1403
1404/*
1405 * create a hard link between files in an AFS filesystem
1406 */
1407static int afs_link(struct dentry *from, struct inode *dir,
1408 struct dentry *dentry)
1409{
David Howellsd2ddc772017-11-02 15:27:50 +00001410 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001411 struct afs_vnode *dvnode, *vnode;
1412 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001413 u64 data_version;
David Howells260a9802007-04-26 15:59:35 -07001414 int ret;
1415
David Howells2b0143b2015-03-17 22:25:59 +00001416 vnode = AFS_FS_I(d_inode(from));
David Howells260a9802007-04-26 15:59:35 -07001417 dvnode = AFS_FS_I(dir);
David Howells63a46812018-04-06 14:17:25 +01001418 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001419
Al Viroa4555892014-10-21 20:11:25 -04001420 _enter("{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001421 vnode->fid.vid, vnode->fid.vnode,
1422 dvnode->fid.vid, dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001423 dentry);
David Howells260a9802007-04-26 15:59:35 -07001424
David Howellsd2ddc772017-11-02 15:27:50 +00001425 ret = -ENAMETOOLONG;
1426 if (dentry->d_name.len >= AFSNAMEMAX)
1427 goto error;
1428
David Howells260a9802007-04-26 15:59:35 -07001429 key = afs_request_key(dvnode->volume->cell);
1430 if (IS_ERR(key)) {
1431 ret = PTR_ERR(key);
1432 goto error;
1433 }
1434
David Howellsd2ddc772017-11-02 15:27:50 +00001435 ret = -ERESTARTSYS;
1436 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1437 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1438 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001439 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001440 }
David Howells260a9802007-04-26 15:59:35 -07001441
David Howellsd2ddc772017-11-02 15:27:50 +00001442 while (afs_select_fileserver(&fc)) {
1443 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1444 fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break;
David Howells63a46812018-04-06 14:17:25 +01001445 afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001446 }
1447
1448 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1449 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1450 ihold(&vnode->vfs_inode);
1451 d_instantiate(dentry, &vnode->vfs_inode);
1452
1453 mutex_unlock(&vnode->io_lock);
1454 ret = afs_end_vnode_operation(&fc);
1455 if (ret < 0)
1456 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001457 } else {
1458 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001459 }
1460
David Howells63a46812018-04-06 14:17:25 +01001461 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1462 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1463 afs_edit_dir_for_link);
1464
David Howells260a9802007-04-26 15:59:35 -07001465 key_put(key);
1466 _leave(" = 0");
1467 return 0;
1468
David Howellsd2ddc772017-11-02 15:27:50 +00001469error_key:
David Howells260a9802007-04-26 15:59:35 -07001470 key_put(key);
1471error:
1472 d_drop(dentry);
1473 _leave(" = %d", ret);
1474 return ret;
1475}
1476
1477/*
1478 * create a symlink in an AFS filesystem
1479 */
1480static int afs_symlink(struct inode *dir, struct dentry *dentry,
1481 const char *content)
1482{
David Howellsd2ddc772017-11-02 15:27:50 +00001483 struct afs_fs_cursor fc;
1484 struct afs_file_status newstatus;
1485 struct afs_vnode *dvnode = AFS_FS_I(dir);
1486 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001487 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001488 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001489 int ret;
1490
Al Viroa4555892014-10-21 20:11:25 -04001491 _enter("{%x:%u},{%pd},%s",
1492 dvnode->fid.vid, dvnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -07001493 content);
1494
David Howellsd2ddc772017-11-02 15:27:50 +00001495 ret = -ENAMETOOLONG;
1496 if (dentry->d_name.len >= AFSNAMEMAX)
1497 goto error;
1498
David Howells260a9802007-04-26 15:59:35 -07001499 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001500 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001501 goto error;
1502
1503 key = afs_request_key(dvnode->volume->cell);
1504 if (IS_ERR(key)) {
1505 ret = PTR_ERR(key);
1506 goto error;
1507 }
1508
David Howellsd2ddc772017-11-02 15:27:50 +00001509 ret = -ERESTARTSYS;
1510 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1511 while (afs_select_fileserver(&fc)) {
1512 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
David Howells63a46812018-04-06 14:17:25 +01001513 afs_fs_symlink(&fc, dentry->d_name.name,
1514 content, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001515 &newfid, &newstatus);
1516 }
David Howells260a9802007-04-26 15:59:35 -07001517
David Howellsd2ddc772017-11-02 15:27:50 +00001518 afs_check_for_remote_deletion(&fc, fc.vnode);
1519 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1520 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1521 ret = afs_end_vnode_operation(&fc);
1522 if (ret < 0)
1523 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001524 } else {
1525 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001526 }
1527
David Howells63a46812018-04-06 14:17:25 +01001528 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1529 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1530 afs_edit_dir_for_symlink);
1531
David Howells260a9802007-04-26 15:59:35 -07001532 key_put(key);
1533 _leave(" = 0");
1534 return 0;
1535
David Howellsd2ddc772017-11-02 15:27:50 +00001536error_key:
David Howells260a9802007-04-26 15:59:35 -07001537 key_put(key);
1538error:
1539 d_drop(dentry);
1540 _leave(" = %d", ret);
1541 return ret;
1542}
1543
1544/*
1545 * rename a file in an AFS filesystem and/or move it between directories
1546 */
1547static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001548 struct inode *new_dir, struct dentry *new_dentry,
1549 unsigned int flags)
David Howells260a9802007-04-26 15:59:35 -07001550{
David Howellsd2ddc772017-11-02 15:27:50 +00001551 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001552 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1553 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001554 u64 orig_data_version, new_data_version;
1555 bool new_negative = d_is_negative(new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001556 int ret;
1557
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001558 if (flags)
1559 return -EINVAL;
1560
David Howells2b0143b2015-03-17 22:25:59 +00001561 vnode = AFS_FS_I(d_inode(old_dentry));
David Howells260a9802007-04-26 15:59:35 -07001562 orig_dvnode = AFS_FS_I(old_dir);
1563 new_dvnode = AFS_FS_I(new_dir);
David Howells63a46812018-04-06 14:17:25 +01001564 orig_data_version = orig_dvnode->status.data_version;
1565 new_data_version = new_dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001566
Al Viroa4555892014-10-21 20:11:25 -04001567 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001568 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1569 vnode->fid.vid, vnode->fid.vnode,
1570 new_dvnode->fid.vid, new_dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001571 new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001572
David Howells260a9802007-04-26 15:59:35 -07001573 key = afs_request_key(orig_dvnode->volume->cell);
1574 if (IS_ERR(key)) {
1575 ret = PTR_ERR(key);
1576 goto error;
1577 }
1578
David Howellsd2ddc772017-11-02 15:27:50 +00001579 ret = -ERESTARTSYS;
1580 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1581 if (orig_dvnode != new_dvnode) {
1582 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1583 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001584 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001585 }
1586 }
1587 while (afs_select_fileserver(&fc)) {
1588 fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break;
1589 fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break;
1590 afs_fs_rename(&fc, old_dentry->d_name.name,
David Howells63a46812018-04-06 14:17:25 +01001591 new_dvnode, new_dentry->d_name.name,
1592 orig_data_version, new_data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001593 }
1594
1595 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1596 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1597 if (orig_dvnode != new_dvnode)
1598 mutex_unlock(&new_dvnode->io_lock);
1599 ret = afs_end_vnode_operation(&fc);
1600 if (ret < 0)
1601 goto error_key;
1602 }
1603
David Howells63a46812018-04-06 14:17:25 +01001604 if (ret == 0) {
1605 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1606 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1607 afs_edit_dir_for_rename);
1608
1609 if (!new_negative &&
1610 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1611 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1612 afs_edit_dir_for_rename);
1613
1614 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1615 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1616 &vnode->fid, afs_edit_dir_for_rename);
1617 }
1618
David Howellsd2ddc772017-11-02 15:27:50 +00001619error_key:
David Howells260a9802007-04-26 15:59:35 -07001620 key_put(key);
1621error:
David Howells260a9802007-04-26 15:59:35 -07001622 _leave(" = %d", ret);
1623 return ret;
1624}
David Howellsf3ddee82018-04-06 14:17:25 +01001625
1626/*
1627 * Release a directory page and clean up its private state if it's not busy
1628 * - return true if the page can now be released, false if not
1629 */
1630static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1631{
1632 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1633
1634 _enter("{{%x:%u}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1635
1636 set_page_private(page, 0);
1637 ClearPagePrivate(page);
1638
1639 /* The directory will need reloading. */
1640 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1641 afs_stat_v(dvnode, n_relpg);
1642 return 1;
1643}
1644
1645/*
1646 * invalidate part or all of a page
1647 * - release a page and clean up its private data if offset is 0 (indicating
1648 * the entire page)
1649 */
1650static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1651 unsigned int length)
1652{
1653 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1654
1655 _enter("{%lu},%u,%u", page->index, offset, length);
1656
1657 BUG_ON(!PageLocked(page));
1658
1659 /* The directory will need reloading. */
1660 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1661 afs_stat_v(dvnode, n_inval);
1662
1663 /* we clean up only if the entire page is being invalidated */
1664 if (offset == 0 && length == PAGE_SIZE) {
1665 set_page_private(page, 0);
1666 ClearPagePrivate(page);
1667 }
1668}