blob: bb89876fbb8e8c365cf76c86134ff2be3c16b995 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* dir.c: AFS filesystem directory handling
2 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
Nick Piggin34286d62011-01-07 17:49:57 +110016#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/pagemap.h>
David Howells00d3b7a2007-04-26 15:57:07 -070018#include <linux/ctype.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
David Howells260a9802007-04-26 15:59:35 -070022static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040023 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int afs_dir_open(struct inode *inode, struct file *file);
Al Viro1bbae9f2013-05-22 16:31:14 -040025static int afs_readdir(struct file *file, struct dir_context *ctx);
Al Viro0b728e12012-06-10 16:03:43 -040026static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
Nick Pigginfe15ce42011-01-07 17:49:23 +110027static int afs_d_delete(const struct dentry *dentry);
David Howells260a9802007-04-26 15:59:35 -070028static void afs_d_release(struct dentry *dentry);
Miklos Szerediac7576f2014-10-30 17:37:34 +010029static int afs_lookup_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);
Al Viro4acdaf22011-07-26 01:42:34 -040031static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040032 bool excl);
Al Viro18bb1db2011-07-26 01:41:39 -040033static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
David Howells260a9802007-04-26 15:59:35 -070034static int afs_rmdir(struct inode *dir, struct dentry *dentry);
35static int afs_unlink(struct inode *dir, struct dentry *dentry);
36static int afs_link(struct dentry *from, struct inode *dir,
37 struct dentry *dentry);
38static int afs_symlink(struct inode *dir, struct dentry *dentry,
39 const char *content);
40static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +020041 struct inode *new_dir, struct dentry *new_dentry,
42 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080044const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .open = afs_dir_open,
David Howells00d3b7a2007-04-26 15:57:07 -070046 .release = afs_release,
Al Viro29884ef2016-05-10 14:27:44 -040047 .iterate_shared = afs_readdir,
David Howellse8d6c552007-07-15 23:40:12 -070048 .lock = afs_lock,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020049 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Arjan van de Ven754661f2007-02-12 00:55:38 -080052const struct inode_operations afs_dir_inode_operations = {
David Howells260a9802007-04-26 15:59:35 -070053 .create = afs_create,
54 .lookup = afs_lookup,
55 .link = afs_link,
56 .unlink = afs_unlink,
57 .symlink = afs_symlink,
58 .mkdir = afs_mkdir,
59 .rmdir = afs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020060 .rename = afs_rename,
David Howells00d3b7a2007-04-26 15:57:07 -070061 .permission = afs_permission,
David Howells416351f2007-05-09 02:33:45 -070062 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070063 .setattr = afs_setattr,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010064 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Al Virod61dcce2011-01-12 20:04:20 -050067const struct dentry_operations afs_fs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .d_revalidate = afs_d_revalidate,
69 .d_delete = afs_d_delete,
David Howells260a9802007-04-26 15:59:35 -070070 .d_release = afs_d_release,
David Howellsd18610b2011-01-14 19:04:05 +000071 .d_automount = afs_d_automount,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74#define AFS_DIR_HASHTBL_SIZE 128
75#define AFS_DIR_DIRENT_SIZE 32
76#define AFS_DIRENT_PER_BLOCK 64
77
78union afs_dirent {
79 struct {
80 uint8_t valid;
81 uint8_t unused[1];
82 __be16 hash_next;
83 __be32 vnode;
84 __be32 unique;
85 uint8_t name[16];
86 uint8_t overflow[4]; /* if any char of the name (inc
87 * NUL) reaches here, consume
88 * the next dirent too */
89 } u;
90 uint8_t extended_name[32];
91};
92
93/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
94struct afs_dir_pagehdr {
95 __be16 npages;
96 __be16 magic;
97#define AFS_DIR_MAGIC htons(1234)
98 uint8_t nentries;
99 uint8_t bitmap[8];
100 uint8_t pad[19];
101};
102
103/* directory block layout */
104union afs_dir_block {
105
106 struct afs_dir_pagehdr pagehdr;
107
108 struct {
109 struct afs_dir_pagehdr pagehdr;
110 uint8_t alloc_ctrs[128];
111 /* dir hash table */
112 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
113 } hdr;
114
115 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
116};
117
118/* layout on a linux VM page */
119struct afs_dir_page {
120 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
121};
122
David Howells260a9802007-04-26 15:59:35 -0700123struct afs_lookup_cookie {
Al Viro1bbae9f2013-05-22 16:31:14 -0400124 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct afs_fid fid;
Al Viro1bbae9f2013-05-22 16:31:14 -0400126 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int found;
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * check that a directory page is valid
132 */
David Howellsdab17c12017-11-02 15:27:52 +0000133bool afs_dir_check_page(struct inode *dir, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct afs_dir_page *dbuf;
David Howellsdab17c12017-11-02 15:27:52 +0000136 struct afs_vnode *vnode = AFS_FS_I(dir);
137 loff_t latter, i_size, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int tmp, qty;
139
140#if 0
141 /* check the page count */
142 qty = desc.size / sizeof(dbuf->blocks[0]);
143 if (qty == 0)
144 goto error;
145
David Howells08e0e7c2007-04-26 15:55:03 -0700146 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700148 __func__, dir->i_ino, qty,
David Howells08e0e7c2007-04-26 15:55:03 -0700149 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto error;
151 }
152#endif
153
David Howellsdab17c12017-11-02 15:27:52 +0000154 /* Determine how many magic numbers there should be in this page, but
155 * we must take care because the directory may change size under us.
156 */
157 off = page_offset(page);
158 i_size = i_size_read(dir);
159 if (i_size <= off)
160 goto checked;
161
162 latter = i_size - off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (latter >= PAGE_SIZE)
164 qty = PAGE_SIZE;
165 else
166 qty = latter;
167 qty /= sizeof(union afs_dir_block);
168
169 /* check them */
170 dbuf = page_address(page);
171 for (tmp = 0; tmp < qty; tmp++) {
172 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
David Howellsdab17c12017-11-02 15:27:52 +0000173 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
Harvey Harrison530b6412008-04-30 00:55:09 -0700174 __func__, dir->i_ino, tmp, qty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ntohs(dbuf->blocks[tmp].pagehdr.magic));
David Howellsdab17c12017-11-02 15:27:52 +0000176 trace_afs_dir_check_failed(vnode, off, i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto error;
178 }
179 }
180
David Howellsdab17c12017-11-02 15:27:52 +0000181checked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400183 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Howellsec268152007-04-26 15:49:28 -0700185error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400187 return false;
David Howellsec268152007-04-26 15:49:28 -0700188}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * discard a page cached in the pagecache
192 */
193static inline void afs_dir_put_page(struct page *page)
194{
195 kunmap(page);
David Howellsdab17c12017-11-02 15:27:52 +0000196 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300197 put_page(page);
David Howellsec268152007-04-26 15:49:28 -0700198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * get a page into the pagecache
202 */
David Howells00d3b7a2007-04-26 15:57:07 -0700203static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
204 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 _enter("{%lu},%lu", dir->i_ino, index);
208
Al Virof6d335c2010-05-21 15:27:09 +0100209 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!IS_ERR(page)) {
David Howellsdab17c12017-11-02 15:27:52 +0000211 lock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400213 if (unlikely(!PageChecked(page))) {
David Howellsdab17c12017-11-02 15:27:52 +0000214 if (PageError(page))
Al Virobe5b82d2016-04-22 15:06:44 -0400215 goto fail;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 return page;
219
David Howellsec268152007-04-26 15:49:28 -0700220fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700222 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
227 * open an AFS directory file
228 */
229static int afs_dir_open(struct inode *inode, struct file *file)
230{
231 _enter("{%lu}", inode->i_ino);
232
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700233 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
234 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
David Howells08e0e7c2007-04-26 15:55:03 -0700236 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -ENOENT;
238
David Howells00d3b7a2007-04-26 15:57:07 -0700239 return afs_open(inode, file);
David Howellsec268152007-04-26 15:49:28 -0700240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*
243 * deal with one block in an AFS directory
244 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400245static int afs_dir_iterate_block(struct dir_context *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 union afs_dir_block *block,
Al Viro1bbae9f2013-05-22 16:31:14 -0400247 unsigned blkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 union afs_dirent *dire;
250 unsigned offset, next, curr;
251 size_t nlen;
Al Viro1bbae9f2013-05-22 16:31:14 -0400252 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Al Viro1bbae9f2013-05-22 16:31:14 -0400254 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Al Viro1bbae9f2013-05-22 16:31:14 -0400256 curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* walk through the block, an entry at a time */
259 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
260 offset < AFS_DIRENT_PER_BLOCK;
261 offset = next
262 ) {
263 next = offset + 1;
264
265 /* skip entries marked unused in the bitmap */
266 if (!(block->pagehdr.bitmap[offset / 8] &
267 (1 << (offset % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800268 _debug("ENT[%zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 blkoff / sizeof(union afs_dir_block), offset);
270 if (offset >= curr)
Al Viro1bbae9f2013-05-22 16:31:14 -0400271 ctx->pos = blkoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 next * sizeof(union afs_dirent);
273 continue;
274 }
275
276 /* got a valid entry */
277 dire = &block->dirents[offset];
278 nlen = strnlen(dire->u.name,
279 sizeof(*block) -
280 offset * sizeof(union afs_dirent));
281
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800282 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 blkoff / sizeof(union afs_dir_block), offset,
284 (offset < curr ? "skip" : "fill"),
285 nlen, dire->u.name);
286
287 /* work out where the next possible entry is */
288 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
289 if (next >= AFS_DIRENT_PER_BLOCK) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800290 _debug("ENT[%zu.%u]:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 " %u travelled beyond end dir block"
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800292 " (len %u/%zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 blkoff / sizeof(union afs_dir_block),
294 offset, next, tmp, nlen);
295 return -EIO;
296 }
297 if (!(block->pagehdr.bitmap[next / 8] &
298 (1 << (next % 8)))) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800299 _debug("ENT[%zu.%u]:"
300 " %u unmarked extension (len %u/%zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 blkoff / sizeof(union afs_dir_block),
302 offset, next, tmp, nlen);
303 return -EIO;
304 }
305
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800306 _debug("ENT[%zu.%u]: ext %u/%zu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 blkoff / sizeof(union afs_dir_block),
308 next, tmp, nlen);
309 next++;
310 }
311
312 /* skip if starts before the current position */
313 if (offset < curr)
314 continue;
315
316 /* found the next entry */
Al Viro1bbae9f2013-05-22 16:31:14 -0400317 if (!dir_emit(ctx, dire->u.name, nlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ntohl(dire->u.vnode),
Al Viro1bbae9f2013-05-22 16:31:14 -0400319 ctx->actor == afs_lookup_filldir ?
320 ntohl(dire->u.unique) : DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 _leave(" = 0 [full]");
322 return 0;
323 }
324
Al Viro1bbae9f2013-05-22 16:31:14 -0400325 ctx->pos = blkoff + next * sizeof(union afs_dirent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 _leave(" = 1 [more]");
329 return 1;
David Howellsec268152007-04-26 15:49:28 -0700330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
David Howells08e0e7c2007-04-26 15:55:03 -0700333 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400335static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
336 struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
David Howells08e0e7c2007-04-26 15:55:03 -0700338 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct afs_dir_page *dbuf;
340 struct page *page;
341 unsigned blkoff, limit;
342 int ret;
343
Al Viro1bbae9f2013-05-22 16:31:14 -0400344 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
David Howells08e0e7c2007-04-26 15:55:03 -0700346 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 _leave(" = -ESTALE");
348 return -ESTALE;
349 }
350
351 /* round the file position up to the next entry boundary */
Al Viro1bbae9f2013-05-22 16:31:14 -0400352 ctx->pos += sizeof(union afs_dirent) - 1;
353 ctx->pos &= ~(sizeof(union afs_dirent) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* walk through the blocks in sequence */
356 ret = 0;
Al Viro1bbae9f2013-05-22 16:31:14 -0400357 while (ctx->pos < dir->i_size) {
358 blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* fetch the appropriate page from the directory */
David Howells00d3b7a2007-04-26 15:57:07 -0700361 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (IS_ERR(page)) {
363 ret = PTR_ERR(page);
364 break;
365 }
366
367 limit = blkoff & ~(PAGE_SIZE - 1);
368
369 dbuf = page_address(page);
370
371 /* deal with the individual blocks stashed on this page */
372 do {
373 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
374 sizeof(union afs_dir_block)];
Al Viro1bbae9f2013-05-22 16:31:14 -0400375 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (ret != 1) {
377 afs_dir_put_page(page);
378 goto out;
379 }
380
381 blkoff += sizeof(union afs_dir_block);
382
Al Viro1bbae9f2013-05-22 16:31:14 -0400383 } while (ctx->pos < dir->i_size && blkoff < limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 afs_dir_put_page(page);
386 ret = 0;
387 }
388
David Howellsec268152007-04-26 15:49:28 -0700389out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 _leave(" = %d", ret);
391 return ret;
David Howellsec268152007-04-26 15:49:28 -0700392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
395 * read an AFS directory
396 */
Al Viro1bbae9f2013-05-22 16:31:14 -0400397static int afs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
David Howells215804a2017-11-02 15:27:52 +0000399 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
David Howellsec268152007-04-26 15:49:28 -0700400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
403 * search the directory for a name
404 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
405 * uniquifier through dtype
406 */
Miklos Szerediac7576f2014-10-30 17:37:34 +0100407static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
408 int nlen, loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100410 struct afs_lookup_cookie *cookie =
411 container_of(ctx, struct afs_lookup_cookie, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Al Viro1bbae9f2013-05-22 16:31:14 -0400413 _enter("{%s,%u},%s,%u,,%llu,%u",
414 cookie->name.name, cookie->name.len, name, nlen,
David S. Millerba3e0e12007-04-26 16:06:22 -0700415 (unsigned long long) ino, dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells08e0e7c2007-04-26 15:55:03 -0700417 /* insanity checks first */
418 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
419 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
420
Al Viro1bbae9f2013-05-22 16:31:14 -0400421 if (cookie->name.len != nlen ||
422 memcmp(cookie->name.name, name, nlen) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 _leave(" = 0 [no]");
424 return 0;
425 }
426
427 cookie->fid.vnode = ino;
428 cookie->fid.unique = dtype;
429 cookie->found = 1;
430
431 _leave(" = -1 [found]");
432 return -1;
David Howellsec268152007-04-26 15:49:28 -0700433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
David Howells08e0e7c2007-04-26 15:55:03 -0700436 * do a lookup in a directory
David Howells260a9802007-04-26 15:59:35 -0700437 * - just returns the FID the dentry name maps to if found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
David Howells08e0e7c2007-04-26 15:55:03 -0700439static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
David Howells00d3b7a2007-04-26 15:57:07 -0700440 struct afs_fid *fid, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Al Viro1bbae9f2013-05-22 16:31:14 -0400442 struct afs_super_info *as = dir->i_sb->s_fs_info;
443 struct afs_lookup_cookie cookie = {
444 .ctx.actor = afs_lookup_filldir,
445 .name = dentry->d_name,
446 .fid.vid = as->volume->vid
447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int ret;
449
Al Viroa4555892014-10-21 20:11:25 -0400450 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* search the directory */
Al Viro1bbae9f2013-05-22 16:31:14 -0400453 ret = afs_dir_iterate(dir, &cookie.ctx, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700455 _leave(" = %d [iter]", ret);
456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 ret = -ENOENT;
460 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700461 _leave(" = -ENOENT [not found]");
462 return -ENOENT;
463 }
464
465 *fid = cookie.fid;
466 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
467 return 0;
468}
469
470/*
wangleibec5eb62010-08-11 09:38:04 +0100471 * Try to auto mount the mountpoint with pseudo directory, if the autocell
472 * operation is setted.
473 */
474static struct inode *afs_try_auto_mntpt(
475 int ret, struct dentry *dentry, struct inode *dir, struct key *key,
476 struct afs_fid *fid)
477{
478 const char *devname = dentry->d_name.name;
479 struct afs_vnode *vnode = AFS_FS_I(dir);
480 struct inode *inode;
481
Al Viroa4555892014-10-21 20:11:25 -0400482 _enter("%d, %p{%pd}, {%x:%u}, %p",
483 ret, dentry, dentry, vnode->fid.vid, vnode->fid.vnode, key);
wangleibec5eb62010-08-11 09:38:04 +0100484
485 if (ret != -ENOENT ||
486 !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
487 goto out;
488
489 inode = afs_iget_autocell(dir, devname, strlen(devname), key);
490 if (IS_ERR(inode)) {
491 ret = PTR_ERR(inode);
492 goto out;
493 }
494
495 *fid = AFS_FS_I(inode)->fid;
496 _leave("= %p", inode);
497 return inode;
498
499out:
500 _leave("= %d", ret);
501 return ERR_PTR(ret);
502}
503
504/*
David Howells08e0e7c2007-04-26 15:55:03 -0700505 * look up an entry in a directory
506 */
David Howells260a9802007-04-26 15:59:35 -0700507static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400508 unsigned int flags)
David Howells08e0e7c2007-04-26 15:55:03 -0700509{
510 struct afs_vnode *vnode;
511 struct afs_fid fid;
512 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700513 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700514 int ret;
515
David Howells260a9802007-04-26 15:59:35 -0700516 vnode = AFS_FS_I(dir);
517
Al Viroa4555892014-10-21 20:11:25 -0400518 _enter("{%x:%u},%p{%pd},",
519 vnode->fid.vid, vnode->fid.vnode, dentry, dentry);
David Howells260a9802007-04-26 15:59:35 -0700520
David Howells2b0143b2015-03-17 22:25:59 +0000521 ASSERTCMP(d_inode(dentry), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700522
David Howells45222b92007-05-10 22:22:20 -0700523 if (dentry->d_name.len >= AFSNAMEMAX) {
David Howells08e0e7c2007-04-26 15:55:03 -0700524 _leave(" = -ENAMETOOLONG");
525 return ERR_PTR(-ENAMETOOLONG);
526 }
527
David Howells08e0e7c2007-04-26 15:55:03 -0700528 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
529 _leave(" = -ESTALE");
530 return ERR_PTR(-ESTALE);
531 }
532
David Howells00d3b7a2007-04-26 15:57:07 -0700533 key = afs_request_key(vnode->volume->cell);
534 if (IS_ERR(key)) {
535 _leave(" = %ld [key]", PTR_ERR(key));
David Howellse231c2e2008-02-07 00:15:26 -0800536 return ERR_CAST(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700537 }
538
David Howells260a9802007-04-26 15:59:35 -0700539 ret = afs_validate(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700540 if (ret < 0) {
David Howells00d3b7a2007-04-26 15:57:07 -0700541 key_put(key);
David Howells260a9802007-04-26 15:59:35 -0700542 _leave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return ERR_PTR(ret);
544 }
545
David Howells260a9802007-04-26 15:59:35 -0700546 ret = afs_do_lookup(dir, dentry, &fid, key);
547 if (ret < 0) {
wangleibec5eb62010-08-11 09:38:04 +0100548 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
549 if (!IS_ERR(inode)) {
550 key_put(key);
551 goto success;
552 }
553
554 ret = PTR_ERR(inode);
David Howells260a9802007-04-26 15:59:35 -0700555 key_put(key);
556 if (ret == -ENOENT) {
557 d_add(dentry, NULL);
558 _leave(" = NULL [negative]");
559 return NULL;
560 }
561 _leave(" = %d [do]", ret);
562 return ERR_PTR(ret);
563 }
564 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* instantiate the dentry */
David Howellsd2ddc772017-11-02 15:27:50 +0000567 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL, NULL);
David Howells00d3b7a2007-04-26 15:57:07 -0700568 key_put(key);
David Howells08e0e7c2007-04-26 15:55:03 -0700569 if (IS_ERR(inode)) {
570 _leave(" = %ld", PTR_ERR(inode));
David Howellse231c2e2008-02-07 00:15:26 -0800571 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
wangleibec5eb62010-08-11 09:38:04 +0100574success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 d_add(dentry, inode);
David Howellsd6e43f72011-06-14 00:45:44 +0100576 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
David Howells08e0e7c2007-04-26 15:55:03 -0700577 fid.vnode,
578 fid.unique,
David Howells2b0143b2015-03-17 22:25:59 +0000579 d_inode(dentry)->i_ino,
580 d_inode(dentry)->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * check that a dentry lookup hit has found a valid entry
587 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
588 * inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Al Viro0b728e12012-06-10 16:03:43 -0400590static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
David Howells260a9802007-04-26 15:59:35 -0700592 struct afs_vnode *vnode, *dir;
Artem Bityutskiydd0d9a42009-07-09 10:44:30 +0100593 struct afs_fid uninitialized_var(fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct dentry *parent;
David Howellsc435ee32017-11-02 15:27:49 +0000595 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700596 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700597 void *dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int ret;
599
Al Viro0b728e12012-06-10 16:03:43 -0400600 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100601 return -ECHILD;
602
David Howellsc435ee32017-11-02 15:27:49 +0000603 if (d_really_is_positive(dentry)) {
604 vnode = AFS_FS_I(d_inode(dentry));
Al Viroa4555892014-10-21 20:11:25 -0400605 _enter("{v={%x:%u} n=%pd fl=%lx},",
606 vnode->fid.vid, vnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -0700607 vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000608 } else {
Al Viroa4555892014-10-21 20:11:25 -0400609 _enter("{neg n=%pd}", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
David Howells260a9802007-04-26 15:59:35 -0700612 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700613 if (IS_ERR(key))
614 key = NULL;
615
David Howellsc435ee32017-11-02 15:27:49 +0000616 if (d_really_is_positive(dentry)) {
617 inode = d_inode(dentry);
618 if (inode) {
619 vnode = AFS_FS_I(inode);
620 afs_validate(vnode, key);
621 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
622 goto out_bad;
623 }
624 }
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700627 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000628 dir = AFS_FS_I(d_inode(parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
David Howells260a9802007-04-26 15:59:35 -0700630 /* validate the parent directory */
David Howellsc435ee32017-11-02 15:27:49 +0000631 afs_validate(dir, key);
David Howells260a9802007-04-26 15:59:35 -0700632
633 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
Al Viroa4555892014-10-21 20:11:25 -0400634 _debug("%pd: parent dir deleted", dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000635 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
David Howells260a9802007-04-26 15:59:35 -0700638 dir_version = (void *) (unsigned long) dir->status.data_version;
639 if (dentry->d_fsdata == dir_version)
640 goto out_valid; /* the dir contents are unchanged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
David Howells260a9802007-04-26 15:59:35 -0700642 _debug("dir modified");
643
644 /* search the directory for this vnode */
645 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
646 switch (ret) {
647 case 0:
648 /* the filename maps to something */
David Howells2b0143b2015-03-17 22:25:59 +0000649 if (d_really_is_negative(dentry))
David Howellsc435ee32017-11-02 15:27:49 +0000650 goto out_bad_parent;
651 inode = d_inode(dentry);
652 if (is_bad_inode(inode)) {
Al Viroa4555892014-10-21 20:11:25 -0400653 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
654 dentry);
David Howellsc435ee32017-11-02 15:27:49 +0000655 goto out_bad_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
David Howellsc435ee32017-11-02 15:27:49 +0000658 vnode = AFS_FS_I(inode);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* if the vnode ID has changed, then the dirent points to a
661 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700662 if (fid.vnode != vnode->fid.vnode) {
Al Viroa4555892014-10-21 20:11:25 -0400663 _debug("%pd: dirent changed [%u != %u]",
664 dentry, fid.vnode,
David Howells08e0e7c2007-04-26 15:55:03 -0700665 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto not_found;
667 }
668
669 /* if the vnode ID uniqifier has changed, then the file has
David Howells260a9802007-04-26 15:59:35 -0700670 * been deleted and replaced, and the original vnode ID has
671 * been reused */
David Howells08e0e7c2007-04-26 15:55:03 -0700672 if (fid.unique != vnode->fid.unique) {
Al Viroa4555892014-10-21 20:11:25 -0400673 _debug("%pd: file deleted (uq %u -> %u I:%u)",
674 dentry, fid.unique,
Jean Noel Cordenner7a224222008-01-28 23:58:27 -0500675 vnode->fid.unique,
David Howellsc435ee32017-11-02 15:27:49 +0000676 vnode->vfs_inode.i_generation);
677 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700678 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000679 write_sequnlock(&vnode->cb_lock);
David Howells260a9802007-04-26 15:59:35 -0700680 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
David Howells260a9802007-04-26 15:59:35 -0700682 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Howells260a9802007-04-26 15:59:35 -0700684 case -ENOENT:
685 /* the filename is unknown */
Al Viroa4555892014-10-21 20:11:25 -0400686 _debug("%pd: dirent not found", dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000687 if (d_really_is_positive(dentry))
David Howells260a9802007-04-26 15:59:35 -0700688 goto not_found;
689 goto out_valid;
David Howells08e0e7c2007-04-26 15:55:03 -0700690
David Howells260a9802007-04-26 15:59:35 -0700691 default:
Al Viroa4555892014-10-21 20:11:25 -0400692 _debug("failed to iterate dir %pd: %d",
693 parent, ret);
David Howellsc435ee32017-11-02 15:27:49 +0000694 goto out_bad_parent;
David Howells08e0e7c2007-04-26 15:55:03 -0700695 }
696
David Howellsec268152007-04-26 15:49:28 -0700697out_valid:
David Howells260a9802007-04-26 15:59:35 -0700698 dentry->d_fsdata = dir_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dput(parent);
David Howells00d3b7a2007-04-26 15:57:07 -0700700 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 _leave(" = 1 [valid]");
702 return 1;
703
704 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700705not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 spin_lock(&dentry->d_lock);
707 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
708 spin_unlock(&dentry->d_lock);
709
David Howellsc435ee32017-11-02 15:27:49 +0000710out_bad_parent:
Al Viroa4555892014-10-21 20:11:25 -0400711 _debug("dropping dentry %pd2", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 dput(parent);
David Howellsc435ee32017-11-02 15:27:49 +0000713out_bad:
David Howells00d3b7a2007-04-26 15:57:07 -0700714 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 _leave(" = 0 [bad]");
717 return 0;
David Howellsec268152007-04-26 15:49:28 -0700718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
722 * sleep)
723 * - called from dput() when d_count is going to 0.
724 * - return 1 to request dentry be unhashed, 0 otherwise
725 */
Nick Pigginfe15ce42011-01-07 17:49:23 +1100726static int afs_d_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Al Viroa4555892014-10-21 20:11:25 -0400728 _enter("%pd", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
731 goto zap;
732
David Howells2b0143b2015-03-17 22:25:59 +0000733 if (d_really_is_positive(dentry) &&
734 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
735 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
wangleibec5eb62010-08-11 09:38:04 +0100736 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 _leave(" = 0 [keep]");
739 return 0;
740
David Howellsec268152007-04-26 15:49:28 -0700741zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 _leave(" = 1 [zap]");
743 return 1;
David Howellsec268152007-04-26 15:49:28 -0700744}
David Howells260a9802007-04-26 15:59:35 -0700745
746/*
747 * handle dentry release
748 */
749static void afs_d_release(struct dentry *dentry)
750{
Al Viroa4555892014-10-21 20:11:25 -0400751 _enter("%pd", dentry);
David Howells260a9802007-04-26 15:59:35 -0700752}
753
754/*
David Howellsd2ddc772017-11-02 15:27:50 +0000755 * Create a new inode for create/mkdir/symlink
756 */
757static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
758 struct dentry *new_dentry,
759 struct afs_fid *newfid,
760 struct afs_file_status *newstatus,
761 struct afs_callback *newcb)
762{
763 struct inode *inode;
764
765 if (fc->ac.error < 0)
766 return;
767
David Howellsbc1527d2017-11-20 23:04:08 +0000768 d_drop(new_dentry);
769
David Howellsd2ddc772017-11-02 15:27:50 +0000770 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
771 newfid, newstatus, newcb, fc->cbi);
772 if (IS_ERR(inode)) {
773 /* ENOMEM or EINTR at a really inconvenient time - just abandon
774 * the new directory on the server.
775 */
776 fc->ac.error = PTR_ERR(inode);
777 return;
778 }
779
David Howellsbc1527d2017-11-20 23:04:08 +0000780 d_add(new_dentry, inode);
David Howellsd2ddc772017-11-02 15:27:50 +0000781}
782
783/*
David Howells260a9802007-04-26 15:59:35 -0700784 * create a directory on an AFS filesystem
785 */
Al Viro18bb1db2011-07-26 01:41:39 -0400786static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Howells260a9802007-04-26 15:59:35 -0700787{
David Howellsd2ddc772017-11-02 15:27:50 +0000788 struct afs_file_status newstatus;
789 struct afs_fs_cursor fc;
790 struct afs_callback newcb;
791 struct afs_vnode *dvnode = AFS_FS_I(dir);
792 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -0700793 struct key *key;
794 int ret;
795
David Howellsd2ddc772017-11-02 15:27:50 +0000796 mode |= S_IFDIR;
David Howells260a9802007-04-26 15:59:35 -0700797
Al Viroa4555892014-10-21 20:11:25 -0400798 _enter("{%x:%u},{%pd},%ho",
799 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -0700800
David Howells260a9802007-04-26 15:59:35 -0700801 key = afs_request_key(dvnode->volume->cell);
802 if (IS_ERR(key)) {
803 ret = PTR_ERR(key);
804 goto error;
805 }
806
David Howellsd2ddc772017-11-02 15:27:50 +0000807 ret = -ERESTARTSYS;
808 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
809 while (afs_select_fileserver(&fc)) {
810 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
811 afs_fs_create(&fc, dentry->d_name.name, mode,
812 &newfid, &newstatus, &newcb);
813 }
David Howells260a9802007-04-26 15:59:35 -0700814
David Howellsd2ddc772017-11-02 15:27:50 +0000815 afs_check_for_remote_deletion(&fc, fc.vnode);
816 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
817 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
818 ret = afs_end_vnode_operation(&fc);
819 if (ret < 0)
820 goto error_key;
David Howells260a9802007-04-26 15:59:35 -0700821 }
822
David Howells260a9802007-04-26 15:59:35 -0700823 key_put(key);
824 _leave(" = 0");
825 return 0;
826
David Howellsd2ddc772017-11-02 15:27:50 +0000827error_key:
David Howells260a9802007-04-26 15:59:35 -0700828 key_put(key);
829error:
830 d_drop(dentry);
831 _leave(" = %d", ret);
832 return ret;
833}
834
835/*
David Howellsd2ddc772017-11-02 15:27:50 +0000836 * Remove a subdir from a directory.
David Howells260a9802007-04-26 15:59:35 -0700837 */
David Howellsd2ddc772017-11-02 15:27:50 +0000838static void afs_dir_remove_subdir(struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -0700839{
David Howells2b0143b2015-03-17 22:25:59 +0000840 if (d_really_is_positive(dentry)) {
David Howellsd2ddc772017-11-02 15:27:50 +0000841 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
842
David Howells260a9802007-04-26 15:59:35 -0700843 clear_nlink(&vnode->vfs_inode);
844 set_bit(AFS_VNODE_DELETED, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000845 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
David Howells260a9802007-04-26 15:59:35 -0700846 }
David Howells260a9802007-04-26 15:59:35 -0700847}
848
849/*
David Howellsd2ddc772017-11-02 15:27:50 +0000850 * remove a directory from an AFS filesystem
David Howells260a9802007-04-26 15:59:35 -0700851 */
David Howellsd2ddc772017-11-02 15:27:50 +0000852static int afs_rmdir(struct inode *dir, struct dentry *dentry)
David Howells260a9802007-04-26 15:59:35 -0700853{
David Howellsd2ddc772017-11-02 15:27:50 +0000854 struct afs_fs_cursor fc;
855 struct afs_vnode *dvnode = AFS_FS_I(dir);
David Howells260a9802007-04-26 15:59:35 -0700856 struct key *key;
857 int ret;
858
Al Viroa4555892014-10-21 20:11:25 -0400859 _enter("{%x:%u},{%pd}",
860 dvnode->fid.vid, dvnode->fid.vnode, dentry);
David Howells260a9802007-04-26 15:59:35 -0700861
David Howells260a9802007-04-26 15:59:35 -0700862 key = afs_request_key(dvnode->volume->cell);
863 if (IS_ERR(key)) {
864 ret = PTR_ERR(key);
865 goto error;
866 }
867
David Howellsd2ddc772017-11-02 15:27:50 +0000868 ret = -ERESTARTSYS;
869 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
870 while (afs_select_fileserver(&fc)) {
871 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
872 afs_fs_remove(&fc, dentry->d_name.name, true);
873 }
David Howells260a9802007-04-26 15:59:35 -0700874
David Howellsd2ddc772017-11-02 15:27:50 +0000875 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
876 ret = afs_end_vnode_operation(&fc);
877 if (ret == 0)
878 afs_dir_remove_subdir(dentry);
David Howells260a9802007-04-26 15:59:35 -0700879 }
880
881 key_put(key);
David Howellsd2ddc772017-11-02 15:27:50 +0000882error:
883 return ret;
884}
David Howells260a9802007-04-26 15:59:35 -0700885
David Howellsd2ddc772017-11-02 15:27:50 +0000886/*
887 * Remove a link to a file or symlink from a directory.
888 *
889 * If the file was not deleted due to excess hard links, the fileserver will
890 * break the callback promise on the file - if it had one - before it returns
891 * to us, and if it was deleted, it won't
892 *
893 * However, if we didn't have a callback promise outstanding, or it was
894 * outstanding on a different server, then it won't break it either...
895 */
896static int afs_dir_remove_link(struct dentry *dentry, struct key *key)
897{
898 int ret = 0;
899
900 if (d_really_is_positive(dentry)) {
901 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
902
903 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
904 kdebug("AFS_VNODE_DELETED");
905 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
906
907 ret = afs_validate(vnode, key);
908 if (ret == -ESTALE)
909 ret = 0;
910 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
911 }
912
913 return ret;
914}
915
916/*
917 * Remove a file or symlink from an AFS filesystem.
918 */
919static int afs_unlink(struct inode *dir, struct dentry *dentry)
920{
921 struct afs_fs_cursor fc;
922 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
923 struct key *key;
924 int ret;
925
926 _enter("{%x:%u},{%pd}",
927 dvnode->fid.vid, dvnode->fid.vnode, dentry);
928
929 if (dentry->d_name.len >= AFSNAMEMAX)
930 return -ENAMETOOLONG;
931
932 key = afs_request_key(dvnode->volume->cell);
933 if (IS_ERR(key)) {
934 ret = PTR_ERR(key);
935 goto error;
936 }
937
938 /* Try to make sure we have a callback promise on the victim. */
939 if (d_really_is_positive(dentry)) {
940 vnode = AFS_FS_I(d_inode(dentry));
941 ret = afs_validate(vnode, key);
942 if (ret < 0)
943 goto error_key;
944 }
945
946 ret = -ERESTARTSYS;
947 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
948 while (afs_select_fileserver(&fc)) {
949 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
950 afs_fs_remove(&fc, dentry->d_name.name, false);
951 }
952
953 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
954 ret = afs_end_vnode_operation(&fc);
955 if (ret == 0)
956 ret = afs_dir_remove_link(dentry, key);
957 }
958
959error_key:
David Howells260a9802007-04-26 15:59:35 -0700960 key_put(key);
961error:
962 _leave(" = %d", ret);
963 return ret;
964}
965
966/*
967 * create a regular file on an AFS filesystem
968 */
Al Viro4acdaf22011-07-26 01:42:34 -0400969static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400970 bool excl)
David Howells260a9802007-04-26 15:59:35 -0700971{
David Howellsd2ddc772017-11-02 15:27:50 +0000972 struct afs_fs_cursor fc;
973 struct afs_file_status newstatus;
974 struct afs_callback newcb;
975 struct afs_vnode *dvnode = dvnode = AFS_FS_I(dir);
976 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -0700977 struct key *key;
978 int ret;
979
David Howellsd2ddc772017-11-02 15:27:50 +0000980 mode |= S_IFREG;
David Howells260a9802007-04-26 15:59:35 -0700981
Al Viroa4555892014-10-21 20:11:25 -0400982 _enter("{%x:%u},{%pd},%ho,",
983 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
David Howells260a9802007-04-26 15:59:35 -0700984
David Howellsd2ddc772017-11-02 15:27:50 +0000985 ret = -ENAMETOOLONG;
986 if (dentry->d_name.len >= AFSNAMEMAX)
987 goto error;
988
David Howells260a9802007-04-26 15:59:35 -0700989 key = afs_request_key(dvnode->volume->cell);
990 if (IS_ERR(key)) {
991 ret = PTR_ERR(key);
992 goto error;
993 }
994
David Howellsd2ddc772017-11-02 15:27:50 +0000995 ret = -ERESTARTSYS;
996 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
997 while (afs_select_fileserver(&fc)) {
998 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
999 afs_fs_create(&fc, dentry->d_name.name, mode,
1000 &newfid, &newstatus, &newcb);
1001 }
David Howells260a9802007-04-26 15:59:35 -07001002
David Howellsd2ddc772017-11-02 15:27:50 +00001003 afs_check_for_remote_deletion(&fc, fc.vnode);
1004 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1005 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1006 ret = afs_end_vnode_operation(&fc);
1007 if (ret < 0)
1008 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001009 }
1010
David Howells260a9802007-04-26 15:59:35 -07001011 key_put(key);
1012 _leave(" = 0");
1013 return 0;
1014
David Howellsd2ddc772017-11-02 15:27:50 +00001015error_key:
David Howells260a9802007-04-26 15:59:35 -07001016 key_put(key);
1017error:
1018 d_drop(dentry);
1019 _leave(" = %d", ret);
1020 return ret;
1021}
1022
1023/*
1024 * create a hard link between files in an AFS filesystem
1025 */
1026static int afs_link(struct dentry *from, struct inode *dir,
1027 struct dentry *dentry)
1028{
David Howellsd2ddc772017-11-02 15:27:50 +00001029 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001030 struct afs_vnode *dvnode, *vnode;
1031 struct key *key;
1032 int ret;
1033
David Howells2b0143b2015-03-17 22:25:59 +00001034 vnode = AFS_FS_I(d_inode(from));
David Howells260a9802007-04-26 15:59:35 -07001035 dvnode = AFS_FS_I(dir);
1036
Al Viroa4555892014-10-21 20:11:25 -04001037 _enter("{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001038 vnode->fid.vid, vnode->fid.vnode,
1039 dvnode->fid.vid, dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001040 dentry);
David Howells260a9802007-04-26 15:59:35 -07001041
David Howellsd2ddc772017-11-02 15:27:50 +00001042 ret = -ENAMETOOLONG;
1043 if (dentry->d_name.len >= AFSNAMEMAX)
1044 goto error;
1045
David Howells260a9802007-04-26 15:59:35 -07001046 key = afs_request_key(dvnode->volume->cell);
1047 if (IS_ERR(key)) {
1048 ret = PTR_ERR(key);
1049 goto error;
1050 }
1051
David Howellsd2ddc772017-11-02 15:27:50 +00001052 ret = -ERESTARTSYS;
1053 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1054 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1055 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001056 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001057 }
David Howells260a9802007-04-26 15:59:35 -07001058
David Howellsd2ddc772017-11-02 15:27:50 +00001059 while (afs_select_fileserver(&fc)) {
1060 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1061 fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break;
1062 afs_fs_link(&fc, vnode, dentry->d_name.name);
1063 }
1064
1065 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1066 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1067 ihold(&vnode->vfs_inode);
1068 d_instantiate(dentry, &vnode->vfs_inode);
1069
1070 mutex_unlock(&vnode->io_lock);
1071 ret = afs_end_vnode_operation(&fc);
1072 if (ret < 0)
1073 goto error_key;
1074 }
1075
David Howells260a9802007-04-26 15:59:35 -07001076 key_put(key);
1077 _leave(" = 0");
1078 return 0;
1079
David Howellsd2ddc772017-11-02 15:27:50 +00001080error_key:
David Howells260a9802007-04-26 15:59:35 -07001081 key_put(key);
1082error:
1083 d_drop(dentry);
1084 _leave(" = %d", ret);
1085 return ret;
1086}
1087
1088/*
1089 * create a symlink in an AFS filesystem
1090 */
1091static int afs_symlink(struct inode *dir, struct dentry *dentry,
1092 const char *content)
1093{
David Howellsd2ddc772017-11-02 15:27:50 +00001094 struct afs_fs_cursor fc;
1095 struct afs_file_status newstatus;
1096 struct afs_vnode *dvnode = AFS_FS_I(dir);
1097 struct afs_fid newfid;
David Howells260a9802007-04-26 15:59:35 -07001098 struct key *key;
1099 int ret;
1100
Al Viroa4555892014-10-21 20:11:25 -04001101 _enter("{%x:%u},{%pd},%s",
1102 dvnode->fid.vid, dvnode->fid.vnode, dentry,
David Howells260a9802007-04-26 15:59:35 -07001103 content);
1104
David Howellsd2ddc772017-11-02 15:27:50 +00001105 ret = -ENAMETOOLONG;
1106 if (dentry->d_name.len >= AFSNAMEMAX)
1107 goto error;
1108
David Howells260a9802007-04-26 15:59:35 -07001109 ret = -EINVAL;
David Howells45222b92007-05-10 22:22:20 -07001110 if (strlen(content) >= AFSPATHMAX)
David Howells260a9802007-04-26 15:59:35 -07001111 goto error;
1112
1113 key = afs_request_key(dvnode->volume->cell);
1114 if (IS_ERR(key)) {
1115 ret = PTR_ERR(key);
1116 goto error;
1117 }
1118
David Howellsd2ddc772017-11-02 15:27:50 +00001119 ret = -ERESTARTSYS;
1120 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1121 while (afs_select_fileserver(&fc)) {
1122 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1123 afs_fs_symlink(&fc, dentry->d_name.name, content,
1124 &newfid, &newstatus);
1125 }
David Howells260a9802007-04-26 15:59:35 -07001126
David Howellsd2ddc772017-11-02 15:27:50 +00001127 afs_check_for_remote_deletion(&fc, fc.vnode);
1128 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1129 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1130 ret = afs_end_vnode_operation(&fc);
1131 if (ret < 0)
1132 goto error_key;
David Howells260a9802007-04-26 15:59:35 -07001133 }
1134
David Howells260a9802007-04-26 15:59:35 -07001135 key_put(key);
1136 _leave(" = 0");
1137 return 0;
1138
David Howellsd2ddc772017-11-02 15:27:50 +00001139error_key:
David Howells260a9802007-04-26 15:59:35 -07001140 key_put(key);
1141error:
1142 d_drop(dentry);
1143 _leave(" = %d", ret);
1144 return ret;
1145}
1146
1147/*
1148 * rename a file in an AFS filesystem and/or move it between directories
1149 */
1150static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001151 struct inode *new_dir, struct dentry *new_dentry,
1152 unsigned int flags)
David Howells260a9802007-04-26 15:59:35 -07001153{
David Howellsd2ddc772017-11-02 15:27:50 +00001154 struct afs_fs_cursor fc;
David Howells260a9802007-04-26 15:59:35 -07001155 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1156 struct key *key;
1157 int ret;
1158
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001159 if (flags)
1160 return -EINVAL;
1161
David Howells2b0143b2015-03-17 22:25:59 +00001162 vnode = AFS_FS_I(d_inode(old_dentry));
David Howells260a9802007-04-26 15:59:35 -07001163 orig_dvnode = AFS_FS_I(old_dir);
1164 new_dvnode = AFS_FS_I(new_dir);
1165
Al Viroa4555892014-10-21 20:11:25 -04001166 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
David Howells260a9802007-04-26 15:59:35 -07001167 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1168 vnode->fid.vid, vnode->fid.vnode,
1169 new_dvnode->fid.vid, new_dvnode->fid.vnode,
Al Viroa4555892014-10-21 20:11:25 -04001170 new_dentry);
David Howells260a9802007-04-26 15:59:35 -07001171
David Howells260a9802007-04-26 15:59:35 -07001172 key = afs_request_key(orig_dvnode->volume->cell);
1173 if (IS_ERR(key)) {
1174 ret = PTR_ERR(key);
1175 goto error;
1176 }
1177
David Howellsd2ddc772017-11-02 15:27:50 +00001178 ret = -ERESTARTSYS;
1179 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1180 if (orig_dvnode != new_dvnode) {
1181 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1182 afs_end_vnode_operation(&fc);
David Howellsbc1527d2017-11-20 23:04:08 +00001183 goto error_key;
David Howellsd2ddc772017-11-02 15:27:50 +00001184 }
1185 }
1186 while (afs_select_fileserver(&fc)) {
1187 fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break;
1188 fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break;
1189 afs_fs_rename(&fc, old_dentry->d_name.name,
1190 new_dvnode, new_dentry->d_name.name);
1191 }
1192
1193 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1194 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1195 if (orig_dvnode != new_dvnode)
1196 mutex_unlock(&new_dvnode->io_lock);
1197 ret = afs_end_vnode_operation(&fc);
1198 if (ret < 0)
1199 goto error_key;
1200 }
1201
David Howellsd2ddc772017-11-02 15:27:50 +00001202error_key:
David Howells260a9802007-04-26 15:59:35 -07001203 key_put(key);
1204error:
David Howells260a9802007-04-26 15:59:35 -07001205 _leave(" = %d", ret);
1206 return ret;
1207}