blob: 79d93a26759ae7cc0dcf633a79f85efd5a2c7711 [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 Howellsa58823a2019-05-09 15:16:10 +010021#include "afs_fs.h"
David Howells4ea219a2018-04-06 14:17:25 +010022#include "xdr_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
David Howells260a9802007-04-26 15:59:35 -070024static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040025 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int afs_dir_open(struct inode *inode, struct file *file);
Al Viro1bbae9f2013-05-22 16:31:14 -040027static int afs_readdir(struct file *file, struct dir_context *ctx);
Al Viro0b728e12012-06-10 16:03:43 -040028static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
Nick Pigginfe15ce42011-01-07 17:49:23 +110029static int afs_d_delete(const struct dentry *dentry);
David Howells79ddbfa2019-04-25 14:26:51 +010030static void afs_d_iput(struct dentry *dentry, struct inode *inode);
David Howells5cf9dd52018-04-09 21:12:31 +010031static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
David Howellsafefdbb2006-10-03 01:13:46 -070032 loff_t fpos, u64 ino, unsigned dtype);
David Howells5cf9dd52018-04-09 21:12:31 +010033static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
34 loff_t fpos, u64 ino, unsigned dtype);
Al Viro4acdaf22011-07-26 01:42:34 -040035static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040036 bool excl);
Al Viro18bb1db2011-07-26 01:41:39 -040037static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
David Howells260a9802007-04-26 15:59:35 -070038static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39static int afs_unlink(struct inode *dir, struct dentry *dentry);
40static int afs_link(struct dentry *from, struct inode *dir,
41 struct dentry *dentry);
42static int afs_symlink(struct inode *dir, struct dentry *dentry,
43 const char *content);
44static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +020045 struct inode *new_dir, struct dentry *new_dentry,
46 unsigned int flags);
David Howellsf3ddee82018-04-06 14:17:25 +010047static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
48static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
49 unsigned int length);
50
51static int afs_dir_set_page_dirty(struct page *page)
52{
53 BUG(); /* This should never happen. */
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080056const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .open = afs_dir_open,
David Howells00d3b7a2007-04-26 15:57:07 -070058 .release = afs_release,
Al Viro29884ef2016-05-10 14:27:44 -040059 .iterate_shared = afs_readdir,
David Howellse8d6c552007-07-15 23:40:12 -070060 .lock = afs_lock,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020061 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Arjan van de Ven754661f2007-02-12 00:55:38 -080064const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070065 .create = afs_create,
66 .lookup = afs_lookup,
67 .link = afs_link,
68 .unlink = afs_unlink,
69 .symlink = afs_symlink,
70 .mkdir = afs_mkdir,
71 .rmdir = afs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020072 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070073 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070074 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070075 .setattr = afs_setattr,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010076 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
David Howellsf3ddee82018-04-06 14:17:25 +010079const struct address_space_operations afs_dir_aops = {
80 .set_page_dirty = afs_dir_set_page_dirty,
81 .releasepage = afs_dir_releasepage,
82 .invalidatepage = afs_dir_invalidatepage,
83};
84
Al Virod61dcce2011-01-12 20:04:20 -050085const struct dentry_operations afs_fs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .d_revalidate = afs_d_revalidate,
87 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070088 .d_release = afs_d_release,
David Howellsd18610b2011-01-14 19:04:05 +000089 .d_automount = afs_d_automount,
David Howells79ddbfa2019-04-25 14:26:51 +010090 .d_iput = afs_d_iput,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
David Howells5cf9dd52018-04-09 21:12:31 +010093struct afs_lookup_one_cookie {
94 struct dir_context ctx;
95 struct qstr name;
96 bool found;
97 struct afs_fid fid;
98};
99
David Howells260a9802007-04-26 15:59:35 -0700100struct afs_lookup_cookie {
David Howells5cf9dd52018-04-09 21:12:31 +0100101 struct dir_context ctx;
102 struct qstr name;
103 bool found;
104 bool one_only;
105 unsigned short nr_fids;
David Howells39db9812019-05-14 12:33:10 +0100106 struct inode **inodes;
David Howells87182752019-05-09 16:17:05 +0100107 struct afs_status_cb *statuses;
David Howells5cf9dd52018-04-09 21:12:31 +0100108 struct afs_fid fids[50];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * check that a directory page is valid
113 */
David Howellsf3ddee82018-04-06 14:17:25 +0100114static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
115 loff_t i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
David Howells00317632018-04-06 14:17:25 +0100117 struct afs_xdr_dir_page *dbuf;
David Howellsf3ddee82018-04-06 14:17:25 +0100118 loff_t latter, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int tmp, qty;
120
David Howellsdab17c12017-11-02 15:27:52 +0000121 /* Determine how many magic numbers there should be in this page, but
122 * we must take care because the directory may change size under us.
123 */
124 off = page_offset(page);
David Howellsdab17c12017-11-02 15:27:52 +0000125 if (i_size <= off)
126 goto checked;
127
128 latter = i_size - off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (latter >= PAGE_SIZE)
130 qty = PAGE_SIZE;
131 else
132 qty = latter;
David Howells00317632018-04-06 14:17:25 +0100133 qty /= sizeof(union afs_xdr_dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /* check them */
David Howells63a46812018-04-06 14:17:25 +0100136 dbuf = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 for (tmp = 0; tmp < qty; tmp++) {
David Howells00317632018-04-06 14:17:25 +0100138 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
David Howellsdab17c12017-11-02 15:27:52 +0000139 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
David Howellsf3ddee82018-04-06 14:17:25 +0100140 __func__, dvnode->vfs_inode.i_ino, tmp, qty,
David Howells00317632018-04-06 14:17:25 +0100141 ntohs(dbuf->blocks[tmp].hdr.magic));
David Howellsf3ddee82018-04-06 14:17:25 +0100142 trace_afs_dir_check_failed(dvnode, off, i_size);
David Howells63a46812018-04-06 14:17:25 +0100143 kunmap(page);
David Howellsf51375c2018-10-20 00:57:57 +0100144 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto error;
146 }
David Howells63a46812018-04-06 14:17:25 +0100147
148 /* Make sure each block is NUL terminated so we can reasonably
149 * use string functions on it. The filenames in the page
150 * *should* be NUL-terminated anyway.
151 */
152 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
David Howells63a46812018-04-06 14:17:25 +0100155 kunmap(page);
156
David Howellsdab17c12017-11-02 15:27:52 +0000157checked:
David Howellsf3ddee82018-04-06 14:17:25 +0100158 afs_stat_v(dvnode, n_read_dir);
Al Virobe5b82d2016-04-22 15:06:44 -0400159 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsec268152007-04-26 15:49:28 -0700161error:
Al Virobe5b82d2016-04-22 15:06:44 -0400162 return false;
David Howellsec268152007-04-26 15:49:28 -0700163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
David Howells445b1022019-04-25 14:26:51 +0100166 * Check the contents of a directory that we've just read.
167 */
168static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
169{
170 struct afs_xdr_dir_page *dbuf;
171 unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
172
173 for (i = 0; i < req->nr_pages; i++)
174 if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
175 goto bad;
176 return true;
177
178bad:
179 pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
180 dvnode->fid.vid, dvnode->fid.vnode,
181 req->file_size, req->len, req->actual_len, req->remain);
182 pr_warn("DIR %llx %x %x %x\n",
183 req->pos, req->index, req->nr_pages, req->offset);
184
185 for (i = 0; i < req->nr_pages; i++) {
186 dbuf = kmap(req->pages[i]);
187 for (j = 0; j < qty; j++) {
188 union afs_xdr_dir_block *block = &dbuf->blocks[j];
189
190 pr_warn("[%02x] %32phN\n", i * qty + j, block);
191 }
192 kunmap(req->pages[i]);
193 }
194 return false;
195}
196
197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * open an AFS directory file
199 */
200static int afs_dir_open(struct inode *inode, struct file *file)
201{
202 _enter("{%lu}", inode->i_ino);
203
David Howells00317632018-04-06 14:17:25 +0100204 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
205 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David Howells08e0e7c2007-04-26 15:55:03 -0700207 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOENT;
209
David Howells00d3b7a2007-04-26 15:57:07 -0700210 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
David Howellsf3ddee82018-04-06 14:17:25 +0100214 * Read the directory into the pagecache in one go, scrubbing the previous
215 * contents. The list of pages is returned, pinning them so that they don't
216 * get reclaimed during the iteration.
217 */
218static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
David Howellsb61f7dc2018-04-27 20:46:22 +0100219 __acquires(&dvnode->validate_lock)
David Howellsf3ddee82018-04-06 14:17:25 +0100220{
221 struct afs_read *req;
222 loff_t i_size;
223 int nr_pages, nr_inline, i, n;
224 int ret = -ENOMEM;
225
226retry:
227 i_size = i_size_read(&dvnode->vfs_inode);
228 if (i_size < 2048)
David Howellsf51375c2018-10-20 00:57:57 +0100229 return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
230 if (i_size > 2048 * 1024) {
231 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
David Howellsf3ddee82018-04-06 14:17:25 +0100232 return ERR_PTR(-EFBIG);
David Howellsf51375c2018-10-20 00:57:57 +0100233 }
David Howellsf3ddee82018-04-06 14:17:25 +0100234
235 _enter("%llu", i_size);
236
237 /* Get a request record to hold the page list. We want to hold it
238 * inline if we can, but we don't want to make an order 1 allocation.
239 */
240 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
241 nr_inline = nr_pages;
242 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
243 nr_inline = 0;
244
245 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
246 GFP_KERNEL);
247 if (!req)
248 return ERR_PTR(-ENOMEM);
249
250 refcount_set(&req->usage, 1);
251 req->nr_pages = nr_pages;
252 req->actual_len = i_size; /* May change */
253 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
254 req->data_version = dvnode->status.data_version; /* May change */
255 if (nr_inline > 0) {
256 req->pages = req->array;
257 } else {
258 req->pages = kcalloc(nr_pages, sizeof(struct page *),
259 GFP_KERNEL);
260 if (!req->pages)
261 goto error;
262 }
263
264 /* Get a list of all the pages that hold or will hold the directory
265 * content. We need to fill in any gaps that we might find where the
266 * memory reclaimer has been at work. If there are any gaps, we will
267 * need to reread the entire directory contents.
268 */
269 i = 0;
270 do {
271 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
272 req->nr_pages - i,
273 req->pages + i);
274 _debug("find %u at %u/%u", n, i, req->nr_pages);
275 if (n == 0) {
276 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
277
278 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
279 afs_stat_v(dvnode, n_inval);
280
281 ret = -ENOMEM;
282 req->pages[i] = __page_cache_alloc(gfp);
283 if (!req->pages[i])
284 goto error;
285 ret = add_to_page_cache_lru(req->pages[i],
286 dvnode->vfs_inode.i_mapping,
287 i, gfp);
288 if (ret < 0)
289 goto error;
290
291 set_page_private(req->pages[i], 1);
292 SetPagePrivate(req->pages[i]);
293 unlock_page(req->pages[i]);
294 i++;
295 } else {
296 i += n;
297 }
298 } while (i < req->nr_pages);
299
300 /* If we're going to reload, we need to lock all the pages to prevent
301 * races.
302 */
David Howellsb61f7dc2018-04-27 20:46:22 +0100303 ret = -ERESTARTSYS;
304 if (down_read_killable(&dvnode->validate_lock) < 0)
305 goto error;
306
307 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
308 goto success;
309
310 up_read(&dvnode->validate_lock);
311 if (down_write_killable(&dvnode->validate_lock) < 0)
312 goto error;
313
David Howellsf3ddee82018-04-06 14:17:25 +0100314 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
David Howells99987c562019-04-25 14:26:51 +0100315 trace_afs_reload_dir(dvnode);
David Howellsf3ddee82018-04-06 14:17:25 +0100316 ret = afs_fetch_data(dvnode, key, req);
317 if (ret < 0)
David Howellsb61f7dc2018-04-27 20:46:22 +0100318 goto error_unlock;
David Howellsf3ddee82018-04-06 14:17:25 +0100319
320 task_io_account_read(PAGE_SIZE * req->nr_pages);
321
322 if (req->len < req->file_size)
323 goto content_has_grown;
324
325 /* Validate the data we just read. */
326 ret = -EIO;
David Howells445b1022019-04-25 14:26:51 +0100327 if (!afs_dir_check_pages(dvnode, req))
328 goto error_unlock;
David Howellsf3ddee82018-04-06 14:17:25 +0100329
330 // TODO: Trim excess pages
331
332 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
333 }
334
David Howellsb61f7dc2018-04-27 20:46:22 +0100335 downgrade_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100336success:
David Howellsf3ddee82018-04-06 14:17:25 +0100337 return req;
338
David Howellsf3ddee82018-04-06 14:17:25 +0100339error_unlock:
David Howellsb61f7dc2018-04-27 20:46:22 +0100340 up_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100341error:
342 afs_put_read(req);
343 _leave(" = %d", ret);
344 return ERR_PTR(ret);
345
346content_has_grown:
David Howellsb61f7dc2018-04-27 20:46:22 +0100347 up_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100348 afs_put_read(req);
349 goto retry;
350}
351
352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * deal with one block in an AFS directory
354 */
David Howellsf51375c2018-10-20 00:57:57 +0100355static int afs_dir_iterate_block(struct afs_vnode *dvnode,
356 struct dir_context *ctx,
David Howells00317632018-04-06 14:17:25 +0100357 union afs_xdr_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400358 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David Howells00317632018-04-06 14:17:25 +0100360 union afs_xdr_dirent *dire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unsigned offset, next, curr;
362 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400363 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Al Viro1bbae9f2013-05-22 16:31:14 -0400365 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Howells00317632018-04-06 14:17:25 +0100367 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* walk through the block, an entry at a time */
David Howells4ea219a2018-04-06 14:17:25 +0100370 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
371 offset < AFS_DIR_SLOTS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 offset = next
373 ) {
374 next = offset + 1;
375
376 /* skip entries marked unused in the bitmap */
David Howells00317632018-04-06 14:17:25 +0100377 if (!(block->hdr.bitmap[offset / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 (1 << (offset % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800379 _debug("ENT[%zu.%u]: unused",
David Howells00317632018-04-06 14:17:25 +0100380 blkoff / sizeof(union afs_xdr_dir_block), offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (offset >= curr)
Al Viro1bbae9f2013-05-22 16:31:14 -0400382 ctx->pos = blkoff +
David Howells00317632018-04-06 14:17:25 +0100383 next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 continue;
385 }
386
387 /* got a valid entry */
388 dire = &block->dirents[offset];
389 nlen = strnlen(dire->u.name,
390 sizeof(*block) -
David Howells00317632018-04-06 14:17:25 +0100391 offset * sizeof(union afs_xdr_dirent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800393 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
David Howells00317632018-04-06 14:17:25 +0100394 blkoff / sizeof(union afs_xdr_dir_block), offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 (offset < curr ? "skip" : "fill"),
396 nlen, dire->u.name);
397
398 /* work out where the next possible entry is */
David Howells00317632018-04-06 14:17:25 +0100399 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
David Howells4ea219a2018-04-06 14:17:25 +0100400 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800401 _debug("ENT[%zu.%u]:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 " %u travelled beyond end dir block"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800403 " (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100404 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 offset, next, tmp, nlen);
David Howellsf51375c2018-10-20 00:57:57 +0100406 return afs_bad(dvnode, afs_file_error_dir_over_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
David Howells00317632018-04-06 14:17:25 +0100408 if (!(block->hdr.bitmap[next / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 (1 << (next % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800410 _debug("ENT[%zu.%u]:"
411 " %u unmarked extension (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100412 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 offset, next, tmp, nlen);
David Howellsf51375c2018-10-20 00:57:57 +0100414 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800417 _debug("ENT[%zu.%u]: ext %u/%zu",
David Howells00317632018-04-06 14:17:25 +0100418 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 next, tmp, nlen);
420 next++;
421 }
422
423 /* skip if starts before the current position */
424 if (offset < curr)
425 continue;
426
427 /* found the next entry */
Al Viro1bbae9f2013-05-22 16:31:14 -0400428 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 ntohl(dire->u.vnode),
David Howells5cf9dd52018-04-09 21:12:31 +0100430 (ctx->actor == afs_lookup_filldir ||
431 ctx->actor == afs_lookup_one_filldir)?
Al Viro1bbae9f2013-05-22 16:31:14 -0400432 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 _leave(" = 0 [full]");
434 return 0;
435 }
436
David Howells00317632018-04-06 14:17:25 +0100437 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 _leave(" = 1 [more]");
441 return 1;
David Howellsec268152007-04-26 15:49:28 -0700442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
David Howells08e0e7c2007-04-26 15:55:03 -0700445 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400447static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
448 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
David Howellsf3ddee82018-04-06 14:17:25 +0100450 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells00317632018-04-06 14:17:25 +0100451 struct afs_xdr_dir_page *dbuf;
452 union afs_xdr_dir_block *dblock;
David Howellsf3ddee82018-04-06 14:17:25 +0100453 struct afs_read *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct page *page;
455 unsigned blkoff, limit;
456 int ret;
457
Al Viro1bbae9f2013-05-22 16:31:14 -0400458 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
David Howells08e0e7c2007-04-26 15:55:03 -0700460 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 _leave(" = -ESTALE");
462 return -ESTALE;
463 }
464
David Howellsf3ddee82018-04-06 14:17:25 +0100465 req = afs_read_dir(dvnode, key);
466 if (IS_ERR(req))
467 return PTR_ERR(req);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* round the file position up to the next entry boundary */
David Howells00317632018-04-06 14:17:25 +0100470 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
471 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* walk through the blocks in sequence */
474 ret = 0;
David Howellsf3ddee82018-04-06 14:17:25 +0100475 while (ctx->pos < req->actual_len) {
David Howells00317632018-04-06 14:17:25 +0100476 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
David Howellsf3ddee82018-04-06 14:17:25 +0100478 /* Fetch the appropriate page from the directory and re-add it
479 * to the LRU.
480 */
481 page = req->pages[blkoff / PAGE_SIZE];
482 if (!page) {
David Howellsf51375c2018-10-20 00:57:57 +0100483 ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 }
David Howellsf3ddee82018-04-06 14:17:25 +0100486 mark_page_accessed(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 limit = blkoff & ~(PAGE_SIZE - 1);
489
David Howellsf3ddee82018-04-06 14:17:25 +0100490 dbuf = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* deal with the individual blocks stashed on this page */
493 do {
494 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
David Howells00317632018-04-06 14:17:25 +0100495 sizeof(union afs_xdr_dir_block)];
David Howellsf51375c2018-10-20 00:57:57 +0100496 ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (ret != 1) {
David Howellsf3ddee82018-04-06 14:17:25 +0100498 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto out;
500 }
501
David Howells00317632018-04-06 14:17:25 +0100502 blkoff += sizeof(union afs_xdr_dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Al Viro1bbae9f2013-05-22 16:31:14 -0400504 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David Howellsf3ddee82018-04-06 14:17:25 +0100506 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ret = 0;
508 }
509
David Howellsec268152007-04-26 15:49:28 -0700510out:
David Howellsb61f7dc2018-04-27 20:46:22 +0100511 up_read(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100512 afs_put_read(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 _leave(" = %d", ret);
514 return ret;
David Howellsec268152007-04-26 15:49:28 -0700515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/*
518 * read an AFS directory
519 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400520static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
David Howells215804a2017-11-02 15:27:52 +0000522 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
David Howellsec268152007-04-26 15:49:28 -0700523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
David Howells5cf9dd52018-04-09 21:12:31 +0100526 * Search the directory for a single name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
528 * uniquifier through dtype
529 */
David Howells5cf9dd52018-04-09 21:12:31 +0100530static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
531 int nlen, loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
David Howells5cf9dd52018-04-09 21:12:31 +0100533 struct afs_lookup_one_cookie *cookie =
534 container_of(ctx, struct afs_lookup_one_cookie, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Al Viro1bbae9f2013-05-22 16:31:14 -0400536 _enter("{%s,%u},%s,%u,,%llu,%u",
537 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700538 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David Howells08e0e7c2007-04-26 15:55:03 -0700540 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100541 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
542 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells08e0e7c2007-04-26 15:55:03 -0700543
Al Viro1bbae9f2013-05-22 16:31:14 -0400544 if (cookie->name.len != nlen ||
545 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 _leave(" = 0 [no]");
547 return 0;
548 }
549
550 cookie->fid.vnode = ino;
551 cookie->fid.unique = dtype;
552 cookie->found = 1;
553
554 _leave(" = -1 [found]");
555 return -1;
David Howellsec268152007-04-26 15:49:28 -0700556}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
David Howells5cf9dd52018-04-09 21:12:31 +0100559 * Do a lookup of a single name in a directory
David Howells260a9802007-04-26 15:59:35 -0700560 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
David Howells5cf9dd52018-04-09 21:12:31 +0100562static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
563 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Al Viro1bbae9f2013-05-22 16:31:14 -0400565 struct afs_super_info *as = dir->i_sb->s_fs_info;
David Howells5cf9dd52018-04-09 21:12:31 +0100566 struct afs_lookup_one_cookie cookie = {
567 .ctx.actor = afs_lookup_one_filldir,
Al Viro1bbae9f2013-05-22 16:31:14 -0400568 .name = dentry->d_name,
569 .fid.vid = as->volume->vid
570 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int ret;
572
Al Viroa4555892014-10-21 20:11:25 -0400573 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400576 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700578 _leave(" = %d [iter]", ret);
579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 ret = -ENOENT;
583 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700584 _leave(" = -ENOENT [not found]");
585 return -ENOENT;
586 }
587
588 *fid = cookie.fid;
David Howells3b6492d2018-10-20 00:57:57 +0100589 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
David Howells08e0e7c2007-04-26 15:55:03 -0700590 return 0;
591}
592
593/*
David Howells5cf9dd52018-04-09 21:12:31 +0100594 * search the directory for a name
595 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
596 * uniquifier through dtype
597 */
598static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
599 int nlen, loff_t fpos, u64 ino, unsigned dtype)
600{
601 struct afs_lookup_cookie *cookie =
602 container_of(ctx, struct afs_lookup_cookie, ctx);
603 int ret;
604
605 _enter("{%s,%u},%s,%u,,%llu,%u",
606 cookie->name.name, cookie->name.len, name, nlen,
607 (unsigned long long) ino, dtype);
608
609 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100610 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
611 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells5cf9dd52018-04-09 21:12:31 +0100612
613 if (cookie->found) {
614 if (cookie->nr_fids < 50) {
615 cookie->fids[cookie->nr_fids].vnode = ino;
616 cookie->fids[cookie->nr_fids].unique = dtype;
617 cookie->nr_fids++;
618 }
619 } else if (cookie->name.len == nlen &&
620 memcmp(cookie->name.name, name, nlen) == 0) {
621 cookie->fids[0].vnode = ino;
622 cookie->fids[0].unique = dtype;
623 cookie->found = 1;
624 if (cookie->one_only)
625 return -1;
626 }
627
628 ret = cookie->nr_fids >= 50 ? -1 : 0;
629 _leave(" = %d", ret);
630 return ret;
631}
632
633/*
634 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
635 * files in one go and create inodes for them. The inode of the file we were
636 * asked for is returned.
637 */
638static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
639 struct key *key)
640{
641 struct afs_lookup_cookie *cookie;
David Howellsf6424042019-05-13 16:14:32 +0100642 struct afs_cb_interest *dcbi, *cbi = NULL;
David Howells5cf9dd52018-04-09 21:12:31 +0100643 struct afs_super_info *as = dir->i_sb->s_fs_info;
David Howells87182752019-05-09 16:17:05 +0100644 struct afs_status_cb *scb;
David Howellsb8359152019-05-14 12:23:43 +0100645 struct afs_iget_data iget_data;
David Howells5cf9dd52018-04-09 21:12:31 +0100646 struct afs_fs_cursor fc;
David Howellsf6424042019-05-13 16:14:32 +0100647 struct afs_server *server;
David Howells39db9812019-05-14 12:33:10 +0100648 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
649 struct inode *inode = NULL, *ti;
David Howells5cf9dd52018-04-09 21:12:31 +0100650 int ret, i;
651
652 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
653
654 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
655 if (!cookie)
656 return ERR_PTR(-ENOMEM);
657
658 cookie->ctx.actor = afs_lookup_filldir;
659 cookie->name = dentry->d_name;
660 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
661
662 read_seqlock_excl(&dvnode->cb_lock);
David Howellsf6424042019-05-13 16:14:32 +0100663 dcbi = rcu_dereference_protected(dvnode->cb_interest,
664 lockdep_is_held(&dvnode->cb_lock.lock));
665 if (dcbi) {
666 server = dcbi->server;
667 if (server &&
668 test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
669 cookie->one_only = true;
670 }
David Howells5cf9dd52018-04-09 21:12:31 +0100671 read_sequnlock_excl(&dvnode->cb_lock);
672
673 for (i = 0; i < 50; i++)
674 cookie->fids[i].vid = as->volume->vid;
675
676 /* search the directory */
677 ret = afs_dir_iterate(dir, &cookie->ctx, key);
678 if (ret < 0) {
679 inode = ERR_PTR(ret);
680 goto out;
681 }
682
683 inode = ERR_PTR(-ENOENT);
684 if (!cookie->found)
685 goto out;
686
687 /* Check to see if we already have an inode for the primary fid. */
David Howellsb8359152019-05-14 12:23:43 +0100688 iget_data.fid = cookie->fids[0];
689 iget_data.volume = dvnode->volume;
690 iget_data.cb_v_break = dvnode->volume->cb_v_break;
691 iget_data.cb_s_break = 0;
692 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode,
693 afs_iget5_test, &iget_data);
David Howells5cf9dd52018-04-09 21:12:31 +0100694 if (inode)
695 goto out;
696
697 /* Need space for examining all the selected files */
698 inode = ERR_PTR(-ENOMEM);
David Howells87182752019-05-09 16:17:05 +0100699 cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
700 GFP_KERNEL);
David Howells5cf9dd52018-04-09 21:12:31 +0100701 if (!cookie->statuses)
702 goto out;
703
David Howells39db9812019-05-14 12:33:10 +0100704 cookie->inodes = kcalloc(cookie->nr_fids, sizeof(struct inode *),
705 GFP_KERNEL);
706 if (!cookie->inodes)
707 goto out_s;
708
709 for (i = 1; i < cookie->nr_fids; i++) {
710 scb = &cookie->statuses[i];
711
712 /* Find any inodes that already exist and get their
713 * callback counters.
714 */
715 iget_data.fid = cookie->fids[i];
716 ti = ilookup5_nowait(dir->i_sb, iget_data.fid.vnode,
717 afs_iget5_test, &iget_data);
718 if (!IS_ERR_OR_NULL(ti)) {
719 vnode = AFS_FS_I(ti);
720 scb->cb_break = afs_calc_vnode_cb_break(vnode);
721 cookie->inodes[i] = ti;
722 }
723 }
724
David Howells5cf9dd52018-04-09 21:12:31 +0100725 /* Try FS.InlineBulkStatus first. Abort codes for the individual
726 * lookups contained therein are stored in the reply without aborting
727 * the whole operation.
728 */
729 if (cookie->one_only)
730 goto no_inline_bulk_status;
731
732 inode = ERR_PTR(-ERESTARTSYS);
David Howells20b83912019-05-08 16:16:31 +0100733 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howells5cf9dd52018-04-09 21:12:31 +0100734 while (afs_select_fileserver(&fc)) {
735 if (test_bit(AFS_SERVER_FL_NO_IBULK,
736 &fc.cbi->server->flags)) {
737 fc.ac.abort_code = RX_INVALID_OPERATION;
738 fc.ac.error = -ECONNABORTED;
739 break;
740 }
David Howellsb8359152019-05-14 12:23:43 +0100741 iget_data.cb_v_break = dvnode->volume->cb_v_break;
742 iget_data.cb_s_break = fc.cbi->server->cb_s_break;
David Howells5cf9dd52018-04-09 21:12:31 +0100743 afs_fs_inline_bulk_status(&fc,
744 afs_v2net(dvnode),
745 cookie->fids,
746 cookie->statuses,
David Howells5cf9dd52018-04-09 21:12:31 +0100747 cookie->nr_fids, NULL);
748 }
749
750 if (fc.ac.error == 0)
751 cbi = afs_get_cb_interest(fc.cbi);
752 if (fc.ac.abort_code == RX_INVALID_OPERATION)
753 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
754 inode = ERR_PTR(afs_end_vnode_operation(&fc));
755 }
756
757 if (!IS_ERR(inode))
758 goto success;
759 if (fc.ac.abort_code != RX_INVALID_OPERATION)
760 goto out_c;
761
762no_inline_bulk_status:
763 /* We could try FS.BulkStatus next, but this aborts the entire op if
764 * any of the lookups fails - so, for the moment, revert to
765 * FS.FetchStatus for just the primary fid.
766 */
David Howells5cf9dd52018-04-09 21:12:31 +0100767 inode = ERR_PTR(-ERESTARTSYS);
David Howells20b83912019-05-08 16:16:31 +0100768 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howells5cf9dd52018-04-09 21:12:31 +0100769 while (afs_select_fileserver(&fc)) {
David Howellsb8359152019-05-14 12:23:43 +0100770 iget_data.cb_v_break = dvnode->volume->cb_v_break;
771 iget_data.cb_s_break = fc.cbi->server->cb_s_break;
David Howells87182752019-05-09 16:17:05 +0100772 scb = &cookie->statuses[0];
David Howells5cf9dd52018-04-09 21:12:31 +0100773 afs_fs_fetch_status(&fc,
774 afs_v2net(dvnode),
775 cookie->fids,
David Howellsa58823a2019-05-09 15:16:10 +0100776 scb,
David Howells5cf9dd52018-04-09 21:12:31 +0100777 NULL);
778 }
779
780 if (fc.ac.error == 0)
781 cbi = afs_get_cb_interest(fc.cbi);
782 inode = ERR_PTR(afs_end_vnode_operation(&fc));
783 }
784
785 if (IS_ERR(inode))
786 goto out_c;
787
David Howells5cf9dd52018-04-09 21:12:31 +0100788success:
789 /* Turn all the files into inodes and save the first one - which is the
790 * one we actually want.
791 */
David Howells87182752019-05-09 16:17:05 +0100792 scb = &cookie->statuses[0];
793 if (scb->status.abort_code != 0)
794 inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
David Howells5cf9dd52018-04-09 21:12:31 +0100795
796 for (i = 0; i < cookie->nr_fids; i++) {
David Howells87182752019-05-09 16:17:05 +0100797 struct afs_status_cb *scb = &cookie->statuses[i];
David Howells39db9812019-05-14 12:33:10 +0100798
799 if (!scb->have_status && !scb->have_error)
800 continue;
801
802 if (cookie->inodes[i]) {
803 afs_vnode_commit_status(&fc, AFS_FS_I(cookie->inodes[i]),
804 scb->cb_break, NULL, scb);
805 continue;
806 }
David Howells5cf9dd52018-04-09 21:12:31 +0100807
David Howells87182752019-05-09 16:17:05 +0100808 if (scb->status.abort_code != 0)
David Howells5cf9dd52018-04-09 21:12:31 +0100809 continue;
810
David Howellsb8359152019-05-14 12:23:43 +0100811 iget_data.fid = cookie->fids[i];
812 ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode);
David Howells39db9812019-05-14 12:33:10 +0100813 if (!IS_ERR(ti))
814 afs_cache_permit(AFS_FS_I(ti), key,
815 0 /* Assume vnode->cb_break is 0 */ +
816 iget_data.cb_v_break,
817 scb);
David Howells5cf9dd52018-04-09 21:12:31 +0100818 if (i == 0) {
819 inode = ti;
820 } else {
821 if (!IS_ERR(ti))
822 iput(ti);
823 }
824 }
825
826out_c:
827 afs_put_cb_interest(afs_v2net(dvnode), cbi);
David Howells39db9812019-05-14 12:33:10 +0100828 if (cookie->inodes) {
829 for (i = 0; i < cookie->nr_fids; i++)
830 iput(cookie->inodes[i]);
831 kfree(cookie->inodes);
832 }
833out_s:
David Howells87182752019-05-09 16:17:05 +0100834 kvfree(cookie->statuses);
David Howells5cf9dd52018-04-09 21:12:31 +0100835out:
836 kfree(cookie);
837 return inode;
838}
839
840/*
David Howells6f8880d2018-04-09 21:12:31 +0100841 * Look up an entry in a directory with @sys substitution.
842 */
843static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
844 struct key *key)
845{
846 struct afs_sysnames *subs;
847 struct afs_net *net = afs_i2net(dir);
848 struct dentry *ret;
849 char *buf, *p, *name;
850 int len, i;
851
852 _enter("");
853
854 ret = ERR_PTR(-ENOMEM);
855 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
856 if (!buf)
857 goto out_p;
858 if (dentry->d_name.len > 4) {
859 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
860 p += dentry->d_name.len - 4;
861 }
862
863 /* There is an ordered list of substitutes that we have to try. */
864 read_lock(&net->sysnames_lock);
865 subs = net->sysnames;
866 refcount_inc(&subs->usage);
867 read_unlock(&net->sysnames_lock);
868
869 for (i = 0; i < subs->nr; i++) {
870 name = subs->subs[i];
871 len = dentry->d_name.len - 4 + strlen(name);
872 if (len >= AFSNAMEMAX) {
873 ret = ERR_PTR(-ENAMETOOLONG);
874 goto out_s;
875 }
876
877 strcpy(p, name);
878 ret = lookup_one_len(buf, dentry->d_parent, len);
879 if (IS_ERR(ret) || d_is_positive(ret))
880 goto out_s;
881 dput(ret);
882 }
883
884 /* We don't want to d_add() the @sys dentry here as we don't want to
885 * the cached dentry to hide changes to the sysnames list.
886 */
887 ret = NULL;
888out_s:
889 afs_put_sysnames(subs);
890 kfree(buf);
891out_p:
892 key_put(key);
893 return ret;
894}
895
896/*
David Howells08e0e7c2007-04-26 15:55:03 -0700897 * look up an entry in a directory
898 */
David Howells260a9802007-04-26 15:59:35 -0700899static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400900 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700901{
David Howells5cf9dd52018-04-09 21:12:31 +0100902 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells08e0e7c2007-04-26 15:55:03 -0700903 struct inode *inode;
Al Viro34b2a882018-06-24 10:43:51 -0400904 struct dentry *d;
David Howells00d3b7a2007-04-26 15:57:07 -0700905 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700906 int ret;
907
David Howells3b6492d2018-10-20 00:57:57 +0100908 _enter("{%llx:%llu},%p{%pd},",
David Howells5cf9dd52018-04-09 21:12:31 +0100909 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
David Howells260a9802007-04-26 15:59:35 -0700910
David Howells2b0143b2015-03-17 22:25:59 +0000911 ASSERTCMP(d_inode(dentry), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700912
David Howells45222b92007-05-10 22:22:20 -0700913 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700914 _leave(" = -ENAMETOOLONG");
915 return ERR_PTR(-ENAMETOOLONG);
916 }
917
David Howells5cf9dd52018-04-09 21:12:31 +0100918 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700919 _leave(" = -ESTALE");
920 return ERR_PTR(-ESTALE);
921 }
922
David Howells5cf9dd52018-04-09 21:12:31 +0100923 key = afs_request_key(dvnode->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700924 if (IS_ERR(key)) {
925 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800926 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700927 }
928
David Howells5cf9dd52018-04-09 21:12:31 +0100929 ret = afs_validate(dvnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700930 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700931 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700932 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return ERR_PTR(ret);
934 }
935
David Howells6f8880d2018-04-09 21:12:31 +0100936 if (dentry->d_name.len >= 4 &&
937 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
938 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
939 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
940 dentry->d_name.name[dentry->d_name.len - 1] == 's')
941 return afs_lookup_atsys(dir, dentry, key);
942
David Howellsd55b4da2018-04-06 14:17:24 +0100943 afs_stat_v(dvnode, n_lookup);
David Howells5cf9dd52018-04-09 21:12:31 +0100944 inode = afs_do_lookup(dir, dentry, key);
David Howells00d3b7a2007-04-26 15:57:07 -0700945 key_put(key);
Al Viro34b2a882018-06-24 10:43:51 -0400946 if (inode == ERR_PTR(-ENOENT)) {
947 inode = afs_try_auto_mntpt(dentry, dir);
Al Viro34b2a882018-06-24 10:43:51 -0400948 } else {
949 dentry->d_fsdata =
950 (void *)(unsigned long)dvnode->status.data_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Al Viro34b2a882018-06-24 10:43:51 -0400952 d = d_splice_alias(inode, dentry);
David Howells80548b02019-04-25 14:26:51 +0100953 if (!IS_ERR_OR_NULL(d)) {
Al Viro34b2a882018-06-24 10:43:51 -0400954 d->d_fsdata = dentry->d_fsdata;
David Howells80548b02019-04-25 14:26:51 +0100955 trace_afs_lookup(dvnode, &d->d_name,
956 inode ? AFS_FS_I(inode) : NULL);
957 } else {
958 trace_afs_lookup(dvnode, &dentry->d_name,
959 inode ? AFS_FS_I(inode) : NULL);
960 }
Al Viro34b2a882018-06-24 10:43:51 -0400961 return d;
David Howellsec268152007-04-26 15:49:28 -0700962}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * check that a dentry lookup hit has found a valid entry
966 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
967 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Al Viro0b728e12012-06-10 16:03:43 -0400969static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
David Howells260a9802007-04-26 15:59:35 -0700971 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100972 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct dentry *parent;
David Howellsc435ee32017-11-02 15:27:49 +0000974 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700975 struct key *key;
David Howellsa4ff7402018-04-06 14:17:24 +0100976 long dir_version, de_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int ret;
978
Al Viro0b728e12012-06-10 16:03:43 -0400979 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100980 return -ECHILD;
981
David Howellsc435ee32017-11-02 15:27:49 +0000982 if (d_really_is_positive(dentry)) {
983 vnode = AFS_FS_I(d_inode(dentry));
David Howells3b6492d2018-10-20 00:57:57 +0100984 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
Al Viroa4555892014-10-21 20:11:25 -0400985 vnode->fid.vid, vnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -0700986 vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000987 } else {
Al Viroa4555892014-10-21 20:11:25 -0400988 _enter("{neg n=%pd}", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
David Howells260a9802007-04-26 15:59:35 -0700991 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700992 if (IS_ERR(key))
993 key = NULL;
994
David Howellsc435ee32017-11-02 15:27:49 +0000995 if (d_really_is_positive(dentry)) {
996 inode = d_inode(dentry);
997 if (inode) {
998 vnode = AFS_FS_I(inode);
999 afs_validate(vnode, key);
1000 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1001 goto out_bad;
1002 }
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -07001006 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001007 dir = AFS_FS_I(d_inode(parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
David Howells260a9802007-04-26 15:59:35 -07001009 /* validate the parent directory */
David Howellsc435ee32017-11-02 15:27:49 +00001010 afs_validate(dir, key);
David Howells260a9802007-04-26 15:59:35 -07001011
1012 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Al Viroa4555892014-10-21 20:11:25 -04001013 _debug("%pd: parent dir deleted", dentry);
David Howellsc435ee32017-11-02 15:27:49 +00001014 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
David Howellsa4ff7402018-04-06 14:17:24 +01001017 /* We only need to invalidate a dentry if the server's copy changed
1018 * behind our back. If we made the change, it's no problem. Note that
1019 * on a 32-bit system, we only have 32 bits in the dentry to store the
1020 * version.
1021 */
1022 dir_version = (long)dir->status.data_version;
1023 de_version = (long)dentry->d_fsdata;
1024 if (de_version == dir_version)
1025 goto out_valid;
1026
1027 dir_version = (long)dir->invalid_before;
1028 if (de_version - dir_version >= 0)
1029 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
David Howells260a9802007-04-26 15:59:35 -07001031 _debug("dir modified");
David Howellsd55b4da2018-04-06 14:17:24 +01001032 afs_stat_v(dir, n_reval);
David Howells260a9802007-04-26 15:59:35 -07001033
1034 /* search the directory for this vnode */
David Howells5cf9dd52018-04-09 21:12:31 +01001035 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
David Howells260a9802007-04-26 15:59:35 -07001036 switch (ret) {
1037 case 0:
1038 /* the filename maps to something */
David Howells2b0143b2015-03-17 22:25:59 +00001039 if (d_really_is_negative(dentry))
David Howellsc435ee32017-11-02 15:27:49 +00001040 goto out_bad_parent;
1041 inode = d_inode(dentry);
1042 if (is_bad_inode(inode)) {
Al Viroa4555892014-10-21 20:11:25 -04001043 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1044 dentry);
David Howellsc435ee32017-11-02 15:27:49 +00001045 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
David Howellsc435ee32017-11-02 15:27:49 +00001048 vnode = AFS_FS_I(inode);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* if the vnode ID has changed, then the dirent points to a
1051 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -07001052 if (fid.vnode != vnode->fid.vnode) {
David Howells3b6492d2018-10-20 00:57:57 +01001053 _debug("%pd: dirent changed [%llu != %llu]",
Al Viroa4555892014-10-21 20:11:25 -04001054 dentry, fid.vnode,
David Howells08e0e7c2007-04-26 15:55:03 -07001055 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto not_found;
1057 }
1058
1059 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -07001060 * been deleted and replaced, and the original vnode ID has
1061 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -07001062 if (fid.unique != vnode->fid.unique) {
Al Viroa4555892014-10-21 20:11:25 -04001063 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1064 dentry, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001065 vnode->fid.unique,
David Howellsc435ee32017-11-02 15:27:49 +00001066 vnode->vfs_inode.i_generation);
1067 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -07001068 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +00001069 write_sequnlock(&vnode->cb_lock);
David Howells260a9802007-04-26 15:59:35 -07001070 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
David Howells260a9802007-04-26 15:59:35 -07001072 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David Howells260a9802007-04-26 15:59:35 -07001074 case -ENOENT:
1075 /* the filename is unknown */
Al Viroa4555892014-10-21 20:11:25 -04001076 _debug("%pd: dirent not found", dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001077 if (d_really_is_positive(dentry))
David Howells260a9802007-04-26 15:59:35 -07001078 goto not_found;
1079 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -07001080
David Howells260a9802007-04-26 15:59:35 -07001081 default:
Al Viroa4555892014-10-21 20:11:25 -04001082 _debug("failed to iterate dir %pd: %d",
1083 parent, ret);
David Howellsc435ee32017-11-02 15:27:49 +00001084 goto out_bad_parent;
David Howells08e0e7c2007-04-26 15:55:03 -07001085 }
1086
David Howellsec268152007-04-26 15:49:28 -07001087out_valid:
David Howellsa4ff7402018-04-06 14:17:24 +01001088 dentry->d_fsdata = (void *)dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -07001090 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 _leave(" = 1 [valid]");
1092 return 1;
1093
1094 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -07001095not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 spin_lock(&dentry->d_lock);
1097 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1098 spin_unlock(&dentry->d_lock);
1099
David Howellsc435ee32017-11-02 15:27:49 +00001100out_bad_parent:
Al Viroa4555892014-10-21 20:11:25 -04001101 _debug("dropping dentry %pd2", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 dput(parent);
David Howellsc435ee32017-11-02 15:27:49 +00001103out_bad:
David Howells00d3b7a2007-04-26 15:57:07 -07001104 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 _leave(" = 0 [bad]");
1107 return 0;
David Howellsec268152007-04-26 15:49:28 -07001108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/*
1111 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1112 * sleep)
1113 * - called from dput() when d_count is going to 0.
1114 * - return 1 to request dentry be unhashed, 0 otherwise
1115 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001116static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Al Viroa4555892014-10-21 20:11:25 -04001118 _enter("%pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1121 goto zap;
1122
David Howells2b0143b2015-03-17 22:25:59 +00001123 if (d_really_is_positive(dentry) &&
1124 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1125 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
wangleibec5eb62010-08-11 09:38:04 +01001126 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 _leave(" = 0 [keep]");
1129 return 0;
1130
David Howellsec268152007-04-26 15:49:28 -07001131zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 _leave(" = 1 [zap]");
1133 return 1;
David Howellsec268152007-04-26 15:49:28 -07001134}
David Howells260a9802007-04-26 15:59:35 -07001135
1136/*
David Howells79ddbfa2019-04-25 14:26:51 +01001137 * Clean up sillyrename files on dentry removal.
1138 */
1139static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1140{
1141 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1142 afs_silly_iput(dentry, inode);
1143 iput(inode);
1144}
1145
1146/*
David Howells260a9802007-04-26 15:59:35 -07001147 * handle dentry release
1148 */
David Howells66c7e1d2018-04-06 14:17:25 +01001149void afs_d_release(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001150{
Al Viroa4555892014-10-21 20:11:25 -04001151 _enter("%pd", dentry);
David Howells260a9802007-04-26 15:59:35 -07001152}
1153
1154/*
David Howellsd2ddc772017-11-02 15:27:50 +00001155 * Create a new inode for create/mkdir/symlink
1156 */
1157static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1158 struct dentry *new_dentry,
David Howellsb8359152019-05-14 12:23:43 +01001159 struct afs_iget_data *new_data,
David Howellsa58823a2019-05-09 15:16:10 +01001160 struct afs_status_cb *new_scb)
David Howellsd2ddc772017-11-02 15:27:50 +00001161{
David Howells5a813272018-04-06 14:17:26 +01001162 struct afs_vnode *vnode;
David Howellsd2ddc772017-11-02 15:27:50 +00001163 struct inode *inode;
1164
1165 if (fc->ac.error < 0)
1166 return;
1167
1168 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
David Howellsb8359152019-05-14 12:23:43 +01001169 new_data, new_scb, fc->cbi, fc->vnode);
David Howellsd2ddc772017-11-02 15:27:50 +00001170 if (IS_ERR(inode)) {
1171 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1172 * the new directory on the server.
1173 */
1174 fc->ac.error = PTR_ERR(inode);
1175 return;
1176 }
1177
David Howells5a813272018-04-06 14:17:26 +01001178 vnode = AFS_FS_I(inode);
1179 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howellsa58823a2019-05-09 15:16:10 +01001180 if (fc->ac.error == 0)
1181 afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
David Howells73116df2018-11-13 23:20:35 +00001182 d_instantiate(new_dentry, inode);
David Howellsd2ddc772017-11-02 15:27:50 +00001183}
1184
David Howellsb8359152019-05-14 12:23:43 +01001185static void afs_prep_for_new_inode(struct afs_fs_cursor *fc,
1186 struct afs_iget_data *iget_data)
1187{
1188 iget_data->volume = fc->vnode->volume;
1189 iget_data->cb_v_break = fc->vnode->volume->cb_v_break;
1190 iget_data->cb_s_break = fc->cbi->server->cb_s_break;
1191}
1192
David Howellsd2ddc772017-11-02 15:27:50 +00001193/*
David Howells260a9802007-04-26 15:59:35 -07001194 * create a directory on an AFS filesystem
1195 */
Al Viro18bb1db2011-07-26 01:41:39 -04001196static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -07001197{
David Howellsb8359152019-05-14 12:23:43 +01001198 struct afs_iget_data iget_data;
David Howellsa58823a2019-05-09 15:16:10 +01001199 struct afs_status_cb *scb;
David Howellsd2ddc772017-11-02 15:27:50 +00001200 struct afs_fs_cursor fc;
David Howellsd2ddc772017-11-02 15:27:50 +00001201 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells260a9802007-04-26 15:59:35 -07001202 struct key *key;
1203 int ret;
1204
David Howellsd2ddc772017-11-02 15:27:50 +00001205 mode |= S_IFDIR;
David Howells260a9802007-04-26 15:59:35 -07001206
David Howells3b6492d2018-10-20 00:57:57 +01001207 _enter("{%llx:%llu},{%pd},%ho",
Al Viroa4555892014-10-21 20:11:25 -04001208 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001209
David Howellsa58823a2019-05-09 15:16:10 +01001210 ret = -ENOMEM;
1211 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1212 if (!scb)
1213 goto error;
1214
David Howells260a9802007-04-26 15:59:35 -07001215 key = afs_request_key(dvnode->volume->cell);
1216 if (IS_ERR(key)) {
1217 ret = PTR_ERR(key);
David Howellsa58823a2019-05-09 15:16:10 +01001218 goto error_scb;
David Howells260a9802007-04-26 15:59:35 -07001219 }
1220
David Howellsd2ddc772017-11-02 15:27:50 +00001221 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001222 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001223 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1224
David Howellsd2ddc772017-11-02 15:27:50 +00001225 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001226 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howellsb8359152019-05-14 12:23:43 +01001227 afs_prep_for_new_inode(&fc, &iget_data);
David Howellsa58823a2019-05-09 15:16:10 +01001228 afs_fs_create(&fc, dentry->d_name.name, mode,
David Howellsb8359152019-05-14 12:23:43 +01001229 &scb[0], &iget_data.fid, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001230 }
David Howells260a9802007-04-26 15:59:35 -07001231
David Howellsa58823a2019-05-09 15:16:10 +01001232 afs_check_for_remote_deletion(&fc, dvnode);
1233 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1234 &data_version, &scb[0]);
David Howellsb8359152019-05-14 12:23:43 +01001235 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001236 ret = afs_end_vnode_operation(&fc);
1237 if (ret < 0)
1238 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001239 } else {
1240 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001241 }
1242
David Howells63a46812018-04-06 14:17:25 +01001243 if (ret == 0 &&
1244 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
David Howellsb8359152019-05-14 12:23:43 +01001245 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
David Howells63a46812018-04-06 14:17:25 +01001246 afs_edit_dir_for_create);
1247
David Howells260a9802007-04-26 15:59:35 -07001248 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001249 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001250 _leave(" = 0");
1251 return 0;
1252
David Howellsd2ddc772017-11-02 15:27:50 +00001253error_key:
David Howells260a9802007-04-26 15:59:35 -07001254 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001255error_scb:
1256 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001257error:
1258 d_drop(dentry);
1259 _leave(" = %d", ret);
1260 return ret;
1261}
1262
1263/*
David Howellsd2ddc772017-11-02 15:27:50 +00001264 * Remove a subdir from a directory.
David Howells260a9802007-04-26 15:59:35 -07001265 */
David Howellsd2ddc772017-11-02 15:27:50 +00001266static void afs_dir_remove_subdir(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001267{
David Howells2b0143b2015-03-17 22:25:59 +00001268 if (d_really_is_positive(dentry)) {
David Howellsd2ddc772017-11-02 15:27:50 +00001269 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1270
David Howells260a9802007-04-26 15:59:35 -07001271 clear_nlink(&vnode->vfs_inode);
1272 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +00001273 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
David Howells63a46812018-04-06 14:17:25 +01001274 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
David Howells260a9802007-04-26 15:59:35 -07001275 }
David Howells260a9802007-04-26 15:59:35 -07001276}
1277
1278/*
David Howellsd2ddc772017-11-02 15:27:50 +00001279 * remove a directory from an AFS filesystem
David Howells260a9802007-04-26 15:59:35 -07001280 */
David Howellsd2ddc772017-11-02 15:27:50 +00001281static int afs_rmdir(struct inode *dir, struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001282{
David Howellsa58823a2019-05-09 15:16:10 +01001283 struct afs_status_cb *scb;
David Howellsd2ddc772017-11-02 15:27:50 +00001284 struct afs_fs_cursor fc;
David Howellsf58db832018-10-20 00:57:58 +01001285 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
David Howells260a9802007-04-26 15:59:35 -07001286 struct key *key;
1287 int ret;
1288
David Howells3b6492d2018-10-20 00:57:57 +01001289 _enter("{%llx:%llu},{%pd}",
Al Viroa4555892014-10-21 20:11:25 -04001290 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -07001291
David Howellsa58823a2019-05-09 15:16:10 +01001292 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1293 if (!scb)
1294 return -ENOMEM;
1295
David Howells260a9802007-04-26 15:59:35 -07001296 key = afs_request_key(dvnode->volume->cell);
1297 if (IS_ERR(key)) {
1298 ret = PTR_ERR(key);
1299 goto error;
1300 }
1301
David Howellsf58db832018-10-20 00:57:58 +01001302 /* Try to make sure we have a callback promise on the victim. */
1303 if (d_really_is_positive(dentry)) {
1304 vnode = AFS_FS_I(d_inode(dentry));
1305 ret = afs_validate(vnode, key);
1306 if (ret < 0)
1307 goto error_key;
1308 }
1309
David Howells79ddbfa2019-04-25 14:26:51 +01001310 if (vnode) {
1311 ret = down_write_killable(&vnode->rmdir_lock);
1312 if (ret < 0)
1313 goto error_key;
1314 }
1315
David Howellsd2ddc772017-11-02 15:27:50 +00001316 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001317 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001318 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1319
David Howellsd2ddc772017-11-02 15:27:50 +00001320 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001321 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howellsa58823a2019-05-09 15:16:10 +01001322 afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
David Howellsd2ddc772017-11-02 15:27:50 +00001323 }
David Howells260a9802007-04-26 15:59:35 -07001324
David Howellsa58823a2019-05-09 15:16:10 +01001325 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1326 &data_version, scb);
David Howellsd2ddc772017-11-02 15:27:50 +00001327 ret = afs_end_vnode_operation(&fc);
David Howells63a46812018-04-06 14:17:25 +01001328 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +00001329 afs_dir_remove_subdir(dentry);
David Howells63a46812018-04-06 14:17:25 +01001330 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1331 afs_edit_dir_remove(dvnode, &dentry->d_name,
1332 afs_edit_dir_for_rmdir);
1333 }
David Howells260a9802007-04-26 15:59:35 -07001334 }
1335
David Howells79ddbfa2019-04-25 14:26:51 +01001336 if (vnode)
1337 up_write(&vnode->rmdir_lock);
David Howellsf58db832018-10-20 00:57:58 +01001338error_key:
David Howells260a9802007-04-26 15:59:35 -07001339 key_put(key);
David Howellsd2ddc772017-11-02 15:27:50 +00001340error:
David Howellsa58823a2019-05-09 15:16:10 +01001341 kfree(scb);
David Howellsd2ddc772017-11-02 15:27:50 +00001342 return ret;
1343}
David Howells260a9802007-04-26 15:59:35 -07001344
David Howellsd2ddc772017-11-02 15:27:50 +00001345/*
1346 * Remove a link to a file or symlink from a directory.
1347 *
1348 * If the file was not deleted due to excess hard links, the fileserver will
1349 * break the callback promise on the file - if it had one - before it returns
1350 * to us, and if it was deleted, it won't
1351 *
1352 * However, if we didn't have a callback promise outstanding, or it was
1353 * outstanding on a different server, then it won't break it either...
1354 */
David Howellsa38a7552019-05-14 12:29:11 +01001355static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1356 struct key *key)
David Howellsd2ddc772017-11-02 15:27:50 +00001357{
1358 int ret = 0;
1359
1360 if (d_really_is_positive(dentry)) {
1361 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1362
David Howells30062bd2018-10-20 00:57:58 +01001363 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1364 /* Already done */
David Howellsa38a7552019-05-14 12:29:11 +01001365 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1366 write_seqlock(&vnode->cb_lock);
David Howells440fbc32018-01-02 10:02:19 +00001367 drop_nlink(&vnode->vfs_inode);
1368 if (vnode->vfs_inode.i_nlink == 0) {
1369 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsa38a7552019-05-14 12:29:11 +01001370 __afs_break_callback(vnode);
David Howells440fbc32018-01-02 10:02:19 +00001371 }
David Howellsa38a7552019-05-14 12:29:11 +01001372 write_sequnlock(&vnode->cb_lock);
David Howellsd2ddc772017-11-02 15:27:50 +00001373 ret = 0;
David Howells440fbc32018-01-02 10:02:19 +00001374 } else {
David Howellsa38a7552019-05-14 12:29:11 +01001375 afs_break_callback(vnode);
David Howells440fbc32018-01-02 10:02:19 +00001376
1377 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1378 kdebug("AFS_VNODE_DELETED");
1379
1380 ret = afs_validate(vnode, key);
1381 if (ret == -ESTALE)
1382 ret = 0;
1383 }
David Howellsd2ddc772017-11-02 15:27:50 +00001384 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1385 }
1386
1387 return ret;
1388}
1389
1390/*
1391 * Remove a file or symlink from an AFS filesystem.
1392 */
1393static int afs_unlink(struct inode *dir, struct dentry *dentry)
1394{
1395 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +01001396 struct afs_status_cb *scb;
David Howells30062bd2018-10-20 00:57:58 +01001397 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
David Howellsd2ddc772017-11-02 15:27:50 +00001398 struct key *key;
David Howells79ddbfa2019-04-25 14:26:51 +01001399 bool need_rehash = false;
David Howellsd2ddc772017-11-02 15:27:50 +00001400 int ret;
1401
David Howells3b6492d2018-10-20 00:57:57 +01001402 _enter("{%llx:%llu},{%pd}",
David Howellsd2ddc772017-11-02 15:27:50 +00001403 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1404
1405 if (dentry->d_name.len >= AFSNAMEMAX)
1406 return -ENAMETOOLONG;
1407
David Howellsa58823a2019-05-09 15:16:10 +01001408 ret = -ENOMEM;
1409 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1410 if (!scb)
1411 goto error;
1412
David Howellsd2ddc772017-11-02 15:27:50 +00001413 key = afs_request_key(dvnode->volume->cell);
1414 if (IS_ERR(key)) {
1415 ret = PTR_ERR(key);
David Howellsa58823a2019-05-09 15:16:10 +01001416 goto error_scb;
David Howellsd2ddc772017-11-02 15:27:50 +00001417 }
1418
1419 /* Try to make sure we have a callback promise on the victim. */
1420 if (d_really_is_positive(dentry)) {
1421 vnode = AFS_FS_I(d_inode(dentry));
1422 ret = afs_validate(vnode, key);
1423 if (ret < 0)
1424 goto error_key;
1425 }
1426
David Howells79ddbfa2019-04-25 14:26:51 +01001427 spin_lock(&dentry->d_lock);
1428 if (vnode && d_count(dentry) > 1) {
1429 spin_unlock(&dentry->d_lock);
1430 /* Start asynchronous writeout of the inode */
1431 write_inode_now(d_inode(dentry), 0);
1432 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1433 goto error_key;
1434 }
1435 if (!d_unhashed(dentry)) {
1436 /* Prevent a race with RCU lookup. */
1437 __d_drop(dentry);
1438 need_rehash = true;
1439 }
1440 spin_unlock(&dentry->d_lock);
1441
David Howellsd2ddc772017-11-02 15:27:50 +00001442 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001443 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001444 afs_dataversion_t data_version = dvnode->status.data_version + 1;
David Howellsa38a7552019-05-14 12:29:11 +01001445 afs_dataversion_t data_version_2 = vnode->status.data_version;
David Howellsa58823a2019-05-09 15:16:10 +01001446
David Howellsd2ddc772017-11-02 15:27:50 +00001447 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001448 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howellsa38a7552019-05-14 12:29:11 +01001449 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
David Howells30062bd2018-10-20 00:57:58 +01001450
1451 if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1452 !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1453 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
David Howellsa58823a2019-05-09 15:16:10 +01001454 &scb[0], &scb[1]);
David Howells30062bd2018-10-20 00:57:58 +01001455 if (fc.ac.error != -ECONNABORTED ||
1456 fc.ac.abort_code != RXGEN_OPCODE)
1457 continue;
1458 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1459 }
1460
David Howellsa58823a2019-05-09 15:16:10 +01001461 afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
David Howellsd2ddc772017-11-02 15:27:50 +00001462 }
1463
David Howellsa58823a2019-05-09 15:16:10 +01001464 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1465 &data_version, &scb[0]);
David Howellsa38a7552019-05-14 12:29:11 +01001466 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1467 &data_version_2, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001468 ret = afs_end_vnode_operation(&fc);
David Howellsa38a7552019-05-14 12:29:11 +01001469 if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1470 ret = afs_dir_remove_link(dvnode, dentry, key);
David Howells63a46812018-04-06 14:17:25 +01001471 if (ret == 0 &&
1472 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1473 afs_edit_dir_remove(dvnode, &dentry->d_name,
1474 afs_edit_dir_for_unlink);
David Howellsd2ddc772017-11-02 15:27:50 +00001475 }
1476
David Howells79ddbfa2019-04-25 14:26:51 +01001477 if (need_rehash && ret < 0 && ret != -ENOENT)
1478 d_rehash(dentry);
1479
David Howellsd2ddc772017-11-02 15:27:50 +00001480error_key:
David Howells260a9802007-04-26 15:59:35 -07001481 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001482error_scb:
1483 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001484error:
1485 _leave(" = %d", ret);
1486 return ret;
1487}
1488
1489/*
1490 * create a regular file on an AFS filesystem
1491 */
Al Viro4acdaf22011-07-26 01:42:34 -04001492static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04001493 bool excl)
David Howells260a9802007-04-26 15:59:35 -07001494{
David Howellsb8359152019-05-14 12:23:43 +01001495 struct afs_iget_data iget_data;
David Howellsd2ddc772017-11-02 15:27:50 +00001496 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +01001497 struct afs_status_cb *scb;
Colin Ian King43dd3882017-11-20 13:58:20 +00001498 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells260a9802007-04-26 15:59:35 -07001499 struct key *key;
1500 int ret;
1501
David Howellsd2ddc772017-11-02 15:27:50 +00001502 mode |= S_IFREG;
David Howells260a9802007-04-26 15:59:35 -07001503
David Howells3b6492d2018-10-20 00:57:57 +01001504 _enter("{%llx:%llu},{%pd},%ho,",
Al Viroa4555892014-10-21 20:11:25 -04001505 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001506
David Howellsd2ddc772017-11-02 15:27:50 +00001507 ret = -ENAMETOOLONG;
1508 if (dentry->d_name.len >= AFSNAMEMAX)
1509 goto error;
1510
David Howells260a9802007-04-26 15:59:35 -07001511 key = afs_request_key(dvnode->volume->cell);
1512 if (IS_ERR(key)) {
1513 ret = PTR_ERR(key);
1514 goto error;
1515 }
1516
David Howellsa58823a2019-05-09 15:16:10 +01001517 ret = -ENOMEM;
1518 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1519 if (!scb)
1520 goto error_scb;
1521
David Howellsd2ddc772017-11-02 15:27:50 +00001522 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001523 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001524 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1525
David Howellsd2ddc772017-11-02 15:27:50 +00001526 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001527 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howellsb8359152019-05-14 12:23:43 +01001528 afs_prep_for_new_inode(&fc, &iget_data);
David Howellsa58823a2019-05-09 15:16:10 +01001529 afs_fs_create(&fc, dentry->d_name.name, mode,
David Howellsb8359152019-05-14 12:23:43 +01001530 &scb[0], &iget_data.fid, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001531 }
David Howells260a9802007-04-26 15:59:35 -07001532
David Howellsa58823a2019-05-09 15:16:10 +01001533 afs_check_for_remote_deletion(&fc, dvnode);
1534 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1535 &data_version, &scb[0]);
David Howellsb8359152019-05-14 12:23:43 +01001536 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001537 ret = afs_end_vnode_operation(&fc);
1538 if (ret < 0)
1539 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001540 } else {
1541 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001542 }
1543
David Howells63a46812018-04-06 14:17:25 +01001544 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
David Howellsb8359152019-05-14 12:23:43 +01001545 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
David Howells63a46812018-04-06 14:17:25 +01001546 afs_edit_dir_for_create);
1547
David Howellsa58823a2019-05-09 15:16:10 +01001548 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001549 key_put(key);
1550 _leave(" = 0");
1551 return 0;
1552
David Howellsa58823a2019-05-09 15:16:10 +01001553error_scb:
1554 kfree(scb);
David Howellsd2ddc772017-11-02 15:27:50 +00001555error_key:
David Howells260a9802007-04-26 15:59:35 -07001556 key_put(key);
1557error:
1558 d_drop(dentry);
1559 _leave(" = %d", ret);
1560 return ret;
1561}
1562
1563/*
1564 * create a hard link between files in an AFS filesystem
1565 */
1566static int afs_link(struct dentry *from, struct inode *dir,
1567 struct dentry *dentry)
1568{
David Howellsd2ddc772017-11-02 15:27:50 +00001569 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +01001570 struct afs_status_cb *scb;
1571 struct afs_vnode *dvnode = AFS_FS_I(dir);
1572 struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
David Howells260a9802007-04-26 15:59:35 -07001573 struct key *key;
1574 int ret;
1575
David Howells3b6492d2018-10-20 00:57:57 +01001576 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001577 vnode->fid.vid, vnode->fid.vnode,
1578 dvnode->fid.vid, dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001579 dentry);
David Howells260a9802007-04-26 15:59:35 -07001580
David Howellsd2ddc772017-11-02 15:27:50 +00001581 ret = -ENAMETOOLONG;
1582 if (dentry->d_name.len >= AFSNAMEMAX)
1583 goto error;
1584
David Howellsa58823a2019-05-09 15:16:10 +01001585 ret = -ENOMEM;
1586 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1587 if (!scb)
1588 goto error;
1589
David Howells260a9802007-04-26 15:59:35 -07001590 key = afs_request_key(dvnode->volume->cell);
1591 if (IS_ERR(key)) {
1592 ret = PTR_ERR(key);
David Howellsa58823a2019-05-09 15:16:10 +01001593 goto error_scb;
David Howells260a9802007-04-26 15:59:35 -07001594 }
1595
David Howellsd2ddc772017-11-02 15:27:50 +00001596 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001597 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001598 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1599
David Howellsd2ddc772017-11-02 15:27:50 +00001600 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1601 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001602 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001603 }
David Howells260a9802007-04-26 15:59:35 -07001604
David Howellsd2ddc772017-11-02 15:27:50 +00001605 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001606 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1607 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
David Howellsa58823a2019-05-09 15:16:10 +01001608 afs_fs_link(&fc, vnode, dentry->d_name.name,
1609 &scb[0], &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001610 }
1611
David Howellsa58823a2019-05-09 15:16:10 +01001612 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1613 &data_version, &scb[0]);
1614 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1615 NULL, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001616 ihold(&vnode->vfs_inode);
1617 d_instantiate(dentry, &vnode->vfs_inode);
1618
1619 mutex_unlock(&vnode->io_lock);
1620 ret = afs_end_vnode_operation(&fc);
1621 if (ret < 0)
1622 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001623 } else {
1624 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001625 }
1626
David Howells63a46812018-04-06 14:17:25 +01001627 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1628 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1629 afs_edit_dir_for_link);
1630
David Howells260a9802007-04-26 15:59:35 -07001631 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001632 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001633 _leave(" = 0");
1634 return 0;
1635
David Howellsd2ddc772017-11-02 15:27:50 +00001636error_key:
David Howells260a9802007-04-26 15:59:35 -07001637 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001638error_scb:
1639 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001640error:
1641 d_drop(dentry);
1642 _leave(" = %d", ret);
1643 return ret;
1644}
1645
1646/*
1647 * create a symlink in an AFS filesystem
1648 */
1649static int afs_symlink(struct inode *dir, struct dentry *dentry,
1650 const char *content)
1651{
David Howellsb8359152019-05-14 12:23:43 +01001652 struct afs_iget_data iget_data;
David Howellsd2ddc772017-11-02 15:27:50 +00001653 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +01001654 struct afs_status_cb *scb;
David Howellsd2ddc772017-11-02 15:27:50 +00001655 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells260a9802007-04-26 15:59:35 -07001656 struct key *key;
1657 int ret;
1658
David Howells3b6492d2018-10-20 00:57:57 +01001659 _enter("{%llx:%llu},{%pd},%s",
Al Viroa4555892014-10-21 20:11:25 -04001660 dvnode->fid.vid, dvnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -07001661 content);
1662
David Howellsd2ddc772017-11-02 15:27:50 +00001663 ret = -ENAMETOOLONG;
1664 if (dentry->d_name.len >= AFSNAMEMAX)
1665 goto error;
1666
David Howells260a9802007-04-26 15:59:35 -07001667 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001668 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001669 goto error;
1670
David Howellsa58823a2019-05-09 15:16:10 +01001671 ret = -ENOMEM;
1672 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1673 if (!scb)
1674 goto error;
1675
David Howells260a9802007-04-26 15:59:35 -07001676 key = afs_request_key(dvnode->volume->cell);
1677 if (IS_ERR(key)) {
1678 ret = PTR_ERR(key);
David Howellsa58823a2019-05-09 15:16:10 +01001679 goto error_scb;
David Howells260a9802007-04-26 15:59:35 -07001680 }
1681
David Howellsd2ddc772017-11-02 15:27:50 +00001682 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001683 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001684 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1685
David Howellsd2ddc772017-11-02 15:27:50 +00001686 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001687 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howellsb8359152019-05-14 12:23:43 +01001688 afs_prep_for_new_inode(&fc, &iget_data);
David Howellsa58823a2019-05-09 15:16:10 +01001689 afs_fs_symlink(&fc, dentry->d_name.name, content,
David Howellsb8359152019-05-14 12:23:43 +01001690 &scb[0], &iget_data.fid, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001691 }
David Howells260a9802007-04-26 15:59:35 -07001692
David Howellsa58823a2019-05-09 15:16:10 +01001693 afs_check_for_remote_deletion(&fc, dvnode);
1694 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1695 &data_version, &scb[0]);
David Howellsb8359152019-05-14 12:23:43 +01001696 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001697 ret = afs_end_vnode_operation(&fc);
1698 if (ret < 0)
1699 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001700 } else {
1701 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001702 }
1703
David Howells63a46812018-04-06 14:17:25 +01001704 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
David Howellsb8359152019-05-14 12:23:43 +01001705 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
David Howells63a46812018-04-06 14:17:25 +01001706 afs_edit_dir_for_symlink);
1707
David Howells260a9802007-04-26 15:59:35 -07001708 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001709 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001710 _leave(" = 0");
1711 return 0;
1712
David Howellsd2ddc772017-11-02 15:27:50 +00001713error_key:
David Howells260a9802007-04-26 15:59:35 -07001714 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001715error_scb:
1716 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001717error:
1718 d_drop(dentry);
1719 _leave(" = %d", ret);
1720 return ret;
1721}
1722
1723/*
1724 * rename a file in an AFS filesystem and/or move it between directories
1725 */
1726static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001727 struct inode *new_dir, struct dentry *new_dentry,
1728 unsigned int flags)
David Howells260a9802007-04-26 15:59:35 -07001729{
David Howellsd2ddc772017-11-02 15:27:50 +00001730 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +01001731 struct afs_status_cb *scb;
David Howells260a9802007-04-26 15:59:35 -07001732 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
David Howells79ddbfa2019-04-25 14:26:51 +01001733 struct dentry *tmp = NULL, *rehash = NULL;
1734 struct inode *new_inode;
David Howells260a9802007-04-26 15:59:35 -07001735 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001736 bool new_negative = d_is_negative(new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001737 int ret;
1738
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001739 if (flags)
1740 return -EINVAL;
1741
David Howells79ddbfa2019-04-25 14:26:51 +01001742 /* Don't allow silly-rename files be moved around. */
1743 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1744 return -EINVAL;
1745
David Howells2b0143b2015-03-17 22:25:59 +00001746 vnode = AFS_FS_I(d_inode(old_dentry));
David Howells260a9802007-04-26 15:59:35 -07001747 orig_dvnode = AFS_FS_I(old_dir);
1748 new_dvnode = AFS_FS_I(new_dir);
1749
David Howells3b6492d2018-10-20 00:57:57 +01001750 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001751 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1752 vnode->fid.vid, vnode->fid.vnode,
1753 new_dvnode->fid.vid, new_dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001754 new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001755
David Howellsa58823a2019-05-09 15:16:10 +01001756 ret = -ENOMEM;
1757 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1758 if (!scb)
1759 goto error;
1760
David Howells260a9802007-04-26 15:59:35 -07001761 key = afs_request_key(orig_dvnode->volume->cell);
1762 if (IS_ERR(key)) {
1763 ret = PTR_ERR(key);
David Howellsa58823a2019-05-09 15:16:10 +01001764 goto error_scb;
David Howells260a9802007-04-26 15:59:35 -07001765 }
1766
David Howells79ddbfa2019-04-25 14:26:51 +01001767 /* For non-directories, check whether the target is busy and if so,
1768 * make a copy of the dentry and then do a silly-rename. If the
1769 * silly-rename succeeds, the copied dentry is hashed and becomes the
1770 * new target.
1771 */
1772 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1773 /* To prevent any new references to the target during the
1774 * rename, we unhash the dentry in advance.
1775 */
1776 if (!d_unhashed(new_dentry)) {
1777 d_drop(new_dentry);
1778 rehash = new_dentry;
1779 }
1780
1781 if (d_count(new_dentry) > 2) {
1782 /* copy the target dentry's name */
1783 ret = -ENOMEM;
1784 tmp = d_alloc(new_dentry->d_parent,
1785 &new_dentry->d_name);
1786 if (!tmp)
1787 goto error_rehash;
1788
1789 ret = afs_sillyrename(new_dvnode,
1790 AFS_FS_I(d_inode(new_dentry)),
1791 new_dentry, key);
1792 if (ret)
1793 goto error_rehash;
1794
1795 new_dentry = tmp;
1796 rehash = NULL;
1797 new_negative = true;
David Howells79ddbfa2019-04-25 14:26:51 +01001798 }
1799 }
1800
David Howellsd2ddc772017-11-02 15:27:50 +00001801 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +01001802 if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +01001803 afs_dataversion_t orig_data_version;
1804 afs_dataversion_t new_data_version;
1805 struct afs_status_cb *new_scb = &scb[1];
1806
1807 orig_data_version = orig_dvnode->status.data_version + 1;
1808
David Howellsd2ddc772017-11-02 15:27:50 +00001809 if (orig_dvnode != new_dvnode) {
1810 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1811 afs_end_vnode_operation(&fc);
David Howells79ddbfa2019-04-25 14:26:51 +01001812 goto error_rehash;
David Howellsd2ddc772017-11-02 15:27:50 +00001813 }
David Howellsa58823a2019-05-09 15:16:10 +01001814 new_data_version = new_dvnode->status.data_version;
1815 } else {
1816 new_data_version = orig_data_version;
1817 new_scb = &scb[0];
David Howellsd2ddc772017-11-02 15:27:50 +00001818 }
David Howellsa58823a2019-05-09 15:16:10 +01001819
David Howellsd2ddc772017-11-02 15:27:50 +00001820 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001821 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1822 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
David Howellsd2ddc772017-11-02 15:27:50 +00001823 afs_fs_rename(&fc, old_dentry->d_name.name,
David Howells63a46812018-04-06 14:17:25 +01001824 new_dvnode, new_dentry->d_name.name,
David Howellsa58823a2019-05-09 15:16:10 +01001825 &scb[0], new_scb);
David Howellsd2ddc772017-11-02 15:27:50 +00001826 }
1827
David Howellsa58823a2019-05-09 15:16:10 +01001828 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1829 &orig_data_version, &scb[0]);
1830 if (new_dvnode != orig_dvnode) {
1831 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1832 &new_data_version, &scb[1]);
David Howellsd2ddc772017-11-02 15:27:50 +00001833 mutex_unlock(&new_dvnode->io_lock);
David Howellsa58823a2019-05-09 15:16:10 +01001834 }
David Howellsd2ddc772017-11-02 15:27:50 +00001835 ret = afs_end_vnode_operation(&fc);
1836 if (ret < 0)
David Howells79ddbfa2019-04-25 14:26:51 +01001837 goto error_rehash;
David Howellsd2ddc772017-11-02 15:27:50 +00001838 }
1839
David Howells63a46812018-04-06 14:17:25 +01001840 if (ret == 0) {
David Howells79ddbfa2019-04-25 14:26:51 +01001841 if (rehash)
1842 d_rehash(rehash);
David Howells63a46812018-04-06 14:17:25 +01001843 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1844 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
David Howells79ddbfa2019-04-25 14:26:51 +01001845 afs_edit_dir_for_rename_0);
David Howells63a46812018-04-06 14:17:25 +01001846
1847 if (!new_negative &&
1848 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1849 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
David Howells79ddbfa2019-04-25 14:26:51 +01001850 afs_edit_dir_for_rename_1);
David Howells63a46812018-04-06 14:17:25 +01001851
1852 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1853 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
David Howells79ddbfa2019-04-25 14:26:51 +01001854 &vnode->fid, afs_edit_dir_for_rename_2);
1855
1856 new_inode = d_inode(new_dentry);
1857 if (new_inode) {
1858 spin_lock(&new_inode->i_lock);
1859 if (new_inode->i_nlink > 0)
1860 drop_nlink(new_inode);
1861 spin_unlock(&new_inode->i_lock);
1862 }
1863 d_move(old_dentry, new_dentry);
1864 goto error_tmp;
David Howells63a46812018-04-06 14:17:25 +01001865 }
1866
David Howells79ddbfa2019-04-25 14:26:51 +01001867error_rehash:
1868 if (rehash)
1869 d_rehash(rehash);
1870error_tmp:
1871 if (tmp)
1872 dput(tmp);
David Howells260a9802007-04-26 15:59:35 -07001873 key_put(key);
David Howellsa58823a2019-05-09 15:16:10 +01001874error_scb:
1875 kfree(scb);
David Howells260a9802007-04-26 15:59:35 -07001876error:
David Howells260a9802007-04-26 15:59:35 -07001877 _leave(" = %d", ret);
1878 return ret;
1879}
David Howellsf3ddee82018-04-06 14:17:25 +01001880
1881/*
1882 * Release a directory page and clean up its private state if it's not busy
1883 * - return true if the page can now be released, false if not
1884 */
1885static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1886{
1887 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1888
David Howells3b6492d2018-10-20 00:57:57 +01001889 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
David Howellsf3ddee82018-04-06 14:17:25 +01001890
1891 set_page_private(page, 0);
1892 ClearPagePrivate(page);
1893
1894 /* The directory will need reloading. */
1895 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1896 afs_stat_v(dvnode, n_relpg);
1897 return 1;
1898}
1899
1900/*
1901 * invalidate part or all of a page
1902 * - release a page and clean up its private data if offset is 0 (indicating
1903 * the entire page)
1904 */
1905static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1906 unsigned int length)
1907{
1908 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1909
1910 _enter("{%lu},%u,%u", page->index, offset, length);
1911
1912 BUG_ON(!PageLocked(page));
1913
1914 /* The directory will need reloading. */
1915 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1916 afs_stat_v(dvnode, n_inval);
1917
1918 /* we clean up only if the entire page is being invalidated */
1919 if (offset == 0 && length == PAGE_SIZE) {
1920 set_page_private(page, 0);
1921 ClearPagePrivate(page);
1922 }
1923}