blob: 54e7f6f1405e298db38aafae45a499b41519dfd4 [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)
David Howellsb61f7dc2018-04-27 20:46:22 +0100183 __acquires(&dvnode->validate_lock)
David Howellsf3ddee82018-04-06 14:17:25 +0100184{
185 struct afs_read *req;
186 loff_t i_size;
187 int nr_pages, nr_inline, i, n;
188 int ret = -ENOMEM;
189
190retry:
191 i_size = i_size_read(&dvnode->vfs_inode);
192 if (i_size < 2048)
193 return ERR_PTR(-EIO);
194 if (i_size > 2048 * 1024)
195 return ERR_PTR(-EFBIG);
196
197 _enter("%llu", i_size);
198
199 /* Get a request record to hold the page list. We want to hold it
200 * inline if we can, but we don't want to make an order 1 allocation.
201 */
202 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
203 nr_inline = nr_pages;
204 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
205 nr_inline = 0;
206
207 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
208 GFP_KERNEL);
209 if (!req)
210 return ERR_PTR(-ENOMEM);
211
212 refcount_set(&req->usage, 1);
213 req->nr_pages = nr_pages;
214 req->actual_len = i_size; /* May change */
215 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
216 req->data_version = dvnode->status.data_version; /* May change */
217 if (nr_inline > 0) {
218 req->pages = req->array;
219 } else {
220 req->pages = kcalloc(nr_pages, sizeof(struct page *),
221 GFP_KERNEL);
222 if (!req->pages)
223 goto error;
224 }
225
226 /* Get a list of all the pages that hold or will hold the directory
227 * content. We need to fill in any gaps that we might find where the
228 * memory reclaimer has been at work. If there are any gaps, we will
229 * need to reread the entire directory contents.
230 */
231 i = 0;
232 do {
233 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
234 req->nr_pages - i,
235 req->pages + i);
236 _debug("find %u at %u/%u", n, i, req->nr_pages);
237 if (n == 0) {
238 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
239
240 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
241 afs_stat_v(dvnode, n_inval);
242
243 ret = -ENOMEM;
244 req->pages[i] = __page_cache_alloc(gfp);
245 if (!req->pages[i])
246 goto error;
247 ret = add_to_page_cache_lru(req->pages[i],
248 dvnode->vfs_inode.i_mapping,
249 i, gfp);
250 if (ret < 0)
251 goto error;
252
253 set_page_private(req->pages[i], 1);
254 SetPagePrivate(req->pages[i]);
255 unlock_page(req->pages[i]);
256 i++;
257 } else {
258 i += n;
259 }
260 } while (i < req->nr_pages);
261
262 /* If we're going to reload, we need to lock all the pages to prevent
263 * races.
264 */
David Howellsb61f7dc2018-04-27 20:46:22 +0100265 ret = -ERESTARTSYS;
266 if (down_read_killable(&dvnode->validate_lock) < 0)
267 goto error;
268
269 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
270 goto success;
271
272 up_read(&dvnode->validate_lock);
273 if (down_write_killable(&dvnode->validate_lock) < 0)
274 goto error;
275
David Howellsf3ddee82018-04-06 14:17:25 +0100276 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
David Howellsf3ddee82018-04-06 14:17:25 +0100277 ret = afs_fetch_data(dvnode, key, req);
278 if (ret < 0)
David Howellsb61f7dc2018-04-27 20:46:22 +0100279 goto error_unlock;
David Howellsf3ddee82018-04-06 14:17:25 +0100280
281 task_io_account_read(PAGE_SIZE * req->nr_pages);
282
283 if (req->len < req->file_size)
284 goto content_has_grown;
285
286 /* Validate the data we just read. */
287 ret = -EIO;
288 for (i = 0; i < req->nr_pages; i++)
289 if (!afs_dir_check_page(dvnode, req->pages[i],
290 req->actual_len))
David Howellsb61f7dc2018-04-27 20:46:22 +0100291 goto error_unlock;
David Howellsf3ddee82018-04-06 14:17:25 +0100292
293 // TODO: Trim excess pages
294
295 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
296 }
297
David Howellsb61f7dc2018-04-27 20:46:22 +0100298 downgrade_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100299success:
David Howellsf3ddee82018-04-06 14:17:25 +0100300 return req;
301
David Howellsf3ddee82018-04-06 14:17:25 +0100302error_unlock:
David Howellsb61f7dc2018-04-27 20:46:22 +0100303 up_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100304error:
305 afs_put_read(req);
306 _leave(" = %d", ret);
307 return ERR_PTR(ret);
308
309content_has_grown:
David Howellsb61f7dc2018-04-27 20:46:22 +0100310 up_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100311 afs_put_read(req);
312 goto retry;
313}
314
315/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * deal with one block in an AFS directory
317 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400318static int afs_dir_iterate_block(struct dir_context *ctx,
David Howells00317632018-04-06 14:17:25 +0100319 union afs_xdr_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400320 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
David Howells00317632018-04-06 14:17:25 +0100322 union afs_xdr_dirent *dire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned offset, next, curr;
324 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400325 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Al Viro1bbae9f2013-05-22 16:31:14 -0400327 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Howells00317632018-04-06 14:17:25 +0100329 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* walk through the block, an entry at a time */
David Howells4ea219a2018-04-06 14:17:25 +0100332 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
333 offset < AFS_DIR_SLOTS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 offset = next
335 ) {
336 next = offset + 1;
337
338 /* skip entries marked unused in the bitmap */
David Howells00317632018-04-06 14:17:25 +0100339 if (!(block->hdr.bitmap[offset / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 (1 << (offset % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800341 _debug("ENT[%zu.%u]: unused",
David Howells00317632018-04-06 14:17:25 +0100342 blkoff / sizeof(union afs_xdr_dir_block), offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (offset >= curr)
Al Viro1bbae9f2013-05-22 16:31:14 -0400344 ctx->pos = blkoff +
David Howells00317632018-04-06 14:17:25 +0100345 next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 continue;
347 }
348
349 /* got a valid entry */
350 dire = &block->dirents[offset];
351 nlen = strnlen(dire->u.name,
352 sizeof(*block) -
David Howells00317632018-04-06 14:17:25 +0100353 offset * sizeof(union afs_xdr_dirent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800355 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
David Howells00317632018-04-06 14:17:25 +0100356 blkoff / sizeof(union afs_xdr_dir_block), offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 (offset < curr ? "skip" : "fill"),
358 nlen, dire->u.name);
359
360 /* work out where the next possible entry is */
David Howells00317632018-04-06 14:17:25 +0100361 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
David Howells4ea219a2018-04-06 14:17:25 +0100362 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800363 _debug("ENT[%zu.%u]:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 " %u travelled beyond end dir block"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800365 " (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100366 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 offset, next, tmp, nlen);
368 return -EIO;
369 }
David Howells00317632018-04-06 14:17:25 +0100370 if (!(block->hdr.bitmap[next / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 (1 << (next % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800372 _debug("ENT[%zu.%u]:"
373 " %u unmarked extension (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100374 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 offset, next, tmp, nlen);
376 return -EIO;
377 }
378
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800379 _debug("ENT[%zu.%u]: ext %u/%zu",
David Howells00317632018-04-06 14:17:25 +0100380 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 next, tmp, nlen);
382 next++;
383 }
384
385 /* skip if starts before the current position */
386 if (offset < curr)
387 continue;
388
389 /* found the next entry */
Al Viro1bbae9f2013-05-22 16:31:14 -0400390 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ntohl(dire->u.vnode),
David Howells5cf9dd52018-04-09 21:12:31 +0100392 (ctx->actor == afs_lookup_filldir ||
393 ctx->actor == afs_lookup_one_filldir)?
Al Viro1bbae9f2013-05-22 16:31:14 -0400394 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 _leave(" = 0 [full]");
396 return 0;
397 }
398
David Howells00317632018-04-06 14:17:25 +0100399 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 _leave(" = 1 [more]");
403 return 1;
David Howellsec268152007-04-26 15:49:28 -0700404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
David Howells08e0e7c2007-04-26 15:55:03 -0700407 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400409static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
410 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
David Howellsf3ddee82018-04-06 14:17:25 +0100412 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells00317632018-04-06 14:17:25 +0100413 struct afs_xdr_dir_page *dbuf;
414 union afs_xdr_dir_block *dblock;
David Howellsf3ddee82018-04-06 14:17:25 +0100415 struct afs_read *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct page *page;
417 unsigned blkoff, limit;
418 int ret;
419
Al Viro1bbae9f2013-05-22 16:31:14 -0400420 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David Howells08e0e7c2007-04-26 15:55:03 -0700422 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 _leave(" = -ESTALE");
424 return -ESTALE;
425 }
426
David Howellsf3ddee82018-04-06 14:17:25 +0100427 req = afs_read_dir(dvnode, key);
428 if (IS_ERR(req))
429 return PTR_ERR(req);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* round the file position up to the next entry boundary */
David Howells00317632018-04-06 14:17:25 +0100432 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
433 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* walk through the blocks in sequence */
436 ret = 0;
David Howellsf3ddee82018-04-06 14:17:25 +0100437 while (ctx->pos < req->actual_len) {
David Howells00317632018-04-06 14:17:25 +0100438 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David Howellsf3ddee82018-04-06 14:17:25 +0100440 /* Fetch the appropriate page from the directory and re-add it
441 * to the LRU.
442 */
443 page = req->pages[blkoff / PAGE_SIZE];
444 if (!page) {
445 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447 }
David Howellsf3ddee82018-04-06 14:17:25 +0100448 mark_page_accessed(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 limit = blkoff & ~(PAGE_SIZE - 1);
451
David Howellsf3ddee82018-04-06 14:17:25 +0100452 dbuf = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* deal with the individual blocks stashed on this page */
455 do {
456 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
David Howells00317632018-04-06 14:17:25 +0100457 sizeof(union afs_xdr_dir_block)];
Al Viro1bbae9f2013-05-22 16:31:14 -0400458 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (ret != 1) {
David Howellsf3ddee82018-04-06 14:17:25 +0100460 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto out;
462 }
463
David Howells00317632018-04-06 14:17:25 +0100464 blkoff += sizeof(union afs_xdr_dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Al Viro1bbae9f2013-05-22 16:31:14 -0400466 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David Howellsf3ddee82018-04-06 14:17:25 +0100468 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ret = 0;
470 }
471
David Howellsec268152007-04-26 15:49:28 -0700472out:
David Howellsb61f7dc2018-04-27 20:46:22 +0100473 up_read(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100474 afs_put_read(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 _leave(" = %d", ret);
476 return ret;
David Howellsec268152007-04-26 15:49:28 -0700477}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
480 * read an AFS directory
481 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400482static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
David Howells215804a2017-11-02 15:27:52 +0000484 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
David Howellsec268152007-04-26 15:49:28 -0700485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
David Howells5cf9dd52018-04-09 21:12:31 +0100488 * Search the directory for a single name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
490 * uniquifier through dtype
491 */
David Howells5cf9dd52018-04-09 21:12:31 +0100492static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
493 int nlen, loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
David Howells5cf9dd52018-04-09 21:12:31 +0100495 struct afs_lookup_one_cookie *cookie =
496 container_of(ctx, struct afs_lookup_one_cookie, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Al Viro1bbae9f2013-05-22 16:31:14 -0400498 _enter("{%s,%u},%s,%u,,%llu,%u",
499 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700500 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David Howells08e0e7c2007-04-26 15:55:03 -0700502 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100503 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
504 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells08e0e7c2007-04-26 15:55:03 -0700505
Al Viro1bbae9f2013-05-22 16:31:14 -0400506 if (cookie->name.len != nlen ||
507 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 _leave(" = 0 [no]");
509 return 0;
510 }
511
512 cookie->fid.vnode = ino;
513 cookie->fid.unique = dtype;
514 cookie->found = 1;
515
516 _leave(" = -1 [found]");
517 return -1;
David Howellsec268152007-04-26 15:49:28 -0700518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
David Howells5cf9dd52018-04-09 21:12:31 +0100521 * Do a lookup of a single name in a directory
David Howells260a9802007-04-26 15:59:35 -0700522 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
David Howells5cf9dd52018-04-09 21:12:31 +0100524static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
525 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Al Viro1bbae9f2013-05-22 16:31:14 -0400527 struct afs_super_info *as = dir->i_sb->s_fs_info;
David Howells5cf9dd52018-04-09 21:12:31 +0100528 struct afs_lookup_one_cookie cookie = {
529 .ctx.actor = afs_lookup_one_filldir,
Al Viro1bbae9f2013-05-22 16:31:14 -0400530 .name = dentry->d_name,
531 .fid.vid = as->volume->vid
532 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int ret;
534
Al Viroa4555892014-10-21 20:11:25 -0400535 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400538 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700540 _leave(" = %d [iter]", ret);
541 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 ret = -ENOENT;
545 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700546 _leave(" = -ENOENT [not found]");
547 return -ENOENT;
548 }
549
550 *fid = cookie.fid;
551 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
552 return 0;
553}
554
555/*
David Howells5cf9dd52018-04-09 21:12:31 +0100556 * search the directory for a name
557 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
558 * uniquifier through dtype
559 */
560static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
561 int nlen, loff_t fpos, u64 ino, unsigned dtype)
562{
563 struct afs_lookup_cookie *cookie =
564 container_of(ctx, struct afs_lookup_cookie, ctx);
565 int ret;
566
567 _enter("{%s,%u},%s,%u,,%llu,%u",
568 cookie->name.name, cookie->name.len, name, nlen,
569 (unsigned long long) ino, dtype);
570
571 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100572 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
573 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells5cf9dd52018-04-09 21:12:31 +0100574
575 if (cookie->found) {
576 if (cookie->nr_fids < 50) {
577 cookie->fids[cookie->nr_fids].vnode = ino;
578 cookie->fids[cookie->nr_fids].unique = dtype;
579 cookie->nr_fids++;
580 }
581 } else if (cookie->name.len == nlen &&
582 memcmp(cookie->name.name, name, nlen) == 0) {
583 cookie->fids[0].vnode = ino;
584 cookie->fids[0].unique = dtype;
585 cookie->found = 1;
586 if (cookie->one_only)
587 return -1;
588 }
589
590 ret = cookie->nr_fids >= 50 ? -1 : 0;
591 _leave(" = %d", ret);
592 return ret;
593}
594
595/*
596 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
597 * files in one go and create inodes for them. The inode of the file we were
598 * asked for is returned.
599 */
600static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
601 struct key *key)
602{
603 struct afs_lookup_cookie *cookie;
604 struct afs_cb_interest *cbi = NULL;
605 struct afs_super_info *as = dir->i_sb->s_fs_info;
606 struct afs_iget_data data;
607 struct afs_fs_cursor fc;
608 struct afs_vnode *dvnode = AFS_FS_I(dir);
609 struct inode *inode = NULL;
610 int ret, i;
611
612 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
613
614 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
615 if (!cookie)
616 return ERR_PTR(-ENOMEM);
617
618 cookie->ctx.actor = afs_lookup_filldir;
619 cookie->name = dentry->d_name;
620 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
621
622 read_seqlock_excl(&dvnode->cb_lock);
623 if (dvnode->cb_interest &&
624 dvnode->cb_interest->server &&
625 test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
626 cookie->one_only = true;
627 read_sequnlock_excl(&dvnode->cb_lock);
628
629 for (i = 0; i < 50; i++)
630 cookie->fids[i].vid = as->volume->vid;
631
632 /* search the directory */
633 ret = afs_dir_iterate(dir, &cookie->ctx, key);
634 if (ret < 0) {
635 inode = ERR_PTR(ret);
636 goto out;
637 }
638
639 inode = ERR_PTR(-ENOENT);
640 if (!cookie->found)
641 goto out;
642
643 /* Check to see if we already have an inode for the primary fid. */
644 data.volume = dvnode->volume;
645 data.fid = cookie->fids[0];
646 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
647 if (inode)
648 goto out;
649
650 /* Need space for examining all the selected files */
651 inode = ERR_PTR(-ENOMEM);
652 cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
653 GFP_KERNEL);
654 if (!cookie->statuses)
655 goto out;
656
657 cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
658 GFP_KERNEL);
659 if (!cookie->callbacks)
660 goto out_s;
661
662 /* Try FS.InlineBulkStatus first. Abort codes for the individual
663 * lookups contained therein are stored in the reply without aborting
664 * the whole operation.
665 */
666 if (cookie->one_only)
667 goto no_inline_bulk_status;
668
669 inode = ERR_PTR(-ERESTARTSYS);
670 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
671 while (afs_select_fileserver(&fc)) {
672 if (test_bit(AFS_SERVER_FL_NO_IBULK,
673 &fc.cbi->server->flags)) {
674 fc.ac.abort_code = RX_INVALID_OPERATION;
675 fc.ac.error = -ECONNABORTED;
676 break;
677 }
678 afs_fs_inline_bulk_status(&fc,
679 afs_v2net(dvnode),
680 cookie->fids,
681 cookie->statuses,
682 cookie->callbacks,
683 cookie->nr_fids, NULL);
684 }
685
686 if (fc.ac.error == 0)
687 cbi = afs_get_cb_interest(fc.cbi);
688 if (fc.ac.abort_code == RX_INVALID_OPERATION)
689 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
690 inode = ERR_PTR(afs_end_vnode_operation(&fc));
691 }
692
693 if (!IS_ERR(inode))
694 goto success;
695 if (fc.ac.abort_code != RX_INVALID_OPERATION)
696 goto out_c;
697
698no_inline_bulk_status:
699 /* We could try FS.BulkStatus next, but this aborts the entire op if
700 * any of the lookups fails - so, for the moment, revert to
701 * FS.FetchStatus for just the primary fid.
702 */
703 cookie->nr_fids = 1;
704 inode = ERR_PTR(-ERESTARTSYS);
705 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
706 while (afs_select_fileserver(&fc)) {
707 afs_fs_fetch_status(&fc,
708 afs_v2net(dvnode),
709 cookie->fids,
710 cookie->statuses,
711 cookie->callbacks,
712 NULL);
713 }
714
715 if (fc.ac.error == 0)
716 cbi = afs_get_cb_interest(fc.cbi);
717 inode = ERR_PTR(afs_end_vnode_operation(&fc));
718 }
719
720 if (IS_ERR(inode))
721 goto out_c;
722
723 for (i = 0; i < cookie->nr_fids; i++)
724 cookie->statuses[i].abort_code = 0;
725
726success:
727 /* Turn all the files into inodes and save the first one - which is the
728 * one we actually want.
729 */
730 if (cookie->statuses[0].abort_code != 0)
731 inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
732
733 for (i = 0; i < cookie->nr_fids; i++) {
734 struct inode *ti;
735
736 if (cookie->statuses[i].abort_code != 0)
737 continue;
738
739 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
740 &cookie->statuses[i],
741 &cookie->callbacks[i],
742 cbi);
743 if (i == 0) {
744 inode = ti;
745 } else {
746 if (!IS_ERR(ti))
747 iput(ti);
748 }
749 }
750
751out_c:
752 afs_put_cb_interest(afs_v2net(dvnode), cbi);
753 kfree(cookie->callbacks);
754out_s:
755 kfree(cookie->statuses);
756out:
757 kfree(cookie);
758 return inode;
759}
760
761/*
David Howells6f8880d2018-04-09 21:12:31 +0100762 * Look up an entry in a directory with @sys substitution.
763 */
764static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
765 struct key *key)
766{
767 struct afs_sysnames *subs;
768 struct afs_net *net = afs_i2net(dir);
769 struct dentry *ret;
770 char *buf, *p, *name;
771 int len, i;
772
773 _enter("");
774
775 ret = ERR_PTR(-ENOMEM);
776 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
777 if (!buf)
778 goto out_p;
779 if (dentry->d_name.len > 4) {
780 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
781 p += dentry->d_name.len - 4;
782 }
783
784 /* There is an ordered list of substitutes that we have to try. */
785 read_lock(&net->sysnames_lock);
786 subs = net->sysnames;
787 refcount_inc(&subs->usage);
788 read_unlock(&net->sysnames_lock);
789
790 for (i = 0; i < subs->nr; i++) {
791 name = subs->subs[i];
792 len = dentry->d_name.len - 4 + strlen(name);
793 if (len >= AFSNAMEMAX) {
794 ret = ERR_PTR(-ENAMETOOLONG);
795 goto out_s;
796 }
797
798 strcpy(p, name);
799 ret = lookup_one_len(buf, dentry->d_parent, len);
800 if (IS_ERR(ret) || d_is_positive(ret))
801 goto out_s;
802 dput(ret);
803 }
804
805 /* We don't want to d_add() the @sys dentry here as we don't want to
806 * the cached dentry to hide changes to the sysnames list.
807 */
808 ret = NULL;
809out_s:
810 afs_put_sysnames(subs);
811 kfree(buf);
812out_p:
813 key_put(key);
814 return ret;
815}
816
817/*
David Howells08e0e7c2007-04-26 15:55:03 -0700818 * look up an entry in a directory
819 */
David Howells260a9802007-04-26 15:59:35 -0700820static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400821 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700822{
David Howells5cf9dd52018-04-09 21:12:31 +0100823 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells08e0e7c2007-04-26 15:55:03 -0700824 struct inode *inode;
Al Viro34b2a882018-06-24 10:43:51 -0400825 struct dentry *d;
David Howells00d3b7a2007-04-26 15:57:07 -0700826 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700827 int ret;
828
Al Viroa4555892014-10-21 20:11:25 -0400829 _enter("{%x:%u},%p{%pd},",
David Howells5cf9dd52018-04-09 21:12:31 +0100830 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
David Howells260a9802007-04-26 15:59:35 -0700831
David Howells2b0143b2015-03-17 22:25:59 +0000832 ASSERTCMP(d_inode(dentry), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700833
David Howells45222b92007-05-10 22:22:20 -0700834 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700835 _leave(" = -ENAMETOOLONG");
836 return ERR_PTR(-ENAMETOOLONG);
837 }
838
David Howells5cf9dd52018-04-09 21:12:31 +0100839 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700840 _leave(" = -ESTALE");
841 return ERR_PTR(-ESTALE);
842 }
843
David Howells5cf9dd52018-04-09 21:12:31 +0100844 key = afs_request_key(dvnode->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700845 if (IS_ERR(key)) {
846 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800847 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700848 }
849
David Howells5cf9dd52018-04-09 21:12:31 +0100850 ret = afs_validate(dvnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700851 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700852 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700853 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return ERR_PTR(ret);
855 }
856
David Howells6f8880d2018-04-09 21:12:31 +0100857 if (dentry->d_name.len >= 4 &&
858 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
859 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
860 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
861 dentry->d_name.name[dentry->d_name.len - 1] == 's')
862 return afs_lookup_atsys(dir, dentry, key);
863
David Howellsd55b4da2018-04-06 14:17:24 +0100864 afs_stat_v(dvnode, n_lookup);
David Howells5cf9dd52018-04-09 21:12:31 +0100865 inode = afs_do_lookup(dir, dentry, key);
David Howells00d3b7a2007-04-26 15:57:07 -0700866 key_put(key);
Al Viro34b2a882018-06-24 10:43:51 -0400867 if (inode == ERR_PTR(-ENOENT)) {
868 inode = afs_try_auto_mntpt(dentry, dir);
Al Viro34b2a882018-06-24 10:43:51 -0400869 } else {
870 dentry->d_fsdata =
871 (void *)(unsigned long)dvnode->status.data_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Al Viro34b2a882018-06-24 10:43:51 -0400873 d = d_splice_alias(inode, dentry);
874 if (!IS_ERR_OR_NULL(d))
875 d->d_fsdata = dentry->d_fsdata;
876 return d;
David Howellsec268152007-04-26 15:49:28 -0700877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/*
880 * check that a dentry lookup hit has found a valid entry
881 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
882 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
Al Viro0b728e12012-06-10 16:03:43 -0400884static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
David Howells260a9802007-04-26 15:59:35 -0700886 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100887 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct dentry *parent;
David Howellsc435ee32017-11-02 15:27:49 +0000889 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700890 struct key *key;
David Howellsa4ff7402018-04-06 14:17:24 +0100891 long dir_version, de_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int ret;
893
Al Viro0b728e12012-06-10 16:03:43 -0400894 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100895 return -ECHILD;
896
David Howellsc435ee32017-11-02 15:27:49 +0000897 if (d_really_is_positive(dentry)) {
898 vnode = AFS_FS_I(d_inode(dentry));
Al Viroa4555892014-10-21 20:11:25 -0400899 _enter("{v={%x:%u} n=%pd fl=%lx},",
900 vnode->fid.vid, vnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -0700901 vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000902 } else {
Al Viroa4555892014-10-21 20:11:25 -0400903 _enter("{neg n=%pd}", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
David Howells260a9802007-04-26 15:59:35 -0700906 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700907 if (IS_ERR(key))
908 key = NULL;
909
David Howellsc435ee32017-11-02 15:27:49 +0000910 if (d_really_is_positive(dentry)) {
911 inode = d_inode(dentry);
912 if (inode) {
913 vnode = AFS_FS_I(inode);
914 afs_validate(vnode, key);
915 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
916 goto out_bad;
917 }
918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700921 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000922 dir = AFS_FS_I(d_inode(parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
David Howells260a9802007-04-26 15:59:35 -0700924 /* validate the parent directory */
David Howellsc435ee32017-11-02 15:27:49 +0000925 afs_validate(dir, key);
David Howells260a9802007-04-26 15:59:35 -0700926
927 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Al Viroa4555892014-10-21 20:11:25 -0400928 _debug("%pd: parent dir deleted", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000929 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
David Howellsa4ff7402018-04-06 14:17:24 +0100932 /* We only need to invalidate a dentry if the server's copy changed
933 * behind our back. If we made the change, it's no problem. Note that
934 * on a 32-bit system, we only have 32 bits in the dentry to store the
935 * version.
936 */
937 dir_version = (long)dir->status.data_version;
938 de_version = (long)dentry->d_fsdata;
939 if (de_version == dir_version)
David Howells9c55dc82019-07-30 14:38:51 +0100940 goto out_valid_noupdate;
David Howellsa4ff7402018-04-06 14:17:24 +0100941
942 dir_version = (long)dir->invalid_before;
943 if (de_version - dir_version >= 0)
944 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
David Howells260a9802007-04-26 15:59:35 -0700946 _debug("dir modified");
David Howellsd55b4da2018-04-06 14:17:24 +0100947 afs_stat_v(dir, n_reval);
David Howells260a9802007-04-26 15:59:35 -0700948
949 /* search the directory for this vnode */
David Howells5cf9dd52018-04-09 21:12:31 +0100950 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
David Howells260a9802007-04-26 15:59:35 -0700951 switch (ret) {
952 case 0:
953 /* the filename maps to something */
David Howells2b0143b2015-03-17 22:25:59 +0000954 if (d_really_is_negative(dentry))
David Howellsc435ee32017-11-02 15:27:49 +0000955 goto out_bad_parent;
956 inode = d_inode(dentry);
957 if (is_bad_inode(inode)) {
Al Viroa4555892014-10-21 20:11:25 -0400958 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
959 dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000960 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
David Howellsc435ee32017-11-02 15:27:49 +0000963 vnode = AFS_FS_I(inode);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* if the vnode ID has changed, then the dirent points to a
966 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700967 if (fid.vnode != vnode->fid.vnode) {
Al Viroa4555892014-10-21 20:11:25 -0400968 _debug("%pd: dirent changed [%u != %u]",
969 dentry, fid.vnode,
David Howells08e0e7c2007-04-26 15:55:03 -0700970 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto not_found;
972 }
973
974 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700975 * been deleted and replaced, and the original vnode ID has
976 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700977 if (fid.unique != vnode->fid.unique) {
Al Viroa4555892014-10-21 20:11:25 -0400978 _debug("%pd: file deleted (uq %u -> %u I:%u)",
979 dentry, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500980 vnode->fid.unique,
David Howellsc435ee32017-11-02 15:27:49 +0000981 vnode->vfs_inode.i_generation);
982 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700983 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000984 write_sequnlock(&vnode->cb_lock);
David Howells260a9802007-04-26 15:59:35 -0700985 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
David Howells260a9802007-04-26 15:59:35 -0700987 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
David Howells260a9802007-04-26 15:59:35 -0700989 case -ENOENT:
990 /* the filename is unknown */
Al Viroa4555892014-10-21 20:11:25 -0400991 _debug("%pd: dirent not found", dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000992 if (d_really_is_positive(dentry))
David Howells260a9802007-04-26 15:59:35 -0700993 goto not_found;
994 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700995
David Howells260a9802007-04-26 15:59:35 -0700996 default:
Al Viroa4555892014-10-21 20:11:25 -0400997 _debug("failed to iterate dir %pd: %d",
998 parent, ret);
David Howellsc435ee32017-11-02 15:27:49 +0000999 goto out_bad_parent;
David Howells08e0e7c2007-04-26 15:55:03 -07001000 }
1001
David Howellsec268152007-04-26 15:49:28 -07001002out_valid:
David Howellsa4ff7402018-04-06 14:17:24 +01001003 dentry->d_fsdata = (void *)dir_version;
David Howells9c55dc82019-07-30 14:38:51 +01001004out_valid_noupdate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -07001006 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 _leave(" = 1 [valid]");
1008 return 1;
1009
1010 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -07001011not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_lock(&dentry->d_lock);
1013 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1014 spin_unlock(&dentry->d_lock);
1015
David Howellsc435ee32017-11-02 15:27:49 +00001016out_bad_parent:
Al Viroa4555892014-10-21 20:11:25 -04001017 _debug("dropping dentry %pd2", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 dput(parent);
David Howellsc435ee32017-11-02 15:27:49 +00001019out_bad:
David Howells00d3b7a2007-04-26 15:57:07 -07001020 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 _leave(" = 0 [bad]");
1023 return 0;
David Howellsec268152007-04-26 15:49:28 -07001024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
1027 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1028 * sleep)
1029 * - called from dput() when d_count is going to 0.
1030 * - return 1 to request dentry be unhashed, 0 otherwise
1031 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001032static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Al Viroa4555892014-10-21 20:11:25 -04001034 _enter("%pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1037 goto zap;
1038
David Howells2b0143b2015-03-17 22:25:59 +00001039 if (d_really_is_positive(dentry) &&
1040 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1041 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
wangleibec5eb62010-08-11 09:38:04 +01001042 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 _leave(" = 0 [keep]");
1045 return 0;
1046
David Howellsec268152007-04-26 15:49:28 -07001047zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 _leave(" = 1 [zap]");
1049 return 1;
David Howellsec268152007-04-26 15:49:28 -07001050}
David Howells260a9802007-04-26 15:59:35 -07001051
1052/*
1053 * handle dentry release
1054 */
David Howells66c7e1d2018-04-06 14:17:25 +01001055void afs_d_release(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001056{
Al Viroa4555892014-10-21 20:11:25 -04001057 _enter("%pd", dentry);
David Howells260a9802007-04-26 15:59:35 -07001058}
1059
1060/*
David Howellsd2ddc772017-11-02 15:27:50 +00001061 * Create a new inode for create/mkdir/symlink
1062 */
1063static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1064 struct dentry *new_dentry,
1065 struct afs_fid *newfid,
1066 struct afs_file_status *newstatus,
1067 struct afs_callback *newcb)
1068{
David Howells5a813272018-04-06 14:17:26 +01001069 struct afs_vnode *vnode;
David Howellsd2ddc772017-11-02 15:27:50 +00001070 struct inode *inode;
1071
1072 if (fc->ac.error < 0)
1073 return;
1074
David Howellsbc1527d2017-11-20 23:04:08 +00001075 d_drop(new_dentry);
1076
David Howellsd2ddc772017-11-02 15:27:50 +00001077 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1078 newfid, newstatus, newcb, fc->cbi);
1079 if (IS_ERR(inode)) {
1080 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1081 * the new directory on the server.
1082 */
1083 fc->ac.error = PTR_ERR(inode);
1084 return;
1085 }
1086
David Howells5a813272018-04-06 14:17:26 +01001087 vnode = AFS_FS_I(inode);
1088 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howellsbc1527d2017-11-20 23:04:08 +00001089 d_add(new_dentry, inode);
David Howellsd2ddc772017-11-02 15:27:50 +00001090}
1091
1092/*
David Howells260a9802007-04-26 15:59:35 -07001093 * create a directory on an AFS filesystem
1094 */
Al Viro18bb1db2011-07-26 01:41:39 -04001095static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -07001096{
David Howellsd2ddc772017-11-02 15:27:50 +00001097 struct afs_file_status newstatus;
1098 struct afs_fs_cursor fc;
1099 struct afs_callback newcb;
1100 struct afs_vnode *dvnode = AFS_FS_I(dir);
1101 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001102 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001103 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001104 int ret;
1105
David Howellsd2ddc772017-11-02 15:27:50 +00001106 mode |= S_IFDIR;
David Howells260a9802007-04-26 15:59:35 -07001107
Al Viroa4555892014-10-21 20:11:25 -04001108 _enter("{%x:%u},{%pd},%ho",
1109 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001110
David Howells260a9802007-04-26 15:59:35 -07001111 key = afs_request_key(dvnode->volume->cell);
1112 if (IS_ERR(key)) {
1113 ret = PTR_ERR(key);
1114 goto error;
1115 }
1116
David Howellsd2ddc772017-11-02 15:27:50 +00001117 ret = -ERESTARTSYS;
1118 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1119 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001120 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001121 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001122 &newfid, &newstatus, &newcb);
1123 }
David Howells260a9802007-04-26 15:59:35 -07001124
David Howellsd2ddc772017-11-02 15:27:50 +00001125 afs_check_for_remote_deletion(&fc, fc.vnode);
1126 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1127 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1128 ret = afs_end_vnode_operation(&fc);
1129 if (ret < 0)
1130 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001131 } else {
1132 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001133 }
1134
David Howells63a46812018-04-06 14:17:25 +01001135 if (ret == 0 &&
1136 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1137 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1138 afs_edit_dir_for_create);
1139
David Howells260a9802007-04-26 15:59:35 -07001140 key_put(key);
1141 _leave(" = 0");
1142 return 0;
1143
David Howellsd2ddc772017-11-02 15:27:50 +00001144error_key:
David Howells260a9802007-04-26 15:59:35 -07001145 key_put(key);
1146error:
1147 d_drop(dentry);
1148 _leave(" = %d", ret);
1149 return ret;
1150}
1151
1152/*
David Howellsd2ddc772017-11-02 15:27:50 +00001153 * Remove a subdir from a directory.
David Howells260a9802007-04-26 15:59:35 -07001154 */
David Howellsd2ddc772017-11-02 15:27:50 +00001155static void afs_dir_remove_subdir(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001156{
David Howells2b0143b2015-03-17 22:25:59 +00001157 if (d_really_is_positive(dentry)) {
David Howellsd2ddc772017-11-02 15:27:50 +00001158 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1159
David Howells260a9802007-04-26 15:59:35 -07001160 clear_nlink(&vnode->vfs_inode);
1161 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +00001162 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
David Howells63a46812018-04-06 14:17:25 +01001163 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
David Howells260a9802007-04-26 15:59:35 -07001164 }
David Howells260a9802007-04-26 15:59:35 -07001165}
1166
1167/*
David Howellsd2ddc772017-11-02 15:27:50 +00001168 * remove a directory from an AFS filesystem
David Howells260a9802007-04-26 15:59:35 -07001169 */
David Howellsd2ddc772017-11-02 15:27:50 +00001170static int afs_rmdir(struct inode *dir, struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001171{
David Howellsd2ddc772017-11-02 15:27:50 +00001172 struct afs_fs_cursor fc;
1173 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells260a9802007-04-26 15:59:35 -07001174 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001175 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001176 int ret;
1177
Al Viroa4555892014-10-21 20:11:25 -04001178 _enter("{%x:%u},{%pd}",
1179 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -07001180
David Howells260a9802007-04-26 15:59:35 -07001181 key = afs_request_key(dvnode->volume->cell);
1182 if (IS_ERR(key)) {
1183 ret = PTR_ERR(key);
1184 goto error;
1185 }
1186
David Howellsd2ddc772017-11-02 15:27:50 +00001187 ret = -ERESTARTSYS;
1188 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1189 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001190 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001191 afs_fs_remove(&fc, dentry->d_name.name, true,
1192 data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001193 }
David Howells260a9802007-04-26 15:59:35 -07001194
David Howellsd2ddc772017-11-02 15:27:50 +00001195 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1196 ret = afs_end_vnode_operation(&fc);
David Howells63a46812018-04-06 14:17:25 +01001197 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +00001198 afs_dir_remove_subdir(dentry);
David Howells63a46812018-04-06 14:17:25 +01001199 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1200 afs_edit_dir_remove(dvnode, &dentry->d_name,
1201 afs_edit_dir_for_rmdir);
1202 }
David Howells260a9802007-04-26 15:59:35 -07001203 }
1204
1205 key_put(key);
David Howellsd2ddc772017-11-02 15:27:50 +00001206error:
1207 return ret;
1208}
David Howells260a9802007-04-26 15:59:35 -07001209
David Howellsd2ddc772017-11-02 15:27:50 +00001210/*
1211 * Remove a link to a file or symlink from a directory.
1212 *
1213 * If the file was not deleted due to excess hard links, the fileserver will
1214 * break the callback promise on the file - if it had one - before it returns
1215 * to us, and if it was deleted, it won't
1216 *
1217 * However, if we didn't have a callback promise outstanding, or it was
1218 * outstanding on a different server, then it won't break it either...
1219 */
David Howells440fbc32018-01-02 10:02:19 +00001220static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1221 unsigned long d_version_before,
1222 unsigned long d_version_after)
David Howellsd2ddc772017-11-02 15:27:50 +00001223{
David Howells440fbc32018-01-02 10:02:19 +00001224 bool dir_valid;
David Howellsd2ddc772017-11-02 15:27:50 +00001225 int ret = 0;
1226
David Howells440fbc32018-01-02 10:02:19 +00001227 /* There were no intervening changes on the server if the version
1228 * number we got back was incremented by exactly 1.
1229 */
1230 dir_valid = (d_version_after == d_version_before + 1);
1231
David Howellsd2ddc772017-11-02 15:27:50 +00001232 if (d_really_is_positive(dentry)) {
1233 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1234
David Howells440fbc32018-01-02 10:02:19 +00001235 if (dir_valid) {
1236 drop_nlink(&vnode->vfs_inode);
1237 if (vnode->vfs_inode.i_nlink == 0) {
1238 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1239 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1240 }
David Howellsd2ddc772017-11-02 15:27:50 +00001241 ret = 0;
David Howells440fbc32018-01-02 10:02:19 +00001242 } else {
1243 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1244
1245 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1246 kdebug("AFS_VNODE_DELETED");
1247
1248 ret = afs_validate(vnode, key);
1249 if (ret == -ESTALE)
1250 ret = 0;
1251 }
David Howellsd2ddc772017-11-02 15:27:50 +00001252 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1253 }
1254
1255 return ret;
1256}
1257
1258/*
1259 * Remove a file or symlink from an AFS filesystem.
1260 */
1261static int afs_unlink(struct inode *dir, struct dentry *dentry)
1262{
1263 struct afs_fs_cursor fc;
1264 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
1265 struct key *key;
David Howells440fbc32018-01-02 10:02:19 +00001266 unsigned long d_version = (unsigned long)dentry->d_fsdata;
David Howells63a46812018-04-06 14:17:25 +01001267 u64 data_version = dvnode->status.data_version;
David Howellsd2ddc772017-11-02 15:27:50 +00001268 int ret;
1269
1270 _enter("{%x:%u},{%pd}",
1271 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1272
1273 if (dentry->d_name.len >= AFSNAMEMAX)
1274 return -ENAMETOOLONG;
1275
1276 key = afs_request_key(dvnode->volume->cell);
1277 if (IS_ERR(key)) {
1278 ret = PTR_ERR(key);
1279 goto error;
1280 }
1281
1282 /* Try to make sure we have a callback promise on the victim. */
1283 if (d_really_is_positive(dentry)) {
1284 vnode = AFS_FS_I(d_inode(dentry));
1285 ret = afs_validate(vnode, key);
1286 if (ret < 0)
1287 goto error_key;
1288 }
1289
1290 ret = -ERESTARTSYS;
1291 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1292 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001293 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001294 afs_fs_remove(&fc, dentry->d_name.name, false,
1295 data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001296 }
1297
1298 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1299 ret = afs_end_vnode_operation(&fc);
1300 if (ret == 0)
David Howells440fbc32018-01-02 10:02:19 +00001301 ret = afs_dir_remove_link(
1302 dentry, key, d_version,
1303 (unsigned long)dvnode->status.data_version);
David Howells63a46812018-04-06 14:17:25 +01001304 if (ret == 0 &&
1305 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1306 afs_edit_dir_remove(dvnode, &dentry->d_name,
1307 afs_edit_dir_for_unlink);
David Howellsd2ddc772017-11-02 15:27:50 +00001308 }
1309
1310error_key:
David Howells260a9802007-04-26 15:59:35 -07001311 key_put(key);
1312error:
1313 _leave(" = %d", ret);
1314 return ret;
1315}
1316
1317/*
1318 * create a regular file on an AFS filesystem
1319 */
Al Viro4acdaf22011-07-26 01:42:34 -04001320static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04001321 bool excl)
David Howells260a9802007-04-26 15:59:35 -07001322{
David Howellsd2ddc772017-11-02 15:27:50 +00001323 struct afs_fs_cursor fc;
1324 struct afs_file_status newstatus;
1325 struct afs_callback newcb;
Colin Ian King43dd3882017-11-20 13:58:20 +00001326 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howellsd2ddc772017-11-02 15:27:50 +00001327 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001328 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001329 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001330 int ret;
1331
David Howellsd2ddc772017-11-02 15:27:50 +00001332 mode |= S_IFREG;
David Howells260a9802007-04-26 15:59:35 -07001333
Al Viroa4555892014-10-21 20:11:25 -04001334 _enter("{%x:%u},{%pd},%ho,",
1335 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001336
David Howellsd2ddc772017-11-02 15:27:50 +00001337 ret = -ENAMETOOLONG;
1338 if (dentry->d_name.len >= AFSNAMEMAX)
1339 goto error;
1340
David Howells260a9802007-04-26 15:59:35 -07001341 key = afs_request_key(dvnode->volume->cell);
1342 if (IS_ERR(key)) {
1343 ret = PTR_ERR(key);
1344 goto error;
1345 }
1346
David Howellsd2ddc772017-11-02 15:27:50 +00001347 ret = -ERESTARTSYS;
1348 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1349 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001350 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001351 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001352 &newfid, &newstatus, &newcb);
1353 }
David Howells260a9802007-04-26 15:59:35 -07001354
David Howellsd2ddc772017-11-02 15:27:50 +00001355 afs_check_for_remote_deletion(&fc, fc.vnode);
1356 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1357 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1358 ret = afs_end_vnode_operation(&fc);
1359 if (ret < 0)
1360 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001361 } else {
1362 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001363 }
1364
David Howells63a46812018-04-06 14:17:25 +01001365 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1366 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1367 afs_edit_dir_for_create);
1368
David Howells260a9802007-04-26 15:59:35 -07001369 key_put(key);
1370 _leave(" = 0");
1371 return 0;
1372
David Howellsd2ddc772017-11-02 15:27:50 +00001373error_key:
David Howells260a9802007-04-26 15:59:35 -07001374 key_put(key);
1375error:
1376 d_drop(dentry);
1377 _leave(" = %d", ret);
1378 return ret;
1379}
1380
1381/*
1382 * create a hard link between files in an AFS filesystem
1383 */
1384static int afs_link(struct dentry *from, struct inode *dir,
1385 struct dentry *dentry)
1386{
David Howellsd2ddc772017-11-02 15:27:50 +00001387 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001388 struct afs_vnode *dvnode, *vnode;
1389 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001390 u64 data_version;
David Howells260a9802007-04-26 15:59:35 -07001391 int ret;
1392
David Howells2b0143b2015-03-17 22:25:59 +00001393 vnode = AFS_FS_I(d_inode(from));
David Howells260a9802007-04-26 15:59:35 -07001394 dvnode = AFS_FS_I(dir);
David Howells63a46812018-04-06 14:17:25 +01001395 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001396
Al Viroa4555892014-10-21 20:11:25 -04001397 _enter("{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001398 vnode->fid.vid, vnode->fid.vnode,
1399 dvnode->fid.vid, dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001400 dentry);
David Howells260a9802007-04-26 15:59:35 -07001401
David Howellsd2ddc772017-11-02 15:27:50 +00001402 ret = -ENAMETOOLONG;
1403 if (dentry->d_name.len >= AFSNAMEMAX)
1404 goto error;
1405
David Howells260a9802007-04-26 15:59:35 -07001406 key = afs_request_key(dvnode->volume->cell);
1407 if (IS_ERR(key)) {
1408 ret = PTR_ERR(key);
1409 goto error;
1410 }
1411
David Howellsd2ddc772017-11-02 15:27:50 +00001412 ret = -ERESTARTSYS;
1413 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1414 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1415 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001416 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001417 }
David Howells260a9802007-04-26 15:59:35 -07001418
David Howellsd2ddc772017-11-02 15:27:50 +00001419 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001420 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1421 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
David Howells63a46812018-04-06 14:17:25 +01001422 afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001423 }
1424
1425 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1426 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1427 ihold(&vnode->vfs_inode);
1428 d_instantiate(dentry, &vnode->vfs_inode);
1429
1430 mutex_unlock(&vnode->io_lock);
1431 ret = afs_end_vnode_operation(&fc);
1432 if (ret < 0)
1433 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001434 } else {
1435 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001436 }
1437
David Howells63a46812018-04-06 14:17:25 +01001438 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1439 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1440 afs_edit_dir_for_link);
1441
David Howells260a9802007-04-26 15:59:35 -07001442 key_put(key);
1443 _leave(" = 0");
1444 return 0;
1445
David Howellsd2ddc772017-11-02 15:27:50 +00001446error_key:
David Howells260a9802007-04-26 15:59:35 -07001447 key_put(key);
1448error:
1449 d_drop(dentry);
1450 _leave(" = %d", ret);
1451 return ret;
1452}
1453
1454/*
1455 * create a symlink in an AFS filesystem
1456 */
1457static int afs_symlink(struct inode *dir, struct dentry *dentry,
1458 const char *content)
1459{
David Howellsd2ddc772017-11-02 15:27:50 +00001460 struct afs_fs_cursor fc;
1461 struct afs_file_status newstatus;
1462 struct afs_vnode *dvnode = AFS_FS_I(dir);
1463 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001464 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001465 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001466 int ret;
1467
Al Viroa4555892014-10-21 20:11:25 -04001468 _enter("{%x:%u},{%pd},%s",
1469 dvnode->fid.vid, dvnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -07001470 content);
1471
David Howellsd2ddc772017-11-02 15:27:50 +00001472 ret = -ENAMETOOLONG;
1473 if (dentry->d_name.len >= AFSNAMEMAX)
1474 goto error;
1475
David Howells260a9802007-04-26 15:59:35 -07001476 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001477 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001478 goto error;
1479
1480 key = afs_request_key(dvnode->volume->cell);
1481 if (IS_ERR(key)) {
1482 ret = PTR_ERR(key);
1483 goto error;
1484 }
1485
David Howellsd2ddc772017-11-02 15:27:50 +00001486 ret = -ERESTARTSYS;
1487 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1488 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001489 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001490 afs_fs_symlink(&fc, dentry->d_name.name,
1491 content, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001492 &newfid, &newstatus);
1493 }
David Howells260a9802007-04-26 15:59:35 -07001494
David Howellsd2ddc772017-11-02 15:27:50 +00001495 afs_check_for_remote_deletion(&fc, fc.vnode);
1496 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1497 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1498 ret = afs_end_vnode_operation(&fc);
1499 if (ret < 0)
1500 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001501 } else {
1502 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001503 }
1504
David Howells63a46812018-04-06 14:17:25 +01001505 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1506 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1507 afs_edit_dir_for_symlink);
1508
David Howells260a9802007-04-26 15:59:35 -07001509 key_put(key);
1510 _leave(" = 0");
1511 return 0;
1512
David Howellsd2ddc772017-11-02 15:27:50 +00001513error_key:
David Howells260a9802007-04-26 15:59:35 -07001514 key_put(key);
1515error:
1516 d_drop(dentry);
1517 _leave(" = %d", ret);
1518 return ret;
1519}
1520
1521/*
1522 * rename a file in an AFS filesystem and/or move it between directories
1523 */
1524static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001525 struct inode *new_dir, struct dentry *new_dentry,
1526 unsigned int flags)
David Howells260a9802007-04-26 15:59:35 -07001527{
David Howellsd2ddc772017-11-02 15:27:50 +00001528 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001529 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1530 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001531 u64 orig_data_version, new_data_version;
1532 bool new_negative = d_is_negative(new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001533 int ret;
1534
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001535 if (flags)
1536 return -EINVAL;
1537
David Howells2b0143b2015-03-17 22:25:59 +00001538 vnode = AFS_FS_I(d_inode(old_dentry));
David Howells260a9802007-04-26 15:59:35 -07001539 orig_dvnode = AFS_FS_I(old_dir);
1540 new_dvnode = AFS_FS_I(new_dir);
David Howells63a46812018-04-06 14:17:25 +01001541 orig_data_version = orig_dvnode->status.data_version;
1542 new_data_version = new_dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001543
Al Viroa4555892014-10-21 20:11:25 -04001544 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001545 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1546 vnode->fid.vid, vnode->fid.vnode,
1547 new_dvnode->fid.vid, new_dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001548 new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001549
David Howells260a9802007-04-26 15:59:35 -07001550 key = afs_request_key(orig_dvnode->volume->cell);
1551 if (IS_ERR(key)) {
1552 ret = PTR_ERR(key);
1553 goto error;
1554 }
1555
David Howellsd2ddc772017-11-02 15:27:50 +00001556 ret = -ERESTARTSYS;
1557 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1558 if (orig_dvnode != new_dvnode) {
1559 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1560 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001561 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001562 }
1563 }
1564 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001565 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1566 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
David Howellsd2ddc772017-11-02 15:27:50 +00001567 afs_fs_rename(&fc, old_dentry->d_name.name,
David Howells63a46812018-04-06 14:17:25 +01001568 new_dvnode, new_dentry->d_name.name,
1569 orig_data_version, new_data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001570 }
1571
1572 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1573 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1574 if (orig_dvnode != new_dvnode)
1575 mutex_unlock(&new_dvnode->io_lock);
1576 ret = afs_end_vnode_operation(&fc);
1577 if (ret < 0)
1578 goto error_key;
1579 }
1580
David Howells63a46812018-04-06 14:17:25 +01001581 if (ret == 0) {
1582 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1583 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1584 afs_edit_dir_for_rename);
1585
1586 if (!new_negative &&
1587 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1588 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1589 afs_edit_dir_for_rename);
1590
1591 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1592 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1593 &vnode->fid, afs_edit_dir_for_rename);
1594 }
1595
David Howellsd2ddc772017-11-02 15:27:50 +00001596error_key:
David Howells260a9802007-04-26 15:59:35 -07001597 key_put(key);
1598error:
David Howells260a9802007-04-26 15:59:35 -07001599 _leave(" = %d", ret);
1600 return ret;
1601}
David Howellsf3ddee82018-04-06 14:17:25 +01001602
1603/*
1604 * Release a directory page and clean up its private state if it's not busy
1605 * - return true if the page can now be released, false if not
1606 */
1607static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1608{
1609 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1610
1611 _enter("{{%x:%u}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1612
1613 set_page_private(page, 0);
1614 ClearPagePrivate(page);
1615
1616 /* The directory will need reloading. */
1617 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1618 afs_stat_v(dvnode, n_relpg);
1619 return 1;
1620}
1621
1622/*
1623 * invalidate part or all of a page
1624 * - release a page and clean up its private data if offset is 0 (indicating
1625 * the entire page)
1626 */
1627static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1628 unsigned int length)
1629{
1630 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1631
1632 _enter("{%lu},%u,%u", page->index, offset, length);
1633
1634 BUG_ON(!PageLocked(page));
1635
1636 /* The directory will need reloading. */
1637 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1638 afs_stat_v(dvnode, n_inval);
1639
1640 /* we clean up only if the entire page is being invalidated */
1641 if (offset == 0 && length == PAGE_SIZE) {
1642 set_page_private(page, 0);
1643 ClearPagePrivate(page);
1644 }
1645}