blob: 43dea3b00c29b9dc93b6416e8e869b4f8d726c28 [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);
David Howellsf51375c2018-10-20 00:57:57 +0100141 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto error;
143 }
David Howells63a46812018-04-06 14:17:25 +0100144
145 /* Make sure each block is NUL terminated so we can reasonably
146 * use string functions on it. The filenames in the page
147 * *should* be NUL-terminated anyway.
148 */
149 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
David Howells63a46812018-04-06 14:17:25 +0100152 kunmap(page);
153
David Howellsdab17c12017-11-02 15:27:52 +0000154checked:
David Howellsf3ddee82018-04-06 14:17:25 +0100155 afs_stat_v(dvnode, n_read_dir);
Al Virobe5b82d2016-04-22 15:06:44 -0400156 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David Howellsec268152007-04-26 15:49:28 -0700158error:
Al Virobe5b82d2016-04-22 15:06:44 -0400159 return false;
David Howellsec268152007-04-26 15:49:28 -0700160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * open an AFS directory file
164 */
165static int afs_dir_open(struct inode *inode, struct file *file)
166{
167 _enter("{%lu}", inode->i_ino);
168
David Howells00317632018-04-06 14:17:25 +0100169 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
170 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Howells08e0e7c2007-04-26 15:55:03 -0700172 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -ENOENT;
174
David Howells00d3b7a2007-04-26 15:57:07 -0700175 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
David Howellsf3ddee82018-04-06 14:17:25 +0100179 * Read the directory into the pagecache in one go, scrubbing the previous
180 * contents. The list of pages is returned, pinning them so that they don't
181 * get reclaimed during the iteration.
182 */
183static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
David Howellsb61f7dc2018-04-27 20:46:22 +0100184 __acquires(&dvnode->validate_lock)
David Howellsf3ddee82018-04-06 14:17:25 +0100185{
186 struct afs_read *req;
187 loff_t i_size;
188 int nr_pages, nr_inline, i, n;
189 int ret = -ENOMEM;
190
191retry:
192 i_size = i_size_read(&dvnode->vfs_inode);
193 if (i_size < 2048)
David Howellsf51375c2018-10-20 00:57:57 +0100194 return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
195 if (i_size > 2048 * 1024) {
196 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
David Howellsf3ddee82018-04-06 14:17:25 +0100197 return ERR_PTR(-EFBIG);
David Howellsf51375c2018-10-20 00:57:57 +0100198 }
David Howellsf3ddee82018-04-06 14:17:25 +0100199
200 _enter("%llu", i_size);
201
202 /* Get a request record to hold the page list. We want to hold it
203 * inline if we can, but we don't want to make an order 1 allocation.
204 */
205 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
206 nr_inline = nr_pages;
207 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
208 nr_inline = 0;
209
210 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
211 GFP_KERNEL);
212 if (!req)
213 return ERR_PTR(-ENOMEM);
214
215 refcount_set(&req->usage, 1);
216 req->nr_pages = nr_pages;
217 req->actual_len = i_size; /* May change */
218 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
219 req->data_version = dvnode->status.data_version; /* May change */
220 if (nr_inline > 0) {
221 req->pages = req->array;
222 } else {
223 req->pages = kcalloc(nr_pages, sizeof(struct page *),
224 GFP_KERNEL);
225 if (!req->pages)
226 goto error;
227 }
228
229 /* Get a list of all the pages that hold or will hold the directory
230 * content. We need to fill in any gaps that we might find where the
231 * memory reclaimer has been at work. If there are any gaps, we will
232 * need to reread the entire directory contents.
233 */
234 i = 0;
235 do {
236 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
237 req->nr_pages - i,
238 req->pages + i);
239 _debug("find %u at %u/%u", n, i, req->nr_pages);
240 if (n == 0) {
241 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
242
243 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
244 afs_stat_v(dvnode, n_inval);
245
246 ret = -ENOMEM;
247 req->pages[i] = __page_cache_alloc(gfp);
248 if (!req->pages[i])
249 goto error;
250 ret = add_to_page_cache_lru(req->pages[i],
251 dvnode->vfs_inode.i_mapping,
252 i, gfp);
253 if (ret < 0)
254 goto error;
255
256 set_page_private(req->pages[i], 1);
257 SetPagePrivate(req->pages[i]);
258 unlock_page(req->pages[i]);
259 i++;
260 } else {
261 i += n;
262 }
263 } while (i < req->nr_pages);
264
265 /* If we're going to reload, we need to lock all the pages to prevent
266 * races.
267 */
David Howellsb61f7dc2018-04-27 20:46:22 +0100268 ret = -ERESTARTSYS;
269 if (down_read_killable(&dvnode->validate_lock) < 0)
270 goto error;
271
272 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
273 goto success;
274
275 up_read(&dvnode->validate_lock);
276 if (down_write_killable(&dvnode->validate_lock) < 0)
277 goto error;
278
David Howellsf3ddee82018-04-06 14:17:25 +0100279 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
David Howellsf3ddee82018-04-06 14:17:25 +0100280 ret = afs_fetch_data(dvnode, key, req);
281 if (ret < 0)
David Howellsb61f7dc2018-04-27 20:46:22 +0100282 goto error_unlock;
David Howellsf3ddee82018-04-06 14:17:25 +0100283
284 task_io_account_read(PAGE_SIZE * req->nr_pages);
285
286 if (req->len < req->file_size)
287 goto content_has_grown;
288
289 /* Validate the data we just read. */
290 ret = -EIO;
291 for (i = 0; i < req->nr_pages; i++)
292 if (!afs_dir_check_page(dvnode, req->pages[i],
293 req->actual_len))
David Howellsb61f7dc2018-04-27 20:46:22 +0100294 goto error_unlock;
David Howellsf3ddee82018-04-06 14:17:25 +0100295
296 // TODO: Trim excess pages
297
298 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
299 }
300
David Howellsb61f7dc2018-04-27 20:46:22 +0100301 downgrade_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100302success:
David Howellsf3ddee82018-04-06 14:17:25 +0100303 return req;
304
David Howellsf3ddee82018-04-06 14:17:25 +0100305error_unlock:
David Howellsb61f7dc2018-04-27 20:46:22 +0100306 up_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100307error:
308 afs_put_read(req);
309 _leave(" = %d", ret);
310 return ERR_PTR(ret);
311
312content_has_grown:
David Howellsb61f7dc2018-04-27 20:46:22 +0100313 up_write(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100314 afs_put_read(req);
315 goto retry;
316}
317
318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * deal with one block in an AFS directory
320 */
David Howellsf51375c2018-10-20 00:57:57 +0100321static int afs_dir_iterate_block(struct afs_vnode *dvnode,
322 struct dir_context *ctx,
David Howells00317632018-04-06 14:17:25 +0100323 union afs_xdr_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400324 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
David Howells00317632018-04-06 14:17:25 +0100326 union afs_xdr_dirent *dire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned offset, next, curr;
328 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400329 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Al Viro1bbae9f2013-05-22 16:31:14 -0400331 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
David Howells00317632018-04-06 14:17:25 +0100333 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* walk through the block, an entry at a time */
David Howells4ea219a2018-04-06 14:17:25 +0100336 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
337 offset < AFS_DIR_SLOTS_PER_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 offset = next
339 ) {
340 next = offset + 1;
341
342 /* skip entries marked unused in the bitmap */
David Howells00317632018-04-06 14:17:25 +0100343 if (!(block->hdr.bitmap[offset / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 (1 << (offset % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800345 _debug("ENT[%zu.%u]: unused",
David Howells00317632018-04-06 14:17:25 +0100346 blkoff / sizeof(union afs_xdr_dir_block), offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (offset >= curr)
Al Viro1bbae9f2013-05-22 16:31:14 -0400348 ctx->pos = blkoff +
David Howells00317632018-04-06 14:17:25 +0100349 next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 continue;
351 }
352
353 /* got a valid entry */
354 dire = &block->dirents[offset];
355 nlen = strnlen(dire->u.name,
356 sizeof(*block) -
David Howells00317632018-04-06 14:17:25 +0100357 offset * sizeof(union afs_xdr_dirent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800359 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
David Howells00317632018-04-06 14:17:25 +0100360 blkoff / sizeof(union afs_xdr_dir_block), offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 (offset < curr ? "skip" : "fill"),
362 nlen, dire->u.name);
363
364 /* work out where the next possible entry is */
David Howells00317632018-04-06 14:17:25 +0100365 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
David Howells4ea219a2018-04-06 14:17:25 +0100366 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800367 _debug("ENT[%zu.%u]:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 " %u travelled beyond end dir block"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800369 " (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100370 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 offset, next, tmp, nlen);
David Howellsf51375c2018-10-20 00:57:57 +0100372 return afs_bad(dvnode, afs_file_error_dir_over_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
David Howells00317632018-04-06 14:17:25 +0100374 if (!(block->hdr.bitmap[next / 8] &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 (1 << (next % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800376 _debug("ENT[%zu.%u]:"
377 " %u unmarked extension (len %u/%zu)",
David Howells00317632018-04-06 14:17:25 +0100378 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 offset, next, tmp, nlen);
David Howellsf51375c2018-10-20 00:57:57 +0100380 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800383 _debug("ENT[%zu.%u]: ext %u/%zu",
David Howells00317632018-04-06 14:17:25 +0100384 blkoff / sizeof(union afs_xdr_dir_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 next, tmp, nlen);
386 next++;
387 }
388
389 /* skip if starts before the current position */
390 if (offset < curr)
391 continue;
392
393 /* found the next entry */
Al Viro1bbae9f2013-05-22 16:31:14 -0400394 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ntohl(dire->u.vnode),
David Howells5cf9dd52018-04-09 21:12:31 +0100396 (ctx->actor == afs_lookup_filldir ||
397 ctx->actor == afs_lookup_one_filldir)?
Al Viro1bbae9f2013-05-22 16:31:14 -0400398 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 _leave(" = 0 [full]");
400 return 0;
401 }
402
David Howells00317632018-04-06 14:17:25 +0100403 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
406 _leave(" = 1 [more]");
407 return 1;
David Howellsec268152007-04-26 15:49:28 -0700408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
David Howells08e0e7c2007-04-26 15:55:03 -0700411 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400413static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
414 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
David Howellsf3ddee82018-04-06 14:17:25 +0100416 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells00317632018-04-06 14:17:25 +0100417 struct afs_xdr_dir_page *dbuf;
418 union afs_xdr_dir_block *dblock;
David Howellsf3ddee82018-04-06 14:17:25 +0100419 struct afs_read *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct page *page;
421 unsigned blkoff, limit;
422 int ret;
423
Al Viro1bbae9f2013-05-22 16:31:14 -0400424 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Howells08e0e7c2007-04-26 15:55:03 -0700426 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 _leave(" = -ESTALE");
428 return -ESTALE;
429 }
430
David Howellsf3ddee82018-04-06 14:17:25 +0100431 req = afs_read_dir(dvnode, key);
432 if (IS_ERR(req))
433 return PTR_ERR(req);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* round the file position up to the next entry boundary */
David Howells00317632018-04-06 14:17:25 +0100436 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
437 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* walk through the blocks in sequence */
440 ret = 0;
David Howellsf3ddee82018-04-06 14:17:25 +0100441 while (ctx->pos < req->actual_len) {
David Howells00317632018-04-06 14:17:25 +0100442 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Howellsf3ddee82018-04-06 14:17:25 +0100444 /* Fetch the appropriate page from the directory and re-add it
445 * to the LRU.
446 */
447 page = req->pages[blkoff / PAGE_SIZE];
448 if (!page) {
David Howellsf51375c2018-10-20 00:57:57 +0100449 ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 }
David Howellsf3ddee82018-04-06 14:17:25 +0100452 mark_page_accessed(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 limit = blkoff & ~(PAGE_SIZE - 1);
455
David Howellsf3ddee82018-04-06 14:17:25 +0100456 dbuf = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* deal with the individual blocks stashed on this page */
459 do {
460 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
David Howells00317632018-04-06 14:17:25 +0100461 sizeof(union afs_xdr_dir_block)];
David Howellsf51375c2018-10-20 00:57:57 +0100462 ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ret != 1) {
David Howellsf3ddee82018-04-06 14:17:25 +0100464 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
466 }
467
David Howells00317632018-04-06 14:17:25 +0100468 blkoff += sizeof(union afs_xdr_dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Al Viro1bbae9f2013-05-22 16:31:14 -0400470 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
David Howellsf3ddee82018-04-06 14:17:25 +0100472 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 ret = 0;
474 }
475
David Howellsec268152007-04-26 15:49:28 -0700476out:
David Howellsb61f7dc2018-04-27 20:46:22 +0100477 up_read(&dvnode->validate_lock);
David Howellsf3ddee82018-04-06 14:17:25 +0100478 afs_put_read(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 _leave(" = %d", ret);
480 return ret;
David Howellsec268152007-04-26 15:49:28 -0700481}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*
484 * read an AFS directory
485 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400486static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
David Howells215804a2017-11-02 15:27:52 +0000488 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
David Howellsec268152007-04-26 15:49:28 -0700489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
David Howells5cf9dd52018-04-09 21:12:31 +0100492 * Search the directory for a single name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
494 * uniquifier through dtype
495 */
David Howells5cf9dd52018-04-09 21:12:31 +0100496static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
497 int nlen, loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
David Howells5cf9dd52018-04-09 21:12:31 +0100499 struct afs_lookup_one_cookie *cookie =
500 container_of(ctx, struct afs_lookup_one_cookie, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Al Viro1bbae9f2013-05-22 16:31:14 -0400502 _enter("{%s,%u},%s,%u,,%llu,%u",
503 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700504 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David Howells08e0e7c2007-04-26 15:55:03 -0700506 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100507 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
508 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells08e0e7c2007-04-26 15:55:03 -0700509
Al Viro1bbae9f2013-05-22 16:31:14 -0400510 if (cookie->name.len != nlen ||
511 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 _leave(" = 0 [no]");
513 return 0;
514 }
515
516 cookie->fid.vnode = ino;
517 cookie->fid.unique = dtype;
518 cookie->found = 1;
519
520 _leave(" = -1 [found]");
521 return -1;
David Howellsec268152007-04-26 15:49:28 -0700522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
David Howells5cf9dd52018-04-09 21:12:31 +0100525 * Do a lookup of a single name in a directory
David Howells260a9802007-04-26 15:59:35 -0700526 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
David Howells5cf9dd52018-04-09 21:12:31 +0100528static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
529 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Al Viro1bbae9f2013-05-22 16:31:14 -0400531 struct afs_super_info *as = dir->i_sb->s_fs_info;
David Howells5cf9dd52018-04-09 21:12:31 +0100532 struct afs_lookup_one_cookie cookie = {
533 .ctx.actor = afs_lookup_one_filldir,
Al Viro1bbae9f2013-05-22 16:31:14 -0400534 .name = dentry->d_name,
535 .fid.vid = as->volume->vid
536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int ret;
538
Al Viroa4555892014-10-21 20:11:25 -0400539 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400542 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700544 _leave(" = %d [iter]", ret);
545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
548 ret = -ENOENT;
549 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700550 _leave(" = -ENOENT [not found]");
551 return -ENOENT;
552 }
553
554 *fid = cookie.fid;
David Howells3b6492d2018-10-20 00:57:57 +0100555 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
David Howells08e0e7c2007-04-26 15:55:03 -0700556 return 0;
557}
558
559/*
David Howells5cf9dd52018-04-09 21:12:31 +0100560 * search the directory for a name
561 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
562 * uniquifier through dtype
563 */
564static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
565 int nlen, loff_t fpos, u64 ino, unsigned dtype)
566{
567 struct afs_lookup_cookie *cookie =
568 container_of(ctx, struct afs_lookup_cookie, ctx);
569 int ret;
570
571 _enter("{%s,%u},%s,%u,,%llu,%u",
572 cookie->name.name, cookie->name.len, name, nlen,
573 (unsigned long long) ino, dtype);
574
575 /* insanity checks first */
David Howells00317632018-04-06 14:17:25 +0100576 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
577 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
David Howells5cf9dd52018-04-09 21:12:31 +0100578
579 if (cookie->found) {
580 if (cookie->nr_fids < 50) {
581 cookie->fids[cookie->nr_fids].vnode = ino;
582 cookie->fids[cookie->nr_fids].unique = dtype;
583 cookie->nr_fids++;
584 }
585 } else if (cookie->name.len == nlen &&
586 memcmp(cookie->name.name, name, nlen) == 0) {
587 cookie->fids[0].vnode = ino;
588 cookie->fids[0].unique = dtype;
589 cookie->found = 1;
590 if (cookie->one_only)
591 return -1;
592 }
593
594 ret = cookie->nr_fids >= 50 ? -1 : 0;
595 _leave(" = %d", ret);
596 return ret;
597}
598
599/*
600 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
601 * files in one go and create inodes for them. The inode of the file we were
602 * asked for is returned.
603 */
604static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
605 struct key *key)
606{
607 struct afs_lookup_cookie *cookie;
608 struct afs_cb_interest *cbi = NULL;
609 struct afs_super_info *as = dir->i_sb->s_fs_info;
610 struct afs_iget_data data;
611 struct afs_fs_cursor fc;
612 struct afs_vnode *dvnode = AFS_FS_I(dir);
613 struct inode *inode = NULL;
614 int ret, i;
615
616 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
617
618 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
619 if (!cookie)
620 return ERR_PTR(-ENOMEM);
621
622 cookie->ctx.actor = afs_lookup_filldir;
623 cookie->name = dentry->d_name;
624 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
625
626 read_seqlock_excl(&dvnode->cb_lock);
627 if (dvnode->cb_interest &&
628 dvnode->cb_interest->server &&
629 test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
630 cookie->one_only = true;
631 read_sequnlock_excl(&dvnode->cb_lock);
632
633 for (i = 0; i < 50; i++)
634 cookie->fids[i].vid = as->volume->vid;
635
636 /* search the directory */
637 ret = afs_dir_iterate(dir, &cookie->ctx, key);
638 if (ret < 0) {
639 inode = ERR_PTR(ret);
640 goto out;
641 }
642
643 inode = ERR_PTR(-ENOENT);
644 if (!cookie->found)
645 goto out;
646
647 /* Check to see if we already have an inode for the primary fid. */
648 data.volume = dvnode->volume;
649 data.fid = cookie->fids[0];
650 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
651 if (inode)
652 goto out;
653
654 /* Need space for examining all the selected files */
655 inode = ERR_PTR(-ENOMEM);
656 cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
657 GFP_KERNEL);
658 if (!cookie->statuses)
659 goto out;
660
661 cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
662 GFP_KERNEL);
663 if (!cookie->callbacks)
664 goto out_s;
665
666 /* Try FS.InlineBulkStatus first. Abort codes for the individual
667 * lookups contained therein are stored in the reply without aborting
668 * the whole operation.
669 */
670 if (cookie->one_only)
671 goto no_inline_bulk_status;
672
673 inode = ERR_PTR(-ERESTARTSYS);
674 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
675 while (afs_select_fileserver(&fc)) {
676 if (test_bit(AFS_SERVER_FL_NO_IBULK,
677 &fc.cbi->server->flags)) {
678 fc.ac.abort_code = RX_INVALID_OPERATION;
679 fc.ac.error = -ECONNABORTED;
680 break;
681 }
682 afs_fs_inline_bulk_status(&fc,
683 afs_v2net(dvnode),
684 cookie->fids,
685 cookie->statuses,
686 cookie->callbacks,
687 cookie->nr_fids, NULL);
688 }
689
690 if (fc.ac.error == 0)
691 cbi = afs_get_cb_interest(fc.cbi);
692 if (fc.ac.abort_code == RX_INVALID_OPERATION)
693 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
694 inode = ERR_PTR(afs_end_vnode_operation(&fc));
695 }
696
697 if (!IS_ERR(inode))
698 goto success;
699 if (fc.ac.abort_code != RX_INVALID_OPERATION)
700 goto out_c;
701
702no_inline_bulk_status:
703 /* We could try FS.BulkStatus next, but this aborts the entire op if
704 * any of the lookups fails - so, for the moment, revert to
705 * FS.FetchStatus for just the primary fid.
706 */
707 cookie->nr_fids = 1;
708 inode = ERR_PTR(-ERESTARTSYS);
709 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
710 while (afs_select_fileserver(&fc)) {
711 afs_fs_fetch_status(&fc,
712 afs_v2net(dvnode),
713 cookie->fids,
714 cookie->statuses,
715 cookie->callbacks,
716 NULL);
717 }
718
719 if (fc.ac.error == 0)
720 cbi = afs_get_cb_interest(fc.cbi);
721 inode = ERR_PTR(afs_end_vnode_operation(&fc));
722 }
723
724 if (IS_ERR(inode))
725 goto out_c;
726
727 for (i = 0; i < cookie->nr_fids; i++)
728 cookie->statuses[i].abort_code = 0;
729
730success:
731 /* Turn all the files into inodes and save the first one - which is the
732 * one we actually want.
733 */
734 if (cookie->statuses[0].abort_code != 0)
735 inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
736
737 for (i = 0; i < cookie->nr_fids; i++) {
738 struct inode *ti;
739
740 if (cookie->statuses[i].abort_code != 0)
741 continue;
742
743 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
744 &cookie->statuses[i],
745 &cookie->callbacks[i],
746 cbi);
747 if (i == 0) {
748 inode = ti;
749 } else {
750 if (!IS_ERR(ti))
751 iput(ti);
752 }
753 }
754
755out_c:
756 afs_put_cb_interest(afs_v2net(dvnode), cbi);
757 kfree(cookie->callbacks);
758out_s:
759 kfree(cookie->statuses);
760out:
761 kfree(cookie);
762 return inode;
763}
764
765/*
David Howells6f8880d2018-04-09 21:12:31 +0100766 * Look up an entry in a directory with @sys substitution.
767 */
768static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
769 struct key *key)
770{
771 struct afs_sysnames *subs;
772 struct afs_net *net = afs_i2net(dir);
773 struct dentry *ret;
774 char *buf, *p, *name;
775 int len, i;
776
777 _enter("");
778
779 ret = ERR_PTR(-ENOMEM);
780 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
781 if (!buf)
782 goto out_p;
783 if (dentry->d_name.len > 4) {
784 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
785 p += dentry->d_name.len - 4;
786 }
787
788 /* There is an ordered list of substitutes that we have to try. */
789 read_lock(&net->sysnames_lock);
790 subs = net->sysnames;
791 refcount_inc(&subs->usage);
792 read_unlock(&net->sysnames_lock);
793
794 for (i = 0; i < subs->nr; i++) {
795 name = subs->subs[i];
796 len = dentry->d_name.len - 4 + strlen(name);
797 if (len >= AFSNAMEMAX) {
798 ret = ERR_PTR(-ENAMETOOLONG);
799 goto out_s;
800 }
801
802 strcpy(p, name);
803 ret = lookup_one_len(buf, dentry->d_parent, len);
804 if (IS_ERR(ret) || d_is_positive(ret))
805 goto out_s;
806 dput(ret);
807 }
808
809 /* We don't want to d_add() the @sys dentry here as we don't want to
810 * the cached dentry to hide changes to the sysnames list.
811 */
812 ret = NULL;
813out_s:
814 afs_put_sysnames(subs);
815 kfree(buf);
816out_p:
817 key_put(key);
818 return ret;
819}
820
821/*
David Howells08e0e7c2007-04-26 15:55:03 -0700822 * look up an entry in a directory
823 */
David Howells260a9802007-04-26 15:59:35 -0700824static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400825 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700826{
David Howells5cf9dd52018-04-09 21:12:31 +0100827 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells08e0e7c2007-04-26 15:55:03 -0700828 struct inode *inode;
Al Viro34b2a882018-06-24 10:43:51 -0400829 struct dentry *d;
David Howells00d3b7a2007-04-26 15:57:07 -0700830 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700831 int ret;
832
David Howells3b6492d2018-10-20 00:57:57 +0100833 _enter("{%llx:%llu},%p{%pd},",
David Howells5cf9dd52018-04-09 21:12:31 +0100834 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
David Howells260a9802007-04-26 15:59:35 -0700835
David Howells2b0143b2015-03-17 22:25:59 +0000836 ASSERTCMP(d_inode(dentry), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700837
David Howells45222b92007-05-10 22:22:20 -0700838 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700839 _leave(" = -ENAMETOOLONG");
840 return ERR_PTR(-ENAMETOOLONG);
841 }
842
David Howells5cf9dd52018-04-09 21:12:31 +0100843 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700844 _leave(" = -ESTALE");
845 return ERR_PTR(-ESTALE);
846 }
847
David Howells5cf9dd52018-04-09 21:12:31 +0100848 key = afs_request_key(dvnode->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700849 if (IS_ERR(key)) {
850 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800851 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700852 }
853
David Howells5cf9dd52018-04-09 21:12:31 +0100854 ret = afs_validate(dvnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700855 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700856 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700857 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ERR_PTR(ret);
859 }
860
David Howells6f8880d2018-04-09 21:12:31 +0100861 if (dentry->d_name.len >= 4 &&
862 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
863 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
864 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
865 dentry->d_name.name[dentry->d_name.len - 1] == 's')
866 return afs_lookup_atsys(dir, dentry, key);
867
David Howellsd55b4da2018-04-06 14:17:24 +0100868 afs_stat_v(dvnode, n_lookup);
David Howells5cf9dd52018-04-09 21:12:31 +0100869 inode = afs_do_lookup(dir, dentry, key);
David Howells00d3b7a2007-04-26 15:57:07 -0700870 key_put(key);
Al Viro34b2a882018-06-24 10:43:51 -0400871 if (inode == ERR_PTR(-ENOENT)) {
872 inode = afs_try_auto_mntpt(dentry, dir);
Al Viro34b2a882018-06-24 10:43:51 -0400873 } else {
874 dentry->d_fsdata =
875 (void *)(unsigned long)dvnode->status.data_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Al Viro34b2a882018-06-24 10:43:51 -0400877 d = d_splice_alias(inode, dentry);
878 if (!IS_ERR_OR_NULL(d))
879 d->d_fsdata = dentry->d_fsdata;
880 return d;
David Howellsec268152007-04-26 15:49:28 -0700881}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
884 * check that a dentry lookup hit has found a valid entry
885 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
886 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Al Viro0b728e12012-06-10 16:03:43 -0400888static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
David Howells260a9802007-04-26 15:59:35 -0700890 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100891 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct dentry *parent;
David Howellsc435ee32017-11-02 15:27:49 +0000893 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700894 struct key *key;
David Howellsa4ff7402018-04-06 14:17:24 +0100895 long dir_version, de_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int ret;
897
Al Viro0b728e12012-06-10 16:03:43 -0400898 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100899 return -ECHILD;
900
David Howellsc435ee32017-11-02 15:27:49 +0000901 if (d_really_is_positive(dentry)) {
902 vnode = AFS_FS_I(d_inode(dentry));
David Howells3b6492d2018-10-20 00:57:57 +0100903 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
Al Viroa4555892014-10-21 20:11:25 -0400904 vnode->fid.vid, vnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -0700905 vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000906 } else {
Al Viroa4555892014-10-21 20:11:25 -0400907 _enter("{neg n=%pd}", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
David Howells260a9802007-04-26 15:59:35 -0700910 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700911 if (IS_ERR(key))
912 key = NULL;
913
David Howellsc435ee32017-11-02 15:27:49 +0000914 if (d_really_is_positive(dentry)) {
915 inode = d_inode(dentry);
916 if (inode) {
917 vnode = AFS_FS_I(inode);
918 afs_validate(vnode, key);
919 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
920 goto out_bad;
921 }
922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700925 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000926 dir = AFS_FS_I(d_inode(parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
David Howells260a9802007-04-26 15:59:35 -0700928 /* validate the parent directory */
David Howellsc435ee32017-11-02 15:27:49 +0000929 afs_validate(dir, key);
David Howells260a9802007-04-26 15:59:35 -0700930
931 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Al Viroa4555892014-10-21 20:11:25 -0400932 _debug("%pd: parent dir deleted", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000933 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
David Howellsa4ff7402018-04-06 14:17:24 +0100936 /* We only need to invalidate a dentry if the server's copy changed
937 * behind our back. If we made the change, it's no problem. Note that
938 * on a 32-bit system, we only have 32 bits in the dentry to store the
939 * version.
940 */
941 dir_version = (long)dir->status.data_version;
942 de_version = (long)dentry->d_fsdata;
943 if (de_version == dir_version)
944 goto out_valid;
945
946 dir_version = (long)dir->invalid_before;
947 if (de_version - dir_version >= 0)
948 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
David Howells260a9802007-04-26 15:59:35 -0700950 _debug("dir modified");
David Howellsd55b4da2018-04-06 14:17:24 +0100951 afs_stat_v(dir, n_reval);
David Howells260a9802007-04-26 15:59:35 -0700952
953 /* search the directory for this vnode */
David Howells5cf9dd52018-04-09 21:12:31 +0100954 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
David Howells260a9802007-04-26 15:59:35 -0700955 switch (ret) {
956 case 0:
957 /* the filename maps to something */
David Howells2b0143b2015-03-17 22:25:59 +0000958 if (d_really_is_negative(dentry))
David Howellsc435ee32017-11-02 15:27:49 +0000959 goto out_bad_parent;
960 inode = d_inode(dentry);
961 if (is_bad_inode(inode)) {
Al Viroa4555892014-10-21 20:11:25 -0400962 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
963 dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000964 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
David Howellsc435ee32017-11-02 15:27:49 +0000967 vnode = AFS_FS_I(inode);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* if the vnode ID has changed, then the dirent points to a
970 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700971 if (fid.vnode != vnode->fid.vnode) {
David Howells3b6492d2018-10-20 00:57:57 +0100972 _debug("%pd: dirent changed [%llu != %llu]",
Al Viroa4555892014-10-21 20:11:25 -0400973 dentry, fid.vnode,
David Howells08e0e7c2007-04-26 15:55:03 -0700974 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto not_found;
976 }
977
978 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700979 * been deleted and replaced, and the original vnode ID has
980 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700981 if (fid.unique != vnode->fid.unique) {
Al Viroa4555892014-10-21 20:11:25 -0400982 _debug("%pd: file deleted (uq %u -> %u I:%u)",
983 dentry, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500984 vnode->fid.unique,
David Howellsc435ee32017-11-02 15:27:49 +0000985 vnode->vfs_inode.i_generation);
986 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700987 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000988 write_sequnlock(&vnode->cb_lock);
David Howells260a9802007-04-26 15:59:35 -0700989 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
David Howells260a9802007-04-26 15:59:35 -0700991 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
David Howells260a9802007-04-26 15:59:35 -0700993 case -ENOENT:
994 /* the filename is unknown */
Al Viroa4555892014-10-21 20:11:25 -0400995 _debug("%pd: dirent not found", dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000996 if (d_really_is_positive(dentry))
David Howells260a9802007-04-26 15:59:35 -0700997 goto not_found;
998 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700999
David Howells260a9802007-04-26 15:59:35 -07001000 default:
Al Viroa4555892014-10-21 20:11:25 -04001001 _debug("failed to iterate dir %pd: %d",
1002 parent, ret);
David Howellsc435ee32017-11-02 15:27:49 +00001003 goto out_bad_parent;
David Howells08e0e7c2007-04-26 15:55:03 -07001004 }
1005
David Howellsec268152007-04-26 15:49:28 -07001006out_valid:
David Howellsa4ff7402018-04-06 14:17:24 +01001007 dentry->d_fsdata = (void *)dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -07001009 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 _leave(" = 1 [valid]");
1011 return 1;
1012
1013 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -07001014not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 spin_lock(&dentry->d_lock);
1016 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1017 spin_unlock(&dentry->d_lock);
1018
David Howellsc435ee32017-11-02 15:27:49 +00001019out_bad_parent:
Al Viroa4555892014-10-21 20:11:25 -04001020 _debug("dropping dentry %pd2", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dput(parent);
David Howellsc435ee32017-11-02 15:27:49 +00001022out_bad:
David Howells00d3b7a2007-04-26 15:57:07 -07001023 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 _leave(" = 0 [bad]");
1026 return 0;
David Howellsec268152007-04-26 15:49:28 -07001027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1031 * sleep)
1032 * - called from dput() when d_count is going to 0.
1033 * - return 1 to request dentry be unhashed, 0 otherwise
1034 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001035static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Al Viroa4555892014-10-21 20:11:25 -04001037 _enter("%pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1040 goto zap;
1041
David Howells2b0143b2015-03-17 22:25:59 +00001042 if (d_really_is_positive(dentry) &&
1043 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1044 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
wangleibec5eb62010-08-11 09:38:04 +01001045 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 _leave(" = 0 [keep]");
1048 return 0;
1049
David Howellsec268152007-04-26 15:49:28 -07001050zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 _leave(" = 1 [zap]");
1052 return 1;
David Howellsec268152007-04-26 15:49:28 -07001053}
David Howells260a9802007-04-26 15:59:35 -07001054
1055/*
1056 * handle dentry release
1057 */
David Howells66c7e1d2018-04-06 14:17:25 +01001058void afs_d_release(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001059{
Al Viroa4555892014-10-21 20:11:25 -04001060 _enter("%pd", dentry);
David Howells260a9802007-04-26 15:59:35 -07001061}
1062
1063/*
David Howellsd2ddc772017-11-02 15:27:50 +00001064 * Create a new inode for create/mkdir/symlink
1065 */
1066static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1067 struct dentry *new_dentry,
1068 struct afs_fid *newfid,
1069 struct afs_file_status *newstatus,
1070 struct afs_callback *newcb)
1071{
David Howells5a813272018-04-06 14:17:26 +01001072 struct afs_vnode *vnode;
David Howellsd2ddc772017-11-02 15:27:50 +00001073 struct inode *inode;
1074
1075 if (fc->ac.error < 0)
1076 return;
1077
David Howellsbc1527d2017-11-20 23:04:08 +00001078 d_drop(new_dentry);
1079
David Howellsd2ddc772017-11-02 15:27:50 +00001080 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1081 newfid, newstatus, newcb, fc->cbi);
1082 if (IS_ERR(inode)) {
1083 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1084 * the new directory on the server.
1085 */
1086 fc->ac.error = PTR_ERR(inode);
1087 return;
1088 }
1089
David Howells5a813272018-04-06 14:17:26 +01001090 vnode = AFS_FS_I(inode);
1091 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells00671912018-10-20 00:57:57 +01001092 afs_vnode_commit_status(fc, vnode, 0);
David Howellsbc1527d2017-11-20 23:04:08 +00001093 d_add(new_dentry, inode);
David Howellsd2ddc772017-11-02 15:27:50 +00001094}
1095
1096/*
David Howells260a9802007-04-26 15:59:35 -07001097 * create a directory on an AFS filesystem
1098 */
Al Viro18bb1db2011-07-26 01:41:39 -04001099static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -07001100{
David Howellsd2ddc772017-11-02 15:27:50 +00001101 struct afs_file_status newstatus;
1102 struct afs_fs_cursor fc;
1103 struct afs_callback newcb;
1104 struct afs_vnode *dvnode = AFS_FS_I(dir);
1105 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001106 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001107 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001108 int ret;
1109
David Howellsd2ddc772017-11-02 15:27:50 +00001110 mode |= S_IFDIR;
David Howells260a9802007-04-26 15:59:35 -07001111
David Howells3b6492d2018-10-20 00:57:57 +01001112 _enter("{%llx:%llu},{%pd},%ho",
Al Viroa4555892014-10-21 20:11:25 -04001113 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001114
David Howells260a9802007-04-26 15:59:35 -07001115 key = afs_request_key(dvnode->volume->cell);
1116 if (IS_ERR(key)) {
1117 ret = PTR_ERR(key);
1118 goto error;
1119 }
1120
David Howellsd2ddc772017-11-02 15:27:50 +00001121 ret = -ERESTARTSYS;
1122 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1123 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001124 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001125 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001126 &newfid, &newstatus, &newcb);
1127 }
David Howells260a9802007-04-26 15:59:35 -07001128
David Howellsd2ddc772017-11-02 15:27:50 +00001129 afs_check_for_remote_deletion(&fc, fc.vnode);
1130 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1131 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1132 ret = afs_end_vnode_operation(&fc);
1133 if (ret < 0)
1134 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001135 } else {
1136 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001137 }
1138
David Howells63a46812018-04-06 14:17:25 +01001139 if (ret == 0 &&
1140 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1141 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1142 afs_edit_dir_for_create);
1143
David Howells260a9802007-04-26 15:59:35 -07001144 key_put(key);
1145 _leave(" = 0");
1146 return 0;
1147
David Howellsd2ddc772017-11-02 15:27:50 +00001148error_key:
David Howells260a9802007-04-26 15:59:35 -07001149 key_put(key);
1150error:
1151 d_drop(dentry);
1152 _leave(" = %d", ret);
1153 return ret;
1154}
1155
1156/*
David Howellsd2ddc772017-11-02 15:27:50 +00001157 * Remove a subdir from a directory.
David Howells260a9802007-04-26 15:59:35 -07001158 */
David Howellsd2ddc772017-11-02 15:27:50 +00001159static void afs_dir_remove_subdir(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001160{
David Howells2b0143b2015-03-17 22:25:59 +00001161 if (d_really_is_positive(dentry)) {
David Howellsd2ddc772017-11-02 15:27:50 +00001162 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1163
David Howells260a9802007-04-26 15:59:35 -07001164 clear_nlink(&vnode->vfs_inode);
1165 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +00001166 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
David Howells63a46812018-04-06 14:17:25 +01001167 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
David Howells260a9802007-04-26 15:59:35 -07001168 }
David Howells260a9802007-04-26 15:59:35 -07001169}
1170
1171/*
David Howellsd2ddc772017-11-02 15:27:50 +00001172 * remove a directory from an AFS filesystem
David Howells260a9802007-04-26 15:59:35 -07001173 */
David Howellsd2ddc772017-11-02 15:27:50 +00001174static int afs_rmdir(struct inode *dir, struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -07001175{
David Howellsd2ddc772017-11-02 15:27:50 +00001176 struct afs_fs_cursor fc;
David Howellsf58db832018-10-20 00:57:58 +01001177 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
David Howells260a9802007-04-26 15:59:35 -07001178 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001179 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001180 int ret;
1181
David Howells3b6492d2018-10-20 00:57:57 +01001182 _enter("{%llx:%llu},{%pd}",
Al Viroa4555892014-10-21 20:11:25 -04001183 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -07001184
David Howells260a9802007-04-26 15:59:35 -07001185 key = afs_request_key(dvnode->volume->cell);
1186 if (IS_ERR(key)) {
1187 ret = PTR_ERR(key);
1188 goto error;
1189 }
1190
David Howellsf58db832018-10-20 00:57:58 +01001191 /* Try to make sure we have a callback promise on the victim. */
1192 if (d_really_is_positive(dentry)) {
1193 vnode = AFS_FS_I(d_inode(dentry));
1194 ret = afs_validate(vnode, key);
1195 if (ret < 0)
1196 goto error_key;
1197 }
1198
David Howellsd2ddc772017-11-02 15:27:50 +00001199 ret = -ERESTARTSYS;
1200 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1201 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001202 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells30062bd2018-10-20 00:57:58 +01001203 afs_fs_remove(&fc, vnode, dentry->d_name.name, true,
David Howells63a46812018-04-06 14:17:25 +01001204 data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001205 }
David Howells260a9802007-04-26 15:59:35 -07001206
David Howellsd2ddc772017-11-02 15:27:50 +00001207 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1208 ret = afs_end_vnode_operation(&fc);
David Howells63a46812018-04-06 14:17:25 +01001209 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +00001210 afs_dir_remove_subdir(dentry);
David Howells63a46812018-04-06 14:17:25 +01001211 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1212 afs_edit_dir_remove(dvnode, &dentry->d_name,
1213 afs_edit_dir_for_rmdir);
1214 }
David Howells260a9802007-04-26 15:59:35 -07001215 }
1216
David Howellsf58db832018-10-20 00:57:58 +01001217error_key:
David Howells260a9802007-04-26 15:59:35 -07001218 key_put(key);
David Howellsd2ddc772017-11-02 15:27:50 +00001219error:
1220 return ret;
1221}
David Howells260a9802007-04-26 15:59:35 -07001222
David Howellsd2ddc772017-11-02 15:27:50 +00001223/*
1224 * Remove a link to a file or symlink from a directory.
1225 *
1226 * If the file was not deleted due to excess hard links, the fileserver will
1227 * break the callback promise on the file - if it had one - before it returns
1228 * to us, and if it was deleted, it won't
1229 *
1230 * However, if we didn't have a callback promise outstanding, or it was
1231 * outstanding on a different server, then it won't break it either...
1232 */
David Howells440fbc32018-01-02 10:02:19 +00001233static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1234 unsigned long d_version_before,
1235 unsigned long d_version_after)
David Howellsd2ddc772017-11-02 15:27:50 +00001236{
David Howells440fbc32018-01-02 10:02:19 +00001237 bool dir_valid;
David Howellsd2ddc772017-11-02 15:27:50 +00001238 int ret = 0;
1239
David Howells440fbc32018-01-02 10:02:19 +00001240 /* There were no intervening changes on the server if the version
1241 * number we got back was incremented by exactly 1.
1242 */
1243 dir_valid = (d_version_after == d_version_before + 1);
1244
David Howellsd2ddc772017-11-02 15:27:50 +00001245 if (d_really_is_positive(dentry)) {
1246 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1247
David Howells30062bd2018-10-20 00:57:58 +01001248 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1249 /* Already done */
1250 } else if (dir_valid) {
David Howells440fbc32018-01-02 10:02:19 +00001251 drop_nlink(&vnode->vfs_inode);
1252 if (vnode->vfs_inode.i_nlink == 0) {
1253 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1254 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1255 }
David Howellsd2ddc772017-11-02 15:27:50 +00001256 ret = 0;
David Howells440fbc32018-01-02 10:02:19 +00001257 } else {
1258 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1259
1260 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1261 kdebug("AFS_VNODE_DELETED");
1262
1263 ret = afs_validate(vnode, key);
1264 if (ret == -ESTALE)
1265 ret = 0;
1266 }
David Howellsd2ddc772017-11-02 15:27:50 +00001267 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1268 }
1269
1270 return ret;
1271}
1272
1273/*
1274 * Remove a file or symlink from an AFS filesystem.
1275 */
1276static int afs_unlink(struct inode *dir, struct dentry *dentry)
1277{
1278 struct afs_fs_cursor fc;
David Howells30062bd2018-10-20 00:57:58 +01001279 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
David Howellsd2ddc772017-11-02 15:27:50 +00001280 struct key *key;
David Howells440fbc32018-01-02 10:02:19 +00001281 unsigned long d_version = (unsigned long)dentry->d_fsdata;
David Howells63a46812018-04-06 14:17:25 +01001282 u64 data_version = dvnode->status.data_version;
David Howellsd2ddc772017-11-02 15:27:50 +00001283 int ret;
1284
David Howells3b6492d2018-10-20 00:57:57 +01001285 _enter("{%llx:%llu},{%pd}",
David Howellsd2ddc772017-11-02 15:27:50 +00001286 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1287
1288 if (dentry->d_name.len >= AFSNAMEMAX)
1289 return -ENAMETOOLONG;
1290
1291 key = afs_request_key(dvnode->volume->cell);
1292 if (IS_ERR(key)) {
1293 ret = PTR_ERR(key);
1294 goto error;
1295 }
1296
1297 /* Try to make sure we have a callback promise on the victim. */
1298 if (d_really_is_positive(dentry)) {
1299 vnode = AFS_FS_I(d_inode(dentry));
1300 ret = afs_validate(vnode, key);
1301 if (ret < 0)
1302 goto error_key;
1303 }
1304
1305 ret = -ERESTARTSYS;
1306 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1307 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001308 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells30062bd2018-10-20 00:57:58 +01001309
1310 if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1311 !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1312 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1313 data_version);
1314 if (fc.ac.error != -ECONNABORTED ||
1315 fc.ac.abort_code != RXGEN_OPCODE)
1316 continue;
1317 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1318 }
1319
1320 afs_fs_remove(&fc, vnode, dentry->d_name.name, false,
David Howells63a46812018-04-06 14:17:25 +01001321 data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001322 }
1323
1324 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1325 ret = afs_end_vnode_operation(&fc);
1326 if (ret == 0)
David Howells440fbc32018-01-02 10:02:19 +00001327 ret = afs_dir_remove_link(
1328 dentry, key, d_version,
1329 (unsigned long)dvnode->status.data_version);
David Howells63a46812018-04-06 14:17:25 +01001330 if (ret == 0 &&
1331 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1332 afs_edit_dir_remove(dvnode, &dentry->d_name,
1333 afs_edit_dir_for_unlink);
David Howellsd2ddc772017-11-02 15:27:50 +00001334 }
1335
1336error_key:
David Howells260a9802007-04-26 15:59:35 -07001337 key_put(key);
1338error:
1339 _leave(" = %d", ret);
1340 return ret;
1341}
1342
1343/*
1344 * create a regular file on an AFS filesystem
1345 */
Al Viro4acdaf22011-07-26 01:42:34 -04001346static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04001347 bool excl)
David Howells260a9802007-04-26 15:59:35 -07001348{
David Howellsd2ddc772017-11-02 15:27:50 +00001349 struct afs_fs_cursor fc;
1350 struct afs_file_status newstatus;
1351 struct afs_callback newcb;
Colin Ian King43dd3882017-11-20 13:58:20 +00001352 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howellsd2ddc772017-11-02 15:27:50 +00001353 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001354 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001355 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001356 int ret;
1357
David Howellsd2ddc772017-11-02 15:27:50 +00001358 mode |= S_IFREG;
David Howells260a9802007-04-26 15:59:35 -07001359
David Howells3b6492d2018-10-20 00:57:57 +01001360 _enter("{%llx:%llu},{%pd},%ho,",
Al Viroa4555892014-10-21 20:11:25 -04001361 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -07001362
David Howellsd2ddc772017-11-02 15:27:50 +00001363 ret = -ENAMETOOLONG;
1364 if (dentry->d_name.len >= AFSNAMEMAX)
1365 goto error;
1366
David Howells260a9802007-04-26 15:59:35 -07001367 key = afs_request_key(dvnode->volume->cell);
1368 if (IS_ERR(key)) {
1369 ret = PTR_ERR(key);
1370 goto error;
1371 }
1372
David Howellsd2ddc772017-11-02 15:27:50 +00001373 ret = -ERESTARTSYS;
1374 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1375 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001376 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001377 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001378 &newfid, &newstatus, &newcb);
1379 }
David Howells260a9802007-04-26 15:59:35 -07001380
David Howellsd2ddc772017-11-02 15:27:50 +00001381 afs_check_for_remote_deletion(&fc, fc.vnode);
1382 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1383 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1384 ret = afs_end_vnode_operation(&fc);
1385 if (ret < 0)
1386 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001387 } else {
1388 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001389 }
1390
David Howells63a46812018-04-06 14:17:25 +01001391 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1392 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1393 afs_edit_dir_for_create);
1394
David Howells260a9802007-04-26 15:59:35 -07001395 key_put(key);
1396 _leave(" = 0");
1397 return 0;
1398
David Howellsd2ddc772017-11-02 15:27:50 +00001399error_key:
David Howells260a9802007-04-26 15:59:35 -07001400 key_put(key);
1401error:
1402 d_drop(dentry);
1403 _leave(" = %d", ret);
1404 return ret;
1405}
1406
1407/*
1408 * create a hard link between files in an AFS filesystem
1409 */
1410static int afs_link(struct dentry *from, struct inode *dir,
1411 struct dentry *dentry)
1412{
David Howellsd2ddc772017-11-02 15:27:50 +00001413 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001414 struct afs_vnode *dvnode, *vnode;
1415 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001416 u64 data_version;
David Howells260a9802007-04-26 15:59:35 -07001417 int ret;
1418
David Howells2b0143b2015-03-17 22:25:59 +00001419 vnode = AFS_FS_I(d_inode(from));
David Howells260a9802007-04-26 15:59:35 -07001420 dvnode = AFS_FS_I(dir);
David Howells63a46812018-04-06 14:17:25 +01001421 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001422
David Howells3b6492d2018-10-20 00:57:57 +01001423 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001424 vnode->fid.vid, vnode->fid.vnode,
1425 dvnode->fid.vid, dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001426 dentry);
David Howells260a9802007-04-26 15:59:35 -07001427
David Howellsd2ddc772017-11-02 15:27:50 +00001428 ret = -ENAMETOOLONG;
1429 if (dentry->d_name.len >= AFSNAMEMAX)
1430 goto error;
1431
David Howells260a9802007-04-26 15:59:35 -07001432 key = afs_request_key(dvnode->volume->cell);
1433 if (IS_ERR(key)) {
1434 ret = PTR_ERR(key);
1435 goto error;
1436 }
1437
David Howellsd2ddc772017-11-02 15:27:50 +00001438 ret = -ERESTARTSYS;
1439 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1440 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1441 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001442 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001443 }
David Howells260a9802007-04-26 15:59:35 -07001444
David Howellsd2ddc772017-11-02 15:27:50 +00001445 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001446 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1447 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
David Howells63a46812018-04-06 14:17:25 +01001448 afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001449 }
1450
1451 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1452 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1453 ihold(&vnode->vfs_inode);
1454 d_instantiate(dentry, &vnode->vfs_inode);
1455
1456 mutex_unlock(&vnode->io_lock);
1457 ret = afs_end_vnode_operation(&fc);
1458 if (ret < 0)
1459 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001460 } else {
1461 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001462 }
1463
David Howells63a46812018-04-06 14:17:25 +01001464 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1465 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1466 afs_edit_dir_for_link);
1467
David Howells260a9802007-04-26 15:59:35 -07001468 key_put(key);
1469 _leave(" = 0");
1470 return 0;
1471
David Howellsd2ddc772017-11-02 15:27:50 +00001472error_key:
David Howells260a9802007-04-26 15:59:35 -07001473 key_put(key);
1474error:
1475 d_drop(dentry);
1476 _leave(" = %d", ret);
1477 return ret;
1478}
1479
1480/*
1481 * create a symlink in an AFS filesystem
1482 */
1483static int afs_symlink(struct inode *dir, struct dentry *dentry,
1484 const char *content)
1485{
David Howellsd2ddc772017-11-02 15:27:50 +00001486 struct afs_fs_cursor fc;
1487 struct afs_file_status newstatus;
1488 struct afs_vnode *dvnode = AFS_FS_I(dir);
1489 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001490 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001491 u64 data_version = dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001492 int ret;
1493
David Howells3b6492d2018-10-20 00:57:57 +01001494 _enter("{%llx:%llu},{%pd},%s",
Al Viroa4555892014-10-21 20:11:25 -04001495 dvnode->fid.vid, dvnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -07001496 content);
1497
David Howellsd2ddc772017-11-02 15:27:50 +00001498 ret = -ENAMETOOLONG;
1499 if (dentry->d_name.len >= AFSNAMEMAX)
1500 goto error;
1501
David Howells260a9802007-04-26 15:59:35 -07001502 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001503 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001504 goto error;
1505
1506 key = afs_request_key(dvnode->volume->cell);
1507 if (IS_ERR(key)) {
1508 ret = PTR_ERR(key);
1509 goto error;
1510 }
1511
David Howellsd2ddc772017-11-02 15:27:50 +00001512 ret = -ERESTARTSYS;
1513 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1514 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001515 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
David Howells63a46812018-04-06 14:17:25 +01001516 afs_fs_symlink(&fc, dentry->d_name.name,
1517 content, data_version,
David Howellsd2ddc772017-11-02 15:27:50 +00001518 &newfid, &newstatus);
1519 }
David Howells260a9802007-04-26 15:59:35 -07001520
David Howellsd2ddc772017-11-02 15:27:50 +00001521 afs_check_for_remote_deletion(&fc, fc.vnode);
1522 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1523 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1524 ret = afs_end_vnode_operation(&fc);
1525 if (ret < 0)
1526 goto error_key;
David Howells4433b692017-11-20 22:41:00 +00001527 } else {
1528 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001529 }
1530
David Howells63a46812018-04-06 14:17:25 +01001531 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1532 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1533 afs_edit_dir_for_symlink);
1534
David Howells260a9802007-04-26 15:59:35 -07001535 key_put(key);
1536 _leave(" = 0");
1537 return 0;
1538
David Howellsd2ddc772017-11-02 15:27:50 +00001539error_key:
David Howells260a9802007-04-26 15:59:35 -07001540 key_put(key);
1541error:
1542 d_drop(dentry);
1543 _leave(" = %d", ret);
1544 return ret;
1545}
1546
1547/*
1548 * rename a file in an AFS filesystem and/or move it between directories
1549 */
1550static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001551 struct inode *new_dir, struct dentry *new_dentry,
1552 unsigned int flags)
David Howells260a9802007-04-26 15:59:35 -07001553{
David Howellsd2ddc772017-11-02 15:27:50 +00001554 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001555 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1556 struct key *key;
David Howells63a46812018-04-06 14:17:25 +01001557 u64 orig_data_version, new_data_version;
1558 bool new_negative = d_is_negative(new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001559 int ret;
1560
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001561 if (flags)
1562 return -EINVAL;
1563
David Howells2b0143b2015-03-17 22:25:59 +00001564 vnode = AFS_FS_I(d_inode(old_dentry));
David Howells260a9802007-04-26 15:59:35 -07001565 orig_dvnode = AFS_FS_I(old_dir);
1566 new_dvnode = AFS_FS_I(new_dir);
David Howells63a46812018-04-06 14:17:25 +01001567 orig_data_version = orig_dvnode->status.data_version;
1568 new_data_version = new_dvnode->status.data_version;
David Howells260a9802007-04-26 15:59:35 -07001569
David Howells3b6492d2018-10-20 00:57:57 +01001570 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001571 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1572 vnode->fid.vid, vnode->fid.vnode,
1573 new_dvnode->fid.vid, new_dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001574 new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001575
David Howells260a9802007-04-26 15:59:35 -07001576 key = afs_request_key(orig_dvnode->volume->cell);
1577 if (IS_ERR(key)) {
1578 ret = PTR_ERR(key);
1579 goto error;
1580 }
1581
David Howellsd2ddc772017-11-02 15:27:50 +00001582 ret = -ERESTARTSYS;
1583 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1584 if (orig_dvnode != new_dvnode) {
1585 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1586 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001587 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001588 }
1589 }
1590 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +01001591 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1592 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
David Howellsd2ddc772017-11-02 15:27:50 +00001593 afs_fs_rename(&fc, old_dentry->d_name.name,
David Howells63a46812018-04-06 14:17:25 +01001594 new_dvnode, new_dentry->d_name.name,
1595 orig_data_version, new_data_version);
David Howellsd2ddc772017-11-02 15:27:50 +00001596 }
1597
1598 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1599 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1600 if (orig_dvnode != new_dvnode)
1601 mutex_unlock(&new_dvnode->io_lock);
1602 ret = afs_end_vnode_operation(&fc);
1603 if (ret < 0)
1604 goto error_key;
1605 }
1606
David Howells63a46812018-04-06 14:17:25 +01001607 if (ret == 0) {
1608 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1609 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1610 afs_edit_dir_for_rename);
1611
1612 if (!new_negative &&
1613 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1614 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1615 afs_edit_dir_for_rename);
1616
1617 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1618 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1619 &vnode->fid, afs_edit_dir_for_rename);
1620 }
1621
David Howellsd2ddc772017-11-02 15:27:50 +00001622error_key:
David Howells260a9802007-04-26 15:59:35 -07001623 key_put(key);
1624error:
David Howells260a9802007-04-26 15:59:35 -07001625 _leave(" = %d", ret);
1626 return ret;
1627}
David Howellsf3ddee82018-04-06 14:17:25 +01001628
1629/*
1630 * Release a directory page and clean up its private state if it's not busy
1631 * - return true if the page can now be released, false if not
1632 */
1633static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1634{
1635 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1636
David Howells3b6492d2018-10-20 00:57:57 +01001637 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
David Howellsf3ddee82018-04-06 14:17:25 +01001638
1639 set_page_private(page, 0);
1640 ClearPagePrivate(page);
1641
1642 /* The directory will need reloading. */
1643 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1644 afs_stat_v(dvnode, n_relpg);
1645 return 1;
1646}
1647
1648/*
1649 * invalidate part or all of a page
1650 * - release a page and clean up its private data if offset is 0 (indicating
1651 * the entire page)
1652 */
1653static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1654 unsigned int length)
1655{
1656 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1657
1658 _enter("{%lu},%u,%u", page->index, offset, length);
1659
1660 BUG_ON(!PageLocked(page));
1661
1662 /* The directory will need reloading. */
1663 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1664 afs_stat_v(dvnode, n_inval);
1665
1666 /* we clean up only if the entire page is being invalidated */
1667 if (offset == 0 && length == PAGE_SIZE) {
1668 set_page_private(page, 0);
1669 ClearPagePrivate(page);
1670 }
1671}