1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/nfs/dir.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * nfs directory handling functions
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * 10 Apr 1996 Added silly rename for unlink --okir
101da177e4SLinus Torvalds * 28 Sep 1996 Improved directory cache --okir
111da177e4SLinus Torvalds * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
121da177e4SLinus Torvalds * Re-implemented silly rename for unlink, newly implemented
131da177e4SLinus Torvalds * silly rename for nfs_rename() following the suggestions
141da177e4SLinus Torvalds * of Olaf Kirch (okir) found in this file.
151da177e4SLinus Torvalds * Following Linus comments on my original hack, this version
161da177e4SLinus Torvalds * depends only on the dcache stuff and doesn't touch the inode
171da177e4SLinus Torvalds * layer (iput() and friends).
181da177e4SLinus Torvalds * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
191da177e4SLinus Torvalds */
201da177e4SLinus Torvalds
21b6459415SJakub Kicinski #include <linux/compat.h>
22ddda8e0aSBryan Schumaker #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/time.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/stat.h>
261da177e4SLinus Torvalds #include <linux/fcntl.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
321da177e4SLinus Torvalds #include <linux/nfs_fs.h>
331da177e4SLinus Torvalds #include <linux/nfs_mount.h>
341da177e4SLinus Torvalds #include <linux/pagemap.h>
35873101b3SChuck Lever #include <linux/pagevec.h>
361da177e4SLinus Torvalds #include <linux/namei.h>
3754ceac45SDavid Howells #include <linux/mount.h>
38a0b8cab3SMel Gorman #include <linux/swap.h>
39e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
4004e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4164c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
42830f1111STrond Myklebust #include <linux/hash.h>
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds #include "delegation.h"
4591d5b470SChuck Lever #include "iostat.h"
464c30d56eSAdrian Bunk #include "internal.h"
47cd9a1c0eSTrond Myklebust #include "fscache.h"
481da177e4SLinus Torvalds
49f4ce1299STrond Myklebust #include "nfstrace.h"
50f4ce1299STrond Myklebust
511da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
54480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5523db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
57f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
58ec108d3cSAnna Schumaker static void nfs_readdir_clear_array(struct folio *);
591da177e4SLinus Torvalds
604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
61f0dd2136STrond Myklebust .llseek = nfs_llseek_dir,
621da177e4SLinus Torvalds .read = generic_read_dir,
6393a6ab7bSTrond Myklebust .iterate_shared = nfs_readdir,
641da177e4SLinus Torvalds .open = nfs_opendir,
65480c2006SBryan Schumaker .release = nfs_closedir,
661da177e4SLinus Torvalds .fsync = nfs_fsync_dir,
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds
6911de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
70ec108d3cSAnna Schumaker .free_folio = nfs_readdir_clear_array,
71d1bacf9eSBryan Schumaker };
72d1bacf9eSBryan Schumaker
73580f2367STrond Myklebust #define NFS_INIT_DTSIZE PAGE_SIZE
74580f2367STrond Myklebust
75281f31b2STrond Myklebust static struct nfs_open_dir_context *
alloc_nfs_open_dir_context(struct inode * dir)76281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir)
77480c2006SBryan Schumaker {
78311324adSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir);
79480c2006SBryan Schumaker struct nfs_open_dir_context *ctx;
80281f31b2STrond Myklebust
81281f31b2STrond Myklebust ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
82480c2006SBryan Schumaker if (ctx != NULL) {
83311324adSTrond Myklebust ctx->attr_gencount = nfsi->attr_gencount;
84580f2367STrond Myklebust ctx->dtsize = NFS_INIT_DTSIZE;
85311324adSTrond Myklebust spin_lock(&dir->i_lock);
861c341b77STrond Myklebust if (list_empty(&nfsi->open_files) &&
871c341b77STrond Myklebust (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
88ac46b3d7STrond Myklebust nfs_set_cache_invalid(dir,
89ac46b3d7STrond Myklebust NFS_INO_INVALID_DATA |
90ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED);
91230bc98fSTrond Myklebust list_add_tail_rcu(&ctx->list, &nfsi->open_files);
92d1e32ea3STrond Myklebust memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
93311324adSTrond Myklebust spin_unlock(&dir->i_lock);
94480c2006SBryan Schumaker return ctx;
95480c2006SBryan Schumaker }
960c030806STrond Myklebust return ERR_PTR(-ENOMEM);
970c030806STrond Myklebust }
98480c2006SBryan Schumaker
put_nfs_open_dir_context(struct inode * dir,struct nfs_open_dir_context * ctx)99311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
100480c2006SBryan Schumaker {
101311324adSTrond Myklebust spin_lock(&dir->i_lock);
102230bc98fSTrond Myklebust list_del_rcu(&ctx->list);
103311324adSTrond Myklebust spin_unlock(&dir->i_lock);
104230bc98fSTrond Myklebust kfree_rcu(ctx, rcu_head);
105480c2006SBryan Schumaker }
106480c2006SBryan Schumaker
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds * Open file
1091da177e4SLinus Torvalds */
1101da177e4SLinus Torvalds static int
nfs_opendir(struct inode * inode,struct file * filp)1111da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1121da177e4SLinus Torvalds {
113480c2006SBryan Schumaker int res = 0;
114480c2006SBryan Schumaker struct nfs_open_dir_context *ctx;
1151da177e4SLinus Torvalds
1166de1472fSAl Viro dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
117cc0dd2d1SChuck Lever
118cc0dd2d1SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1191e7cb3dcSChuck Lever
12093b8959aSTrond Myklebust ctx = alloc_nfs_open_dir_context(inode);
121480c2006SBryan Schumaker if (IS_ERR(ctx)) {
122480c2006SBryan Schumaker res = PTR_ERR(ctx);
123480c2006SBryan Schumaker goto out;
124480c2006SBryan Schumaker }
125480c2006SBryan Schumaker filp->private_data = ctx;
126480c2006SBryan Schumaker out:
1271da177e4SLinus Torvalds return res;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds
130480c2006SBryan Schumaker static int
nfs_closedir(struct inode * inode,struct file * filp)131480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
132480c2006SBryan Schumaker {
133a455589fSAl Viro put_nfs_open_dir_context(file_inode(filp), filp->private_data);
134480c2006SBryan Schumaker return 0;
135480c2006SBryan Schumaker }
136480c2006SBryan Schumaker
137d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
138d1bacf9eSBryan Schumaker u64 cookie;
139d1bacf9eSBryan Schumaker u64 ino;
140a52a8a6aSTrond Myklebust const char *name;
141a52a8a6aSTrond Myklebust unsigned int name_len;
1420b26a0bfSTrond Myklebust unsigned char d_type;
143d1bacf9eSBryan Schumaker };
144d1bacf9eSBryan Schumaker
145d1bacf9eSBryan Schumaker struct nfs_cache_array {
146d09e673fSTrond Myklebust u64 change_attr;
147d1bacf9eSBryan Schumaker u64 last_cookie;
148b1e21c97STrond Myklebust unsigned int size;
149ec108d3cSAnna Schumaker unsigned char folio_full : 1,
150ec108d3cSAnna Schumaker folio_is_eof : 1,
151762567b7STrond Myklebust cookies_are_ordered : 1;
1525601cda8SGustavo A. R. Silva struct nfs_cache_array_entry array[];
153d1bacf9eSBryan Schumaker };
154d1bacf9eSBryan Schumaker
1556c981effSTrond Myklebust struct nfs_readdir_descriptor {
1561da177e4SLinus Torvalds struct file *file;
15761f02e0aSAnna Schumaker struct folio *folio;
15823db8620SAl Viro struct dir_context *ctx;
15961f02e0aSAnna Schumaker pgoff_t folio_index;
16061f02e0aSAnna Schumaker pgoff_t folio_index_max;
1612e7a4641STrond Myklebust u64 dir_cookie;
1620aded708STrond Myklebust u64 last_cookie;
163f0dd2136STrond Myklebust loff_t current_index;
164d1bacf9eSBryan Schumaker
165b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE];
166a1147b82STrond Myklebust unsigned long dir_verifier;
1671f4eab7eSNeil Brown unsigned long timestamp;
1684704f0e2STrond Myklebust unsigned long gencount;
1692e7a4641STrond Myklebust unsigned long attr_gencount;
170d1bacf9eSBryan Schumaker unsigned int cache_entry_index;
171580f2367STrond Myklebust unsigned int buffer_fills;
172580f2367STrond Myklebust unsigned int dtsize;
173b0365ccbSTrond Myklebust bool clear_cache;
174a7a3b1e9SBenjamin Coddington bool plus;
175e1d2699bSTrond Myklebust bool eob;
176a7a3b1e9SBenjamin Coddington bool eof;
1776c981effSTrond Myklebust };
1781da177e4SLinus Torvalds
nfs_set_dtsize(struct nfs_readdir_descriptor * desc,unsigned int sz)179580f2367STrond Myklebust static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
180580f2367STrond Myklebust {
181580f2367STrond Myklebust struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
182580f2367STrond Myklebust unsigned int maxsize = server->dtsize;
183580f2367STrond Myklebust
184580f2367STrond Myklebust if (sz > maxsize)
185580f2367STrond Myklebust sz = maxsize;
186580f2367STrond Myklebust if (sz < NFS_MIN_FILE_IO_SIZE)
187580f2367STrond Myklebust sz = NFS_MIN_FILE_IO_SIZE;
188580f2367STrond Myklebust desc->dtsize = sz;
189580f2367STrond Myklebust }
190580f2367STrond Myklebust
nfs_shrink_dtsize(struct nfs_readdir_descriptor * desc)191580f2367STrond Myklebust static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
192580f2367STrond Myklebust {
193580f2367STrond Myklebust nfs_set_dtsize(desc, desc->dtsize >> 1);
194580f2367STrond Myklebust }
195580f2367STrond Myklebust
nfs_grow_dtsize(struct nfs_readdir_descriptor * desc)196580f2367STrond Myklebust static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
197580f2367STrond Myklebust {
198580f2367STrond Myklebust nfs_set_dtsize(desc, desc->dtsize << 1);
199580f2367STrond Myklebust }
200580f2367STrond Myklebust
nfs_readdir_folio_init_array(struct folio * folio,u64 last_cookie,u64 change_attr)201ec108d3cSAnna Schumaker static void nfs_readdir_folio_init_array(struct folio *folio, u64 last_cookie,
202d09e673fSTrond Myklebust u64 change_attr)
2034b310319STrond Myklebust {
2044b310319STrond Myklebust struct nfs_cache_array *array;
2054b310319STrond Myklebust
206ec108d3cSAnna Schumaker array = kmap_local_folio(folio, 0);
207d09e673fSTrond Myklebust array->change_attr = change_attr;
2081f1d4aa4STrond Myklebust array->last_cookie = last_cookie;
2099332cf14STrond Myklebust array->size = 0;
210ec108d3cSAnna Schumaker array->folio_full = 0;
211ec108d3cSAnna Schumaker array->folio_is_eof = 0;
212762567b7STrond Myklebust array->cookies_are_ordered = 1;
2131683ed16SFabio M. De Francesco kunmap_local(array);
2144b310319STrond Myklebust }
2154b310319STrond Myklebust
216d1bacf9eSBryan Schumaker /*
217d1bacf9eSBryan Schumaker * we are freeing strings created by nfs_add_to_readdir_array()
218d1bacf9eSBryan Schumaker */
nfs_readdir_clear_array(struct folio * folio)219ec108d3cSAnna Schumaker static void nfs_readdir_clear_array(struct folio *folio)
220d1bacf9eSBryan Schumaker {
22111de3b11STrond Myklebust struct nfs_cache_array *array;
2229332cf14STrond Myklebust unsigned int i;
2238cd51a0cSTrond Myklebust
224ec108d3cSAnna Schumaker array = kmap_local_folio(folio, 0);
225d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++)
226a52a8a6aSTrond Myklebust kfree(array->array[i].name);
2279332cf14STrond Myklebust array->size = 0;
2281683ed16SFabio M. De Francesco kunmap_local(array);
229d1bacf9eSBryan Schumaker }
230d1bacf9eSBryan Schumaker
nfs_readdir_folio_reinit_array(struct folio * folio,u64 last_cookie,u64 change_attr)231ec108d3cSAnna Schumaker static void nfs_readdir_folio_reinit_array(struct folio *folio, u64 last_cookie,
232b0365ccbSTrond Myklebust u64 change_attr)
233b0365ccbSTrond Myklebust {
234ec108d3cSAnna Schumaker nfs_readdir_clear_array(folio);
235ec108d3cSAnna Schumaker nfs_readdir_folio_init_array(folio, last_cookie, change_attr);
236b0365ccbSTrond Myklebust }
237b0365ccbSTrond Myklebust
238ec108d3cSAnna Schumaker static struct folio *
nfs_readdir_folio_array_alloc(u64 last_cookie,gfp_t gfp_flags)239ec108d3cSAnna Schumaker nfs_readdir_folio_array_alloc(u64 last_cookie, gfp_t gfp_flags)
24035df59d3STrond Myklebust {
241ec108d3cSAnna Schumaker struct folio *folio = folio_alloc(gfp_flags, 0);
242ec108d3cSAnna Schumaker if (folio)
243ec108d3cSAnna Schumaker nfs_readdir_folio_init_array(folio, last_cookie, 0);
244ec108d3cSAnna Schumaker return folio;
24535df59d3STrond Myklebust }
24635df59d3STrond Myklebust
nfs_readdir_folio_array_free(struct folio * folio)247ec108d3cSAnna Schumaker static void nfs_readdir_folio_array_free(struct folio *folio)
24835df59d3STrond Myklebust {
249ec108d3cSAnna Schumaker if (folio) {
250ec108d3cSAnna Schumaker nfs_readdir_clear_array(folio);
251ec108d3cSAnna Schumaker folio_put(folio);
25235df59d3STrond Myklebust }
25335df59d3STrond Myklebust }
25435df59d3STrond Myklebust
nfs_readdir_array_index_cookie(struct nfs_cache_array * array)255e47a62dfSTrond Myklebust static u64 nfs_readdir_array_index_cookie(struct nfs_cache_array *array)
256e47a62dfSTrond Myklebust {
257e47a62dfSTrond Myklebust return array->size == 0 ? array->last_cookie : array->array[0].cookie;
258e47a62dfSTrond Myklebust }
259e47a62dfSTrond Myklebust
nfs_readdir_array_set_eof(struct nfs_cache_array * array)260b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
261b1e21c97STrond Myklebust {
262ec108d3cSAnna Schumaker array->folio_is_eof = 1;
263ec108d3cSAnna Schumaker array->folio_full = 1;
264b1e21c97STrond Myklebust }
265b1e21c97STrond Myklebust
nfs_readdir_array_is_full(struct nfs_cache_array * array)266b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
267b1e21c97STrond Myklebust {
268ec108d3cSAnna Schumaker return array->folio_full;
269b1e21c97STrond Myklebust }
270b1e21c97STrond Myklebust
271d1bacf9eSBryan Schumaker /*
272d1bacf9eSBryan Schumaker * the caller is responsible for freeing qstr.name
273d1bacf9eSBryan Schumaker * when called by nfs_readdir_add_to_array, the strings will be freed in
274d1bacf9eSBryan Schumaker * nfs_clear_readdir_array()
275d1bacf9eSBryan Schumaker */
nfs_readdir_copy_name(const char * name,unsigned int len)276a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
277d1bacf9eSBryan Schumaker {
278a52a8a6aSTrond Myklebust const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
279a52a8a6aSTrond Myklebust
28004e4bd1cSCatalin Marinas /*
28104e4bd1cSCatalin Marinas * Avoid a kmemleak false positive. The pointer to the name is stored
28204e4bd1cSCatalin Marinas * in a page cache page which kmemleak does not scan.
28304e4bd1cSCatalin Marinas */
284a52a8a6aSTrond Myklebust if (ret != NULL)
285a52a8a6aSTrond Myklebust kmemleak_not_leak(ret);
286a52a8a6aSTrond Myklebust return ret;
287d1bacf9eSBryan Schumaker }
288d1bacf9eSBryan Schumaker
nfs_readdir_array_maxentries(void)2890b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void)
2900b2662b7STrond Myklebust {
2910b2662b7STrond Myklebust return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
2920b2662b7STrond Myklebust sizeof(struct nfs_cache_array_entry);
2930b2662b7STrond Myklebust }
2940b2662b7STrond Myklebust
295b1e21c97STrond Myklebust /*
296b1e21c97STrond Myklebust * Check that the next array entry lies entirely within the page bounds
297b1e21c97STrond Myklebust */
nfs_readdir_array_can_expand(struct nfs_cache_array * array)298b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
299b1e21c97STrond Myklebust {
300ec108d3cSAnna Schumaker if (array->folio_full)
301b1e21c97STrond Myklebust return -ENOSPC;
3020b2662b7STrond Myklebust if (array->size == nfs_readdir_array_maxentries()) {
303ec108d3cSAnna Schumaker array->folio_full = 1;
304b1e21c97STrond Myklebust return -ENOSPC;
305b1e21c97STrond Myklebust }
306d1bacf9eSBryan Schumaker return 0;
307d1bacf9eSBryan Schumaker }
308d1bacf9eSBryan Schumaker
nfs_readdir_folio_array_append(struct folio * folio,const struct nfs_entry * entry,u64 * cookie)309ec108d3cSAnna Schumaker static int nfs_readdir_folio_array_append(struct folio *folio,
3100adf85b4STrond Myklebust const struct nfs_entry *entry,
3110adf85b4STrond Myklebust u64 *cookie)
312d1bacf9eSBryan Schumaker {
313a52a8a6aSTrond Myklebust struct nfs_cache_array *array;
3144a201d6eSTrond Myklebust struct nfs_cache_array_entry *cache_entry;
315a52a8a6aSTrond Myklebust const char *name;
3160adf85b4STrond Myklebust int ret = -ENOMEM;
3174a201d6eSTrond Myklebust
318a52a8a6aSTrond Myklebust name = nfs_readdir_copy_name(entry->name, entry->len);
3193020093fSTrond Myklebust
3204b71e241SFabio M. De Francesco array = kmap_local_folio(folio, 0);
3210adf85b4STrond Myklebust if (!name)
3220adf85b4STrond Myklebust goto out;
323b1e21c97STrond Myklebust ret = nfs_readdir_array_can_expand(array);
324a52a8a6aSTrond Myklebust if (ret) {
325a52a8a6aSTrond Myklebust kfree(name);
3263020093fSTrond Myklebust goto out;
327a52a8a6aSTrond Myklebust }
3283020093fSTrond Myklebust
329b1e21c97STrond Myklebust cache_entry = &array->array[array->size];
3300adf85b4STrond Myklebust cache_entry->cookie = array->last_cookie;
3314a201d6eSTrond Myklebust cache_entry->ino = entry->ino;
3320b26a0bfSTrond Myklebust cache_entry->d_type = entry->d_type;
333a52a8a6aSTrond Myklebust cache_entry->name_len = entry->len;
334a52a8a6aSTrond Myklebust cache_entry->name = name;
335d1bacf9eSBryan Schumaker array->last_cookie = entry->cookie;
336762567b7STrond Myklebust if (array->last_cookie <= cache_entry->cookie)
337762567b7STrond Myklebust array->cookies_are_ordered = 0;
3388cd51a0cSTrond Myklebust array->size++;
33947c716cbSTrond Myklebust if (entry->eof != 0)
340b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array);
3414a201d6eSTrond Myklebust out:
3420adf85b4STrond Myklebust *cookie = array->last_cookie;
3434b71e241SFabio M. De Francesco kunmap_local(array);
3444a201d6eSTrond Myklebust return ret;
345d1bacf9eSBryan Schumaker }
346d1bacf9eSBryan Schumaker
347f648022fSTrond Myklebust #define NFS_READDIR_COOKIE_MASK (U32_MAX >> 14)
348f648022fSTrond Myklebust /*
349f648022fSTrond Myklebust * Hash algorithm allowing content addressible access to sequences
350f648022fSTrond Myklebust * of directory cookies. Content is addressed by the value of the
351f648022fSTrond Myklebust * cookie index of the first readdir entry in a page.
352f648022fSTrond Myklebust *
353830f1111STrond Myklebust * We select only the first 18 bits to avoid issues with excessive
354f648022fSTrond Myklebust * memory use for the page cache XArray. 18 bits should allow the caching
355f648022fSTrond Myklebust * of 262144 pages of sequences of readdir entries. Since each page holds
356f648022fSTrond Myklebust * 127 readdir entries for a typical 64-bit system, that works out to a
357f648022fSTrond Myklebust * cache of ~ 33 million entries per directory.
358f648022fSTrond Myklebust */
nfs_readdir_folio_cookie_hash(u64 cookie)359ec108d3cSAnna Schumaker static pgoff_t nfs_readdir_folio_cookie_hash(u64 cookie)
360f648022fSTrond Myklebust {
361f648022fSTrond Myklebust if (cookie == 0)
362f648022fSTrond Myklebust return 0;
363830f1111STrond Myklebust return hash_64(cookie, 18);
364f648022fSTrond Myklebust }
365f648022fSTrond Myklebust
nfs_readdir_folio_validate(struct folio * folio,u64 last_cookie,u64 change_attr)366ec108d3cSAnna Schumaker static bool nfs_readdir_folio_validate(struct folio *folio, u64 last_cookie,
367d09e673fSTrond Myklebust u64 change_attr)
368d09e673fSTrond Myklebust {
369ec108d3cSAnna Schumaker struct nfs_cache_array *array = kmap_local_folio(folio, 0);
370d09e673fSTrond Myklebust int ret = true;
371d09e673fSTrond Myklebust
372d09e673fSTrond Myklebust if (array->change_attr != change_attr)
373d09e673fSTrond Myklebust ret = false;
374e47a62dfSTrond Myklebust if (nfs_readdir_array_index_cookie(array) != last_cookie)
375d09e673fSTrond Myklebust ret = false;
3761683ed16SFabio M. De Francesco kunmap_local(array);
377d09e673fSTrond Myklebust return ret;
378d09e673fSTrond Myklebust }
379d09e673fSTrond Myklebust
nfs_readdir_folio_unlock_and_put(struct folio * folio)380ec108d3cSAnna Schumaker static void nfs_readdir_folio_unlock_and_put(struct folio *folio)
381d09e673fSTrond Myklebust {
382ec108d3cSAnna Schumaker folio_unlock(folio);
383ec108d3cSAnna Schumaker folio_put(folio);
384d09e673fSTrond Myklebust }
385d09e673fSTrond Myklebust
nfs_readdir_folio_init_and_validate(struct folio * folio,u64 cookie,u64 change_attr)386ec108d3cSAnna Schumaker static void nfs_readdir_folio_init_and_validate(struct folio *folio, u64 cookie,
387f648022fSTrond Myklebust u64 change_attr)
3881f1d4aa4STrond Myklebust {
389ec108d3cSAnna Schumaker if (folio_test_uptodate(folio)) {
390ec108d3cSAnna Schumaker if (nfs_readdir_folio_validate(folio, cookie, change_attr))
391648a4548STrond Myklebust return;
392ec108d3cSAnna Schumaker nfs_readdir_clear_array(folio);
393648a4548STrond Myklebust }
394ec108d3cSAnna Schumaker nfs_readdir_folio_init_array(folio, cookie, change_attr);
395ec108d3cSAnna Schumaker folio_mark_uptodate(folio);
396648a4548STrond Myklebust }
397648a4548STrond Myklebust
nfs_readdir_folio_get_locked(struct address_space * mapping,u64 cookie,u64 change_attr)398ec108d3cSAnna Schumaker static struct folio *nfs_readdir_folio_get_locked(struct address_space *mapping,
399648a4548STrond Myklebust u64 cookie, u64 change_attr)
400648a4548STrond Myklebust {
401ec108d3cSAnna Schumaker pgoff_t index = nfs_readdir_folio_cookie_hash(cookie);
402ec108d3cSAnna Schumaker struct folio *folio;
4031f1d4aa4STrond Myklebust
404ec108d3cSAnna Schumaker folio = filemap_grab_folio(mapping, index);
40516a88291SLinus Torvalds if (IS_ERR(folio))
406d09e673fSTrond Myklebust return NULL;
407ec108d3cSAnna Schumaker nfs_readdir_folio_init_and_validate(folio, cookie, change_attr);
408ec108d3cSAnna Schumaker return folio;
4091f1d4aa4STrond Myklebust }
4101f1d4aa4STrond Myklebust
nfs_readdir_folio_last_cookie(struct folio * folio)411ec108d3cSAnna Schumaker static u64 nfs_readdir_folio_last_cookie(struct folio *folio)
4121f1d4aa4STrond Myklebust {
4131f1d4aa4STrond Myklebust struct nfs_cache_array *array;
4141f1d4aa4STrond Myklebust u64 ret;
4151f1d4aa4STrond Myklebust
416ec108d3cSAnna Schumaker array = kmap_local_folio(folio, 0);
4171f1d4aa4STrond Myklebust ret = array->last_cookie;
4181683ed16SFabio M. De Francesco kunmap_local(array);
4191f1d4aa4STrond Myklebust return ret;
4201f1d4aa4STrond Myklebust }
4211f1d4aa4STrond Myklebust
nfs_readdir_folio_needs_filling(struct folio * folio)422ec108d3cSAnna Schumaker static bool nfs_readdir_folio_needs_filling(struct folio *folio)
4231f1d4aa4STrond Myklebust {
4241f1d4aa4STrond Myklebust struct nfs_cache_array *array;
4251f1d4aa4STrond Myklebust bool ret;
4261f1d4aa4STrond Myklebust
427ec108d3cSAnna Schumaker array = kmap_local_folio(folio, 0);
4281f1d4aa4STrond Myklebust ret = !nfs_readdir_array_is_full(array);
4291683ed16SFabio M. De Francesco kunmap_local(array);
4301f1d4aa4STrond Myklebust return ret;
4311f1d4aa4STrond Myklebust }
4321f1d4aa4STrond Myklebust
nfs_readdir_folio_set_eof(struct folio * folio)433ec108d3cSAnna Schumaker static void nfs_readdir_folio_set_eof(struct folio *folio)
434b1e21c97STrond Myklebust {
435b1e21c97STrond Myklebust struct nfs_cache_array *array;
436b1e21c97STrond Myklebust
437ec108d3cSAnna Schumaker array = kmap_local_folio(folio, 0);
438b1e21c97STrond Myklebust nfs_readdir_array_set_eof(array);
4391683ed16SFabio M. De Francesco kunmap_local(array);
440b1e21c97STrond Myklebust }
441b1e21c97STrond Myklebust
nfs_readdir_folio_get_next(struct address_space * mapping,u64 cookie,u64 change_attr)442ec108d3cSAnna Schumaker static struct folio *nfs_readdir_folio_get_next(struct address_space *mapping,
443f648022fSTrond Myklebust u64 cookie, u64 change_attr)
4443b2a09f1STrond Myklebust {
445ec108d3cSAnna Schumaker pgoff_t index = nfs_readdir_folio_cookie_hash(cookie);
446ec108d3cSAnna Schumaker struct folio *folio;
4473b2a09f1STrond Myklebust
448ec108d3cSAnna Schumaker folio = __filemap_get_folio(mapping, index,
449ec108d3cSAnna Schumaker FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
450ec108d3cSAnna Schumaker mapping_gfp_mask(mapping));
451994e2419SLinus Torvalds if (IS_ERR(folio))
4523b2a09f1STrond Myklebust return NULL;
453ec108d3cSAnna Schumaker nfs_readdir_folio_init_and_validate(folio, cookie, change_attr);
454ec108d3cSAnna Schumaker if (nfs_readdir_folio_last_cookie(folio) != cookie)
455ec108d3cSAnna Schumaker nfs_readdir_folio_reinit_array(folio, cookie, change_attr);
456ec108d3cSAnna Schumaker return folio;
4573b2a09f1STrond Myklebust }
4583b2a09f1STrond Myklebust
45959e356a9STrond Myklebust static inline
is_32bit_api(void)46059e356a9STrond Myklebust int is_32bit_api(void)
46159e356a9STrond Myklebust {
46259e356a9STrond Myklebust #ifdef CONFIG_COMPAT
46359e356a9STrond Myklebust return in_compat_syscall();
46459e356a9STrond Myklebust #else
46559e356a9STrond Myklebust return (BITS_PER_LONG == 32);
46659e356a9STrond Myklebust #endif
46759e356a9STrond Myklebust }
46859e356a9STrond Myklebust
46959e356a9STrond Myklebust static
nfs_readdir_use_cookie(const struct file * filp)47059e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
47159e356a9STrond Myklebust {
47259e356a9STrond Myklebust if ((filp->f_mode & FMODE_32BITHASH) ||
47359e356a9STrond Myklebust (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
47459e356a9STrond Myklebust return false;
47559e356a9STrond Myklebust return true;
47659e356a9STrond Myklebust }
47759e356a9STrond Myklebust
nfs_readdir_seek_next_array(struct nfs_cache_array * array,struct nfs_readdir_descriptor * desc)478c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
479c8f0523bSTrond Myklebust struct nfs_readdir_descriptor *desc)
480c8f0523bSTrond Myklebust {
481ec108d3cSAnna Schumaker if (array->folio_full) {
482c8f0523bSTrond Myklebust desc->last_cookie = array->last_cookie;
483c8f0523bSTrond Myklebust desc->current_index += array->size;
484c8f0523bSTrond Myklebust desc->cache_entry_index = 0;
48561f02e0aSAnna Schumaker desc->folio_index++;
486c8f0523bSTrond Myklebust } else
487e47a62dfSTrond Myklebust desc->last_cookie = nfs_readdir_array_index_cookie(array);
488c8f0523bSTrond Myklebust }
489c8f0523bSTrond Myklebust
nfs_readdir_rewind_search(struct nfs_readdir_descriptor * desc)490f648022fSTrond Myklebust static void nfs_readdir_rewind_search(struct nfs_readdir_descriptor *desc)
491f648022fSTrond Myklebust {
492f648022fSTrond Myklebust desc->current_index = 0;
493f648022fSTrond Myklebust desc->last_cookie = 0;
49461f02e0aSAnna Schumaker desc->folio_index = 0;
495f648022fSTrond Myklebust }
496f648022fSTrond Myklebust
nfs_readdir_search_for_pos(struct nfs_cache_array * array,struct nfs_readdir_descriptor * desc)4976c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
4986c981effSTrond Myklebust struct nfs_readdir_descriptor *desc)
499d1bacf9eSBryan Schumaker {
50023db8620SAl Viro loff_t diff = desc->ctx->pos - desc->current_index;
501d1bacf9eSBryan Schumaker unsigned int index;
502d1bacf9eSBryan Schumaker
503d1bacf9eSBryan Schumaker if (diff < 0)
504d1bacf9eSBryan Schumaker goto out_eof;
505d1bacf9eSBryan Schumaker if (diff >= array->size) {
506ec108d3cSAnna Schumaker if (array->folio_is_eof)
507d1bacf9eSBryan Schumaker goto out_eof;
508c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc);
509d1bacf9eSBryan Schumaker return -EAGAIN;
510d1bacf9eSBryan Schumaker }
511d1bacf9eSBryan Schumaker
512d1bacf9eSBryan Schumaker index = (unsigned int)diff;
5132e7a4641STrond Myklebust desc->dir_cookie = array->array[index].cookie;
514d1bacf9eSBryan Schumaker desc->cache_entry_index = index;
515d1bacf9eSBryan Schumaker return 0;
516d1bacf9eSBryan Schumaker out_eof:
5176089dd0dSThomas Meyer desc->eof = true;
518d1bacf9eSBryan Schumaker return -EBADCOOKIE;
519d1bacf9eSBryan Schumaker }
520d1bacf9eSBryan Schumaker
nfs_readdir_array_cookie_in_range(struct nfs_cache_array * array,u64 cookie)521762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
522762567b7STrond Myklebust u64 cookie)
523762567b7STrond Myklebust {
524762567b7STrond Myklebust if (!array->cookies_are_ordered)
525762567b7STrond Myklebust return true;
526762567b7STrond Myklebust /* Optimisation for monotonically increasing cookies */
527762567b7STrond Myklebust if (cookie >= array->last_cookie)
528762567b7STrond Myklebust return false;
529762567b7STrond Myklebust if (array->size && cookie < array->array[0].cookie)
530762567b7STrond Myklebust return false;
531762567b7STrond Myklebust return true;
532762567b7STrond Myklebust }
533762567b7STrond Myklebust
nfs_readdir_search_for_cookie(struct nfs_cache_array * array,struct nfs_readdir_descriptor * desc)5346c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
5356c981effSTrond Myklebust struct nfs_readdir_descriptor *desc)
536d1bacf9eSBryan Schumaker {
537f648022fSTrond Myklebust unsigned int i;
538d1bacf9eSBryan Schumaker int status = -EAGAIN;
539d1bacf9eSBryan Schumaker
540762567b7STrond Myklebust if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
541762567b7STrond Myklebust goto check_eof;
542762567b7STrond Myklebust
543d1bacf9eSBryan Schumaker for (i = 0; i < array->size; i++) {
5442e7a4641STrond Myklebust if (array->array[i].cookie == desc->dir_cookie) {
54559e356a9STrond Myklebust if (nfs_readdir_use_cookie(desc->file))
5462e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie;
54759e356a9STrond Myklebust else
548f648022fSTrond Myklebust desc->ctx->pos = desc->current_index + i;
5498cd51a0cSTrond Myklebust desc->cache_entry_index = i;
55047c716cbSTrond Myklebust return 0;
5518cd51a0cSTrond Myklebust }
5528cd51a0cSTrond Myklebust }
553762567b7STrond Myklebust check_eof:
554ec108d3cSAnna Schumaker if (array->folio_is_eof) {
555d1bacf9eSBryan Schumaker status = -EBADCOOKIE;
5562e7a4641STrond Myklebust if (desc->dir_cookie == array->last_cookie)
5576089dd0dSThomas Meyer desc->eof = true;
558c8f0523bSTrond Myklebust } else
559c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc);
560d1bacf9eSBryan Schumaker return status;
561d1bacf9eSBryan Schumaker }
562d1bacf9eSBryan Schumaker
nfs_readdir_search_array(struct nfs_readdir_descriptor * desc)5636c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
564d1bacf9eSBryan Schumaker {
565d1bacf9eSBryan Schumaker struct nfs_cache_array *array;
56647c716cbSTrond Myklebust int status;
567d1bacf9eSBryan Schumaker
56861f02e0aSAnna Schumaker array = kmap_local_folio(desc->folio, 0);
569d1bacf9eSBryan Schumaker
5702e7a4641STrond Myklebust if (desc->dir_cookie == 0)
571d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_pos(array, desc);
572d1bacf9eSBryan Schumaker else
573d1bacf9eSBryan Schumaker status = nfs_readdir_search_for_cookie(array, desc);
574d1bacf9eSBryan Schumaker
5751683ed16SFabio M. De Francesco kunmap_local(array);
576d1bacf9eSBryan Schumaker return status;
577d1bacf9eSBryan Schumaker }
578d1bacf9eSBryan Schumaker
579d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
nfs_readdir_xdr_filler(struct nfs_readdir_descriptor * desc,__be32 * verf,u64 cookie,struct page ** pages,size_t bufsize,__be32 * verf_res)58093b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
581b593c09fSTrond Myklebust __be32 *verf, u64 cookie,
582b593c09fSTrond Myklebust struct page **pages, size_t bufsize,
583b593c09fSTrond Myklebust __be32 *verf_res)
584d1bacf9eSBryan Schumaker {
58582e22a5eSTrond Myklebust struct inode *inode = file_inode(desc->file);
58682e22a5eSTrond Myklebust struct nfs_readdir_arg arg = {
58782e22a5eSTrond Myklebust .dentry = file_dentry(desc->file),
58882e22a5eSTrond Myklebust .cred = desc->file->f_cred,
589b593c09fSTrond Myklebust .verf = verf,
59082e22a5eSTrond Myklebust .cookie = cookie,
59182e22a5eSTrond Myklebust .pages = pages,
59282e22a5eSTrond Myklebust .page_len = bufsize,
59382e22a5eSTrond Myklebust .plus = desc->plus,
59482e22a5eSTrond Myklebust };
59582e22a5eSTrond Myklebust struct nfs_readdir_res res = {
59682e22a5eSTrond Myklebust .verf = verf_res,
59782e22a5eSTrond Myklebust };
5984704f0e2STrond Myklebust unsigned long timestamp, gencount;
5991da177e4SLinus Torvalds int error;
6001da177e4SLinus Torvalds
6011da177e4SLinus Torvalds again:
6021da177e4SLinus Torvalds timestamp = jiffies;
6034704f0e2STrond Myklebust gencount = nfs_inc_attr_generation_counter();
604a1147b82STrond Myklebust desc->dir_verifier = nfs_save_change_attribute(inode);
60582e22a5eSTrond Myklebust error = NFS_PROTO(inode)->readdir(&arg, &res);
6061da177e4SLinus Torvalds if (error < 0) {
6071da177e4SLinus Torvalds /* We requested READDIRPLUS, but the server doesn't grok it */
6081da177e4SLinus Torvalds if (error == -ENOTSUPP && desc->plus) {
6091da177e4SLinus Torvalds NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
61082e22a5eSTrond Myklebust desc->plus = arg.plus = false;
6111da177e4SLinus Torvalds goto again;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds goto error;
6141da177e4SLinus Torvalds }
6151f4eab7eSNeil Brown desc->timestamp = timestamp;
6164704f0e2STrond Myklebust desc->gencount = gencount;
617d1bacf9eSBryan Schumaker error:
618d1bacf9eSBryan Schumaker return error;
619d1bacf9eSBryan Schumaker }
620d1bacf9eSBryan Schumaker
xdr_decode(struct nfs_readdir_descriptor * desc,struct nfs_entry * entry,struct xdr_stream * xdr)6216c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
622573c4e1eSChuck Lever struct nfs_entry *entry, struct xdr_stream *xdr)
623d1bacf9eSBryan Schumaker {
62459e356a9STrond Myklebust struct inode *inode = file_inode(desc->file);
625573c4e1eSChuck Lever int error;
626d1bacf9eSBryan Schumaker
62759e356a9STrond Myklebust error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
628573c4e1eSChuck Lever if (error)
629573c4e1eSChuck Lever return error;
630d1bacf9eSBryan Schumaker entry->fattr->time_start = desc->timestamp;
631d1bacf9eSBryan Schumaker entry->fattr->gencount = desc->gencount;
632d1bacf9eSBryan Schumaker return 0;
633d1bacf9eSBryan Schumaker }
634d1bacf9eSBryan Schumaker
635fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
636fa923369STrond Myklebust * Note: caller is responsible for checking the fsid
637fa923369STrond Myklebust */
638d39ab9deSBryan Schumaker static
nfs_same_file(struct dentry * dentry,struct nfs_entry * entry)639d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
640d39ab9deSBryan Schumaker {
641d8fdb47fSTrond Myklebust struct inode *inode;
642fa923369STrond Myklebust struct nfs_inode *nfsi;
643fa923369STrond Myklebust
6442b0143b5SDavid Howells if (d_really_is_negative(dentry))
6452b0143b5SDavid Howells return 0;
646fa923369STrond Myklebust
647d8fdb47fSTrond Myklebust inode = d_inode(dentry);
648d8fdb47fSTrond Myklebust if (is_bad_inode(inode) || NFS_STALE(inode))
649d8fdb47fSTrond Myklebust return 0;
650d8fdb47fSTrond Myklebust
651d8fdb47fSTrond Myklebust nfsi = NFS_I(inode);
6527dc72d5fSTrond Myklebust if (entry->fattr->fileid != nfsi->fileid)
653d39ab9deSBryan Schumaker return 0;
6547dc72d5fSTrond Myklebust if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
6557dc72d5fSTrond Myklebust return 0;
6567dc72d5fSTrond Myklebust return 1;
657d39ab9deSBryan Schumaker }
658d39ab9deSBryan Schumaker
659230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL)
660230bc98fSTrond Myklebust
nfs_use_readdirplus(struct inode * dir,struct dir_context * ctx,unsigned int cache_hits,unsigned int cache_misses)661230bc98fSTrond Myklebust static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx,
662230bc98fSTrond Myklebust unsigned int cache_hits,
663230bc98fSTrond Myklebust unsigned int cache_misses)
664d69ee9b8STrond Myklebust {
665d69ee9b8STrond Myklebust if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
666d69ee9b8STrond Myklebust return false;
667230bc98fSTrond Myklebust if (ctx->pos == 0 ||
668230bc98fSTrond Myklebust cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD)
669d69ee9b8STrond Myklebust return true;
670d69ee9b8STrond Myklebust return false;
671d69ee9b8STrond Myklebust }
672d69ee9b8STrond Myklebust
673d69ee9b8STrond Myklebust /*
674230bc98fSTrond Myklebust * This function is called by the getattr code to request the
67563519fbcSTrond Myklebust * use of readdirplus to accelerate any future lookups in the same
676d69ee9b8STrond Myklebust * directory.
677d69ee9b8STrond Myklebust */
nfs_readdir_record_entry_cache_hit(struct inode * dir)678230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_hit(struct inode *dir)
679d69ee9b8STrond Myklebust {
68063519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir);
681230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx;
68263519fbcSTrond Myklebust
68363519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
684230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) {
685230bc98fSTrond Myklebust rcu_read_lock();
686230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
687230bc98fSTrond Myklebust atomic_inc(&ctx->cache_hits);
688230bc98fSTrond Myklebust rcu_read_unlock();
689230bc98fSTrond Myklebust }
690d69ee9b8STrond Myklebust }
691d69ee9b8STrond Myklebust
692311324adSTrond Myklebust /*
693311324adSTrond Myklebust * This function is mainly for use by nfs_getattr().
694311324adSTrond Myklebust *
695311324adSTrond Myklebust * If this is an 'ls -l', we want to force use of readdirplus.
696311324adSTrond Myklebust */
nfs_readdir_record_entry_cache_miss(struct inode * dir)697230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_miss(struct inode *dir)
698311324adSTrond Myklebust {
69963519fbcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(dir);
700230bc98fSTrond Myklebust struct nfs_open_dir_context *ctx;
70163519fbcSTrond Myklebust
70263519fbcSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
703230bc98fSTrond Myklebust S_ISDIR(dir->i_mode)) {
704230bc98fSTrond Myklebust rcu_read_lock();
705230bc98fSTrond Myklebust list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
706230bc98fSTrond Myklebust atomic_inc(&ctx->cache_misses);
707230bc98fSTrond Myklebust rcu_read_unlock();
708311324adSTrond Myklebust }
709311324adSTrond Myklebust }
710311324adSTrond Myklebust
nfs_lookup_advise_force_readdirplus(struct inode * dir,unsigned int flags)7110b3cc71bSTrond Myklebust static void nfs_lookup_advise_force_readdirplus(struct inode *dir,
7120b3cc71bSTrond Myklebust unsigned int flags)
713230bc98fSTrond Myklebust {
7142c2c3365STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
7152c2c3365STrond Myklebust return;
7160b3cc71bSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL))
7170b3cc71bSTrond Myklebust return;
718230bc98fSTrond Myklebust nfs_readdir_record_entry_cache_miss(dir);
719230bc98fSTrond Myklebust }
720230bc98fSTrond Myklebust
721d69ee9b8STrond Myklebust static
nfs_prime_dcache(struct dentry * parent,struct nfs_entry * entry,unsigned long dir_verifier)722a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
723a1147b82STrond Myklebust unsigned long dir_verifier)
724d39ab9deSBryan Schumaker {
72526fe5750SLinus Torvalds struct qstr filename = QSTR_INIT(entry->name, entry->len);
7269ac3d3e8SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
7274a201d6eSTrond Myklebust struct dentry *dentry;
7284a201d6eSTrond Myklebust struct dentry *alias;
729d39ab9deSBryan Schumaker struct inode *inode;
730aa9c2669SDavid Quigley int status;
731d39ab9deSBryan Schumaker
732fa923369STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
733fa923369STrond Myklebust return;
7346c441c25STrond Myklebust if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
7356c441c25STrond Myklebust return;
73678d04af4STrond Myklebust if (filename.len == 0)
73778d04af4STrond Myklebust return;
73878d04af4STrond Myklebust /* Validate that the name doesn't contain any illegal '\0' */
73978d04af4STrond Myklebust if (strnlen(filename.name, filename.len) != filename.len)
74078d04af4STrond Myklebust return;
74178d04af4STrond Myklebust /* ...or '/' */
74278d04af4STrond Myklebust if (strnchr(filename.name, filename.len, '/'))
74378d04af4STrond Myklebust return;
7444a201d6eSTrond Myklebust if (filename.name[0] == '.') {
7454a201d6eSTrond Myklebust if (filename.len == 1)
7464a201d6eSTrond Myklebust return;
7474a201d6eSTrond Myklebust if (filename.len == 2 && filename.name[1] == '.')
7484a201d6eSTrond Myklebust return;
7494a201d6eSTrond Myklebust }
7508387ff25SLinus Torvalds filename.hash = full_name_hash(parent, filename.name, filename.len);
751d39ab9deSBryan Schumaker
7524a201d6eSTrond Myklebust dentry = d_lookup(parent, &filename);
7539ac3d3e8SAl Viro again:
7549ac3d3e8SAl Viro if (!dentry) {
7559ac3d3e8SAl Viro dentry = d_alloc_parallel(parent, &filename, &wq);
7569ac3d3e8SAl Viro if (IS_ERR(dentry))
7579ac3d3e8SAl Viro return;
7589ac3d3e8SAl Viro }
7599ac3d3e8SAl Viro if (!d_in_lookup(dentry)) {
7606c441c25STrond Myklebust /* Is there a mountpoint here? If so, just exit */
7616c441c25STrond Myklebust if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
7626c441c25STrond Myklebust &entry->fattr->fsid))
7636c441c25STrond Myklebust goto out;
764d39ab9deSBryan Schumaker if (nfs_same_file(dentry, entry)) {
7657dc72d5fSTrond Myklebust if (!entry->fh->size)
7667dc72d5fSTrond Myklebust goto out;
767a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier);
7682b0143b5SDavid Howells status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
769aa9c2669SDavid Quigley if (!status)
770dd225cb3SAnna Schumaker nfs_setsecurity(d_inode(dentry), entry->fattr);
771eace45a1STrond Myklebust trace_nfs_readdir_lookup_revalidate(d_inode(parent),
772eace45a1STrond Myklebust dentry, 0, status);
773d39ab9deSBryan Schumaker goto out;
774d39ab9deSBryan Schumaker } else {
775eace45a1STrond Myklebust trace_nfs_readdir_lookup_revalidate_failed(
776eace45a1STrond Myklebust d_inode(parent), dentry, 0);
7775542aa2fSEric W. Biederman d_invalidate(dentry);
778d39ab9deSBryan Schumaker dput(dentry);
7799ac3d3e8SAl Viro dentry = NULL;
7809ac3d3e8SAl Viro goto again;
781d39ab9deSBryan Schumaker }
782d39ab9deSBryan Schumaker }
7837dc72d5fSTrond Myklebust if (!entry->fh->size) {
7847dc72d5fSTrond Myklebust d_lookup_done(dentry);
7857dc72d5fSTrond Myklebust goto out;
7867dc72d5fSTrond Myklebust }
787d39ab9deSBryan Schumaker
788cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
78941d28bcaSAl Viro alias = d_splice_alias(inode, dentry);
7909ac3d3e8SAl Viro d_lookup_done(dentry);
7919ac3d3e8SAl Viro if (alias) {
792d39ab9deSBryan Schumaker if (IS_ERR(alias))
793d39ab9deSBryan Schumaker goto out;
7949ac3d3e8SAl Viro dput(dentry);
7959ac3d3e8SAl Viro dentry = alias;
7969ac3d3e8SAl Viro }
797a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier);
798eace45a1STrond Myklebust trace_nfs_readdir_lookup(d_inode(parent), dentry, 0);
799d39ab9deSBryan Schumaker out:
800d39ab9deSBryan Schumaker dput(dentry);
801d39ab9deSBryan Schumaker }
802d39ab9deSBryan Schumaker
nfs_readdir_entry_decode(struct nfs_readdir_descriptor * desc,struct nfs_entry * entry,struct xdr_stream * stream)803612896ecSTrond Myklebust static int nfs_readdir_entry_decode(struct nfs_readdir_descriptor *desc,
804612896ecSTrond Myklebust struct nfs_entry *entry,
805612896ecSTrond Myklebust struct xdr_stream *stream)
806612896ecSTrond Myklebust {
807612896ecSTrond Myklebust int ret;
808612896ecSTrond Myklebust
809612896ecSTrond Myklebust if (entry->fattr->label)
810612896ecSTrond Myklebust entry->fattr->label->len = NFS4_MAXLABELLEN;
811612896ecSTrond Myklebust ret = xdr_decode(desc, entry, stream);
812612896ecSTrond Myklebust if (ret || !desc->plus)
813612896ecSTrond Myklebust return ret;
814612896ecSTrond Myklebust nfs_prime_dcache(file_dentry(desc->file), entry, desc->dir_verifier);
815612896ecSTrond Myklebust return 0;
816612896ecSTrond Myklebust }
817612896ecSTrond Myklebust
818d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
nfs_readdir_folio_filler(struct nfs_readdir_descriptor * desc,struct nfs_entry * entry,struct page ** xdr_pages,unsigned int buflen,struct folio ** arrays,size_t narrays,u64 change_attr)81961f02e0aSAnna Schumaker static int nfs_readdir_folio_filler(struct nfs_readdir_descriptor *desc,
8203b2a09f1STrond Myklebust struct nfs_entry *entry,
821f648022fSTrond Myklebust struct page **xdr_pages, unsigned int buflen,
82261f02e0aSAnna Schumaker struct folio **arrays, size_t narrays,
823f648022fSTrond Myklebust u64 change_attr)
824d1bacf9eSBryan Schumaker {
8253b2a09f1STrond Myklebust struct address_space *mapping = desc->file->f_mapping;
826ec108d3cSAnna Schumaker struct folio *new, *folio = *arrays;
827babddc72SBryan Schumaker struct xdr_stream stream;
828ec108d3cSAnna Schumaker struct page *scratch;
829f7da7a12SBenny Halevy struct xdr_buf buf;
8300adf85b4STrond Myklebust u64 cookie;
8315c346854STrond Myklebust int status;
832babddc72SBryan Schumaker
8336650239aSTrond Myklebust scratch = alloc_page(GFP_KERNEL);
8346650239aSTrond Myklebust if (scratch == NULL)
8356650239aSTrond Myklebust return -ENOMEM;
836babddc72SBryan Schumaker
837f7da7a12SBenny Halevy xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
8380ae4c3e8SChuck Lever xdr_set_scratch_page(&stream, scratch);
83999424380SBryan Schumaker
84099424380SBryan Schumaker do {
841612896ecSTrond Myklebust status = nfs_readdir_entry_decode(desc, entry, &stream);
842972bcdf2STrond Myklebust if (status != 0)
84399424380SBryan Schumaker break;
8445c346854STrond Myklebust
845ec108d3cSAnna Schumaker status = nfs_readdir_folio_array_append(folio, entry, &cookie);
8463b2a09f1STrond Myklebust if (status != -ENOSPC)
8473b2a09f1STrond Myklebust continue;
84899424380SBryan Schumaker
84961f02e0aSAnna Schumaker if (folio->mapping != mapping) {
85035df59d3STrond Myklebust if (!--narrays)
8513b2a09f1STrond Myklebust break;
852ec108d3cSAnna Schumaker new = nfs_readdir_folio_array_alloc(cookie, GFP_KERNEL);
85335df59d3STrond Myklebust if (!new)
85435df59d3STrond Myklebust break;
85535df59d3STrond Myklebust arrays++;
856ec108d3cSAnna Schumaker *arrays = folio = new;
85735df59d3STrond Myklebust } else {
858ec108d3cSAnna Schumaker new = nfs_readdir_folio_get_next(mapping, cookie,
8590adf85b4STrond Myklebust change_attr);
8603b2a09f1STrond Myklebust if (!new)
8613b2a09f1STrond Myklebust break;
86261f02e0aSAnna Schumaker if (folio != *arrays)
863ec108d3cSAnna Schumaker nfs_readdir_folio_unlock_and_put(folio);
864ec108d3cSAnna Schumaker folio = new;
86556e4ebf8SBryan Schumaker }
86661f02e0aSAnna Schumaker desc->folio_index_max++;
867ec108d3cSAnna Schumaker status = nfs_readdir_folio_array_append(folio, entry, &cookie);
868972bcdf2STrond Myklebust } while (!status && !entry->eof);
86956e4ebf8SBryan Schumaker
870972bcdf2STrond Myklebust switch (status) {
871972bcdf2STrond Myklebust case -EBADCOOKIE:
872612896ecSTrond Myklebust if (!entry->eof)
873972bcdf2STrond Myklebust break;
874ec108d3cSAnna Schumaker nfs_readdir_folio_set_eof(folio);
875612896ecSTrond Myklebust fallthrough;
876972bcdf2STrond Myklebust case -EAGAIN:
877972bcdf2STrond Myklebust status = 0;
878972bcdf2STrond Myklebust break;
879612896ecSTrond Myklebust case -ENOSPC:
880612896ecSTrond Myklebust status = 0;
881612896ecSTrond Myklebust if (!desc->plus)
882612896ecSTrond Myklebust break;
883612896ecSTrond Myklebust while (!nfs_readdir_entry_decode(desc, entry, &stream))
884612896ecSTrond Myklebust ;
885972bcdf2STrond Myklebust }
8866650239aSTrond Myklebust
88761f02e0aSAnna Schumaker if (folio != *arrays)
888ec108d3cSAnna Schumaker nfs_readdir_folio_unlock_and_put(folio);
8896650239aSTrond Myklebust
8906650239aSTrond Myklebust put_page(scratch);
8918cd51a0cSTrond Myklebust return status;
8928cd51a0cSTrond Myklebust }
89356e4ebf8SBryan Schumaker
nfs_readdir_free_pages(struct page ** pages,size_t npages)8941a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
89556e4ebf8SBryan Schumaker {
8961a34c8c9STrond Myklebust while (npages--)
8971a34c8c9STrond Myklebust put_page(pages[npages]);
8981a34c8c9STrond Myklebust kfree(pages);
89956e4ebf8SBryan Schumaker }
90056e4ebf8SBryan Schumaker
90156e4ebf8SBryan Schumaker /*
902bf211ca1Szhangliguang * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
903bf211ca1Szhangliguang * to nfs_readdir_free_pages()
90456e4ebf8SBryan Schumaker */
nfs_readdir_alloc_pages(size_t npages)9051a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
90656e4ebf8SBryan Schumaker {
9071a34c8c9STrond Myklebust struct page **pages;
9081a34c8c9STrond Myklebust size_t i;
90956e4ebf8SBryan Schumaker
9101a34c8c9STrond Myklebust pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
9111a34c8c9STrond Myklebust if (!pages)
9121a34c8c9STrond Myklebust return NULL;
91356e4ebf8SBryan Schumaker for (i = 0; i < npages; i++) {
91456e4ebf8SBryan Schumaker struct page *page = alloc_page(GFP_KERNEL);
91556e4ebf8SBryan Schumaker if (page == NULL)
91656e4ebf8SBryan Schumaker goto out_freepages;
91756e4ebf8SBryan Schumaker pages[i] = page;
91856e4ebf8SBryan Schumaker }
9191a34c8c9STrond Myklebust return pages;
92056e4ebf8SBryan Schumaker
92156e4ebf8SBryan Schumaker out_freepages:
922c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, i);
9231a34c8c9STrond Myklebust return NULL;
924d1bacf9eSBryan Schumaker }
925d1bacf9eSBryan Schumaker
nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor * desc,__be32 * verf_arg,__be32 * verf_res,struct folio ** arrays,size_t narrays)9266c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
92735df59d3STrond Myklebust __be32 *verf_arg, __be32 *verf_res,
92861f02e0aSAnna Schumaker struct folio **arrays, size_t narrays)
929d1bacf9eSBryan Schumaker {
930f648022fSTrond Myklebust u64 change_attr;
9311a34c8c9STrond Myklebust struct page **pages;
93261f02e0aSAnna Schumaker struct folio *folio = *arrays;
9336b75cf9eSTrond Myklebust struct nfs_entry *entry;
9341a34c8c9STrond Myklebust size_t array_size;
935b593c09fSTrond Myklebust struct inode *inode = file_inode(desc->file);
936580f2367STrond Myklebust unsigned int dtsize = desc->dtsize;
9379ff89c25STrond Myklebust unsigned int pglen;
9388cd51a0cSTrond Myklebust int status = -ENOMEM;
939d1bacf9eSBryan Schumaker
9406b75cf9eSTrond Myklebust entry = kzalloc(sizeof(*entry), GFP_KERNEL);
9416b75cf9eSTrond Myklebust if (!entry)
9426b75cf9eSTrond Myklebust return -ENOMEM;
943ec108d3cSAnna Schumaker entry->cookie = nfs_readdir_folio_last_cookie(folio);
9446b75cf9eSTrond Myklebust entry->fh = nfs_alloc_fhandle();
945b1db9a40SAnna Schumaker entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
9466b75cf9eSTrond Myklebust entry->server = NFS_SERVER(inode);
9476b75cf9eSTrond Myklebust if (entry->fh == NULL || entry->fattr == NULL)
948d1bacf9eSBryan Schumaker goto out;
949d1bacf9eSBryan Schumaker
9501a34c8c9STrond Myklebust array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
9511a34c8c9STrond Myklebust pages = nfs_readdir_alloc_pages(array_size);
9521a34c8c9STrond Myklebust if (!pages)
953b1db9a40SAnna Schumaker goto out;
954d1bacf9eSBryan Schumaker
955f648022fSTrond Myklebust change_attr = inode_peek_iversion_raw(inode);
9569ff89c25STrond Myklebust status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
9579ff89c25STrond Myklebust dtsize, verf_res);
958d1bacf9eSBryan Schumaker if (status < 0)
9599ff89c25STrond Myklebust goto free_pages;
960972bcdf2STrond Myklebust
961ac396128STrond Myklebust pglen = status;
9629ff89c25STrond Myklebust if (pglen != 0)
96361f02e0aSAnna Schumaker status = nfs_readdir_folio_filler(desc, entry, pages, pglen,
964f648022fSTrond Myklebust arrays, narrays, change_attr);
9659ff89c25STrond Myklebust else
966ec108d3cSAnna Schumaker nfs_readdir_folio_set_eof(folio);
967580f2367STrond Myklebust desc->buffer_fills++;
968d1bacf9eSBryan Schumaker
9699ff89c25STrond Myklebust free_pages:
970c7e9668eSAnna Schumaker nfs_readdir_free_pages(pages, array_size);
971d1bacf9eSBryan Schumaker out:
9726b75cf9eSTrond Myklebust nfs_free_fattr(entry->fattr);
9736b75cf9eSTrond Myklebust nfs_free_fhandle(entry->fh);
9746b75cf9eSTrond Myklebust kfree(entry);
975d1bacf9eSBryan Schumaker return status;
976d1bacf9eSBryan Schumaker }
977d1bacf9eSBryan Schumaker
nfs_readdir_folio_put(struct nfs_readdir_descriptor * desc)97861f02e0aSAnna Schumaker static void nfs_readdir_folio_put(struct nfs_readdir_descriptor *desc)
9791da177e4SLinus Torvalds {
98061f02e0aSAnna Schumaker folio_put(desc->folio);
98161f02e0aSAnna Schumaker desc->folio = NULL;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds
9841f1d4aa4STrond Myklebust static void
nfs_readdir_folio_unlock_and_put_cached(struct nfs_readdir_descriptor * desc)98561f02e0aSAnna Schumaker nfs_readdir_folio_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
9861da177e4SLinus Torvalds {
98761f02e0aSAnna Schumaker folio_unlock(desc->folio);
98861f02e0aSAnna Schumaker nfs_readdir_folio_put(desc);
9891f1d4aa4STrond Myklebust }
9901f1d4aa4STrond Myklebust
99161f02e0aSAnna Schumaker static struct folio *
nfs_readdir_folio_get_cached(struct nfs_readdir_descriptor * desc)99261f02e0aSAnna Schumaker nfs_readdir_folio_get_cached(struct nfs_readdir_descriptor *desc)
9931f1d4aa4STrond Myklebust {
994f648022fSTrond Myklebust struct address_space *mapping = desc->file->f_mapping;
995f648022fSTrond Myklebust u64 change_attr = inode_peek_iversion_raw(mapping->host);
996b0365ccbSTrond Myklebust u64 cookie = desc->last_cookie;
997ec108d3cSAnna Schumaker struct folio *folio;
998f648022fSTrond Myklebust
999ec108d3cSAnna Schumaker folio = nfs_readdir_folio_get_locked(mapping, cookie, change_attr);
1000ec108d3cSAnna Schumaker if (!folio)
1001b0365ccbSTrond Myklebust return NULL;
1002ec108d3cSAnna Schumaker if (desc->clear_cache && !nfs_readdir_folio_needs_filling(folio))
1003ec108d3cSAnna Schumaker nfs_readdir_folio_reinit_array(folio, cookie, change_attr);
1004ec108d3cSAnna Schumaker return folio;
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds
10071da177e4SLinus Torvalds /*
1008d1bacf9eSBryan Schumaker * Returns 0 if desc->dir_cookie was found on page desc->page_index
1009114de382STrond Myklebust * and locks the page to prevent removal from the page cache.
10101da177e4SLinus Torvalds */
find_and_lock_cache_page(struct nfs_readdir_descriptor * desc)10116c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
1012d1bacf9eSBryan Schumaker {
1013227823d2SDai Ngo struct inode *inode = file_inode(desc->file);
1014227823d2SDai Ngo struct nfs_inode *nfsi = NFS_I(inode);
1015b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE];
1016d1bacf9eSBryan Schumaker int res;
1017d1bacf9eSBryan Schumaker
101861f02e0aSAnna Schumaker desc->folio = nfs_readdir_folio_get_cached(desc);
101961f02e0aSAnna Schumaker if (!desc->folio)
10201f1d4aa4STrond Myklebust return -ENOMEM;
1021ec108d3cSAnna Schumaker if (nfs_readdir_folio_needs_filling(desc->folio)) {
1022580f2367STrond Myklebust /* Grow the dtsize if we had to go back for more pages */
102361f02e0aSAnna Schumaker if (desc->folio_index == desc->folio_index_max)
1024580f2367STrond Myklebust nfs_grow_dtsize(desc);
102561f02e0aSAnna Schumaker desc->folio_index_max = desc->folio_index;
1026310e3187STrond Myklebust trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf,
1027310e3187STrond Myklebust desc->last_cookie,
102861f02e0aSAnna Schumaker desc->folio->index, desc->dtsize);
102935df59d3STrond Myklebust res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
103061f02e0aSAnna Schumaker &desc->folio, 1);
10319fff59edSTrond Myklebust if (res < 0) {
103261f02e0aSAnna Schumaker nfs_readdir_folio_unlock_and_put_cached(desc);
1033310e3187STrond Myklebust trace_nfs_readdir_cache_fill_done(inode, res);
10349fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC) {
10359fff59edSTrond Myklebust invalidate_inode_pages2(desc->file->f_mapping);
1036f648022fSTrond Myklebust nfs_readdir_rewind_search(desc);
103711d03d0aSTrond Myklebust trace_nfs_readdir_invalidate_cache_range(
103811d03d0aSTrond Myklebust inode, 0, MAX_LFS_FILESIZE);
10399fff59edSTrond Myklebust return -EAGAIN;
10409fff59edSTrond Myklebust }
10419fff59edSTrond Myklebust return res;
10429fff59edSTrond Myklebust }
1043f892c41cSTrond Myklebust /*
1044f892c41cSTrond Myklebust * Set the cookie verifier if the page cache was empty
1045f892c41cSTrond Myklebust */
10466c34f05bSTrond Myklebust if (desc->last_cookie == 0 &&
10476c34f05bSTrond Myklebust memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
1048f892c41cSTrond Myklebust memcpy(nfsi->cookieverf, verf,
1049f892c41cSTrond Myklebust sizeof(nfsi->cookieverf));
1050f648022fSTrond Myklebust invalidate_inode_pages2_range(desc->file->f_mapping, 1,
10516c34f05bSTrond Myklebust -1);
105211d03d0aSTrond Myklebust trace_nfs_readdir_invalidate_cache_range(
1053f648022fSTrond Myklebust inode, 1, MAX_LFS_FILESIZE);
10546c34f05bSTrond Myklebust }
1055b0365ccbSTrond Myklebust desc->clear_cache = false;
10561f1d4aa4STrond Myklebust }
1057114de382STrond Myklebust res = nfs_readdir_search_array(desc);
1058ff81dfb5STrond Myklebust if (res == 0)
1059114de382STrond Myklebust return 0;
106061f02e0aSAnna Schumaker nfs_readdir_folio_unlock_and_put_cached(desc);
1061d1bacf9eSBryan Schumaker return res;
1062d1bacf9eSBryan Schumaker }
1063d1bacf9eSBryan Schumaker
1064d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
readdir_search_pagecache(struct nfs_readdir_descriptor * desc)10656c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
10661da177e4SLinus Torvalds {
10678cd51a0cSTrond Myklebust int res;
1068d1bacf9eSBryan Schumaker
10699fff59edSTrond Myklebust do {
1070114de382STrond Myklebust res = find_and_lock_cache_page(desc);
107147c716cbSTrond Myklebust } while (res == -EAGAIN);
10721da177e4SLinus Torvalds return res;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds
107585aa8ddcSBenjamin Coddington #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
107685aa8ddcSBenjamin Coddington
10771da177e4SLinus Torvalds /*
10781da177e4SLinus Torvalds * Once we've found the start of the dirent within a page: fill 'er up...
10791da177e4SLinus Torvalds */
nfs_do_filldir(struct nfs_readdir_descriptor * desc,const __be32 * verf)108013884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
108113884ff2STrond Myklebust const __be32 *verf)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds struct file *file = desc->file;
1084dbeaf8c9STrond Myklebust struct nfs_cache_array *array;
1085c8f0523bSTrond Myklebust unsigned int i;
108685aa8ddcSBenjamin Coddington bool first_emit = !desc->dir_cookie;
10878ef2ce3eSBryan Schumaker
108861f02e0aSAnna Schumaker array = kmap_local_folio(desc->folio, 0);
1089d1bacf9eSBryan Schumaker for (i = desc->cache_entry_index; i < array->size; i++) {
1090ece0b423STrond Myklebust struct nfs_cache_array_entry *ent;
10911da177e4SLinus Torvalds
109214e7316aSKinglong Mee /*
109314e7316aSKinglong Mee * nfs_readdir_handle_cache_misses return force clear at
109414e7316aSKinglong Mee * (cache_misses > NFS_READDIR_CACHE_MISS_THRESHOLD) for
109514e7316aSKinglong Mee * readdir heuristic, NFS_READDIR_CACHE_MISS_THRESHOLD + 1
109614e7316aSKinglong Mee * entries need be emitted here.
109714e7316aSKinglong Mee */
109814e7316aSKinglong Mee if (first_emit && i > NFS_READDIR_CACHE_MISS_THRESHOLD + 2) {
109914e7316aSKinglong Mee desc->eob = true;
110014e7316aSKinglong Mee break;
110114e7316aSKinglong Mee }
110214e7316aSKinglong Mee
1103ece0b423STrond Myklebust ent = &array->array[i];
1104a52a8a6aSTrond Myklebust if (!dir_emit(desc->ctx, ent->name, ent->name_len,
110523db8620SAl Viro nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1106e1d2699bSTrond Myklebust desc->eob = true;
11071da177e4SLinus Torvalds break;
1108ece0b423STrond Myklebust }
110913884ff2STrond Myklebust memcpy(desc->verf, verf, sizeof(desc->verf));
1110c8f0523bSTrond Myklebust if (i == array->size - 1) {
11112e7a4641STrond Myklebust desc->dir_cookie = array->last_cookie;
1112c8f0523bSTrond Myklebust nfs_readdir_seek_next_array(array, desc);
1113c8f0523bSTrond Myklebust } else {
1114c8f0523bSTrond Myklebust desc->dir_cookie = array->array[i + 1].cookie;
1115c8f0523bSTrond Myklebust desc->last_cookie = array->array[0].cookie;
1116c8f0523bSTrond Myklebust }
111759e356a9STrond Myklebust if (nfs_readdir_use_cookie(file))
11182e7a4641STrond Myklebust desc->ctx->pos = desc->dir_cookie;
111959e356a9STrond Myklebust else
112059e356a9STrond Myklebust desc->ctx->pos++;
11218cd51a0cSTrond Myklebust }
1122ec108d3cSAnna Schumaker if (array->folio_is_eof)
1123e1d2699bSTrond Myklebust desc->eof = !desc->eob;
1124d1bacf9eSBryan Schumaker
1125c77c738cSFabio M. De Francesco kunmap_local(array);
1126dbeaf8c9STrond Myklebust dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1127dbeaf8c9STrond Myklebust (unsigned long long)desc->dir_cookie);
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds
11301da177e4SLinus Torvalds /*
11311da177e4SLinus Torvalds * If we cannot find a cookie in our cache, we suspect that this is
11321da177e4SLinus Torvalds * because it points to a deleted file, so we ask the server to return
11331da177e4SLinus Torvalds * whatever it thinks is the next entry. We then feed this to filldir.
11341da177e4SLinus Torvalds * If all goes well, we should then be able to find our way round the
11351da177e4SLinus Torvalds * cache on the next call to readdir_search_pagecache();
11361da177e4SLinus Torvalds *
11371da177e4SLinus Torvalds * NOTE: we cannot add the anonymous page to the pagecache because
11381da177e4SLinus Torvalds * the data it contains might not be page aligned. Besides,
11391da177e4SLinus Torvalds * we should already have a complete representation of the
11401da177e4SLinus Torvalds * directory in the page cache by the time we get here.
11411da177e4SLinus Torvalds */
uncached_readdir(struct nfs_readdir_descriptor * desc)11426c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
11431da177e4SLinus Torvalds {
114461f02e0aSAnna Schumaker struct folio **arrays;
114535df59d3STrond Myklebust size_t i, sz = 512;
1146b593c09fSTrond Myklebust __be32 verf[NFS_DIR_VERIFIER_SIZE];
114735df59d3STrond Myklebust int status = -ENOMEM;
11481da177e4SLinus Torvalds
114935df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
11502e7a4641STrond Myklebust (unsigned long long)desc->dir_cookie);
11511da177e4SLinus Torvalds
115235df59d3STrond Myklebust arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
115335df59d3STrond Myklebust if (!arrays)
11541da177e4SLinus Torvalds goto out;
1155ec108d3cSAnna Schumaker arrays[0] = nfs_readdir_folio_array_alloc(desc->dir_cookie, GFP_KERNEL);
115635df59d3STrond Myklebust if (!arrays[0])
115735df59d3STrond Myklebust goto out;
11581da177e4SLinus Torvalds
115961f02e0aSAnna Schumaker desc->folio_index = 0;
1160ce292d8fStrondmy@kernel.org desc->cache_entry_index = 0;
11612e7a4641STrond Myklebust desc->last_cookie = desc->dir_cookie;
116261f02e0aSAnna Schumaker desc->folio_index_max = 0;
11637a8e1dc3STrond Myklebust
1164310e3187STrond Myklebust trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie,
1165310e3187STrond Myklebust -1, desc->dtsize);
1166310e3187STrond Myklebust
116735df59d3STrond Myklebust status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1168310e3187STrond Myklebust if (status < 0) {
1169310e3187STrond Myklebust trace_nfs_readdir_uncached_done(file_inode(desc->file), status);
1170310e3187STrond Myklebust goto out_free;
1171310e3187STrond Myklebust }
1172d1bacf9eSBryan Schumaker
1173e1d2699bSTrond Myklebust for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
117461f02e0aSAnna Schumaker desc->folio = arrays[i];
117513884ff2STrond Myklebust nfs_do_filldir(desc, verf);
117635df59d3STrond Myklebust }
117761f02e0aSAnna Schumaker desc->folio = NULL;
11781da177e4SLinus Torvalds
1179580f2367STrond Myklebust /*
1180580f2367STrond Myklebust * Grow the dtsize if we have to go back for more pages,
1181580f2367STrond Myklebust * or shrink it if we're reading too many.
1182580f2367STrond Myklebust */
1183580f2367STrond Myklebust if (!desc->eof) {
1184580f2367STrond Myklebust if (!desc->eob)
1185580f2367STrond Myklebust nfs_grow_dtsize(desc);
1186580f2367STrond Myklebust else if (desc->buffer_fills == 1 &&
118761f02e0aSAnna Schumaker i < (desc->folio_index_max >> 1))
1188580f2367STrond Myklebust nfs_shrink_dtsize(desc);
1189580f2367STrond Myklebust }
1190310e3187STrond Myklebust out_free:
119135df59d3STrond Myklebust for (i = 0; i < sz && arrays[i]; i++)
1192ec108d3cSAnna Schumaker nfs_readdir_folio_array_free(arrays[i]);
11931da177e4SLinus Torvalds out:
1194f648022fSTrond Myklebust if (!nfs_readdir_use_cookie(desc->file))
1195f648022fSTrond Myklebust nfs_readdir_rewind_search(desc);
119661f02e0aSAnna Schumaker desc->folio_index_max = -1;
119735df59d3STrond Myklebust kfree(arrays);
119835df59d3STrond Myklebust dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
11991da177e4SLinus Torvalds return status;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds
nfs_readdir_handle_cache_misses(struct inode * inode,struct nfs_readdir_descriptor * desc,unsigned int cache_misses,bool force_clear)1202b0365ccbSTrond Myklebust static bool nfs_readdir_handle_cache_misses(struct inode *inode,
1203230bc98fSTrond Myklebust struct nfs_readdir_descriptor *desc,
1204b0365ccbSTrond Myklebust unsigned int cache_misses,
1205b0365ccbSTrond Myklebust bool force_clear)
1206230bc98fSTrond Myklebust {
1207b0365ccbSTrond Myklebust if (desc->ctx->pos == 0 || !desc->plus)
1208b0365ccbSTrond Myklebust return false;
1209b0365ccbSTrond Myklebust if (cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD && !force_clear)
1210b0365ccbSTrond Myklebust return false;
1211b0365ccbSTrond Myklebust trace_nfs_readdir_force_readdirplus(inode);
1212b0365ccbSTrond Myklebust return true;
1213230bc98fSTrond Myklebust }
1214230bc98fSTrond Myklebust
121500a92642SOlivier Galibert /* The file offset position represents the dirent entry number. A
121600a92642SOlivier Galibert last cookie cache takes care of the common case of reading the
121700a92642SOlivier Galibert whole directory.
12181da177e4SLinus Torvalds */
nfs_readdir(struct file * file,struct dir_context * ctx)121923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
12201da177e4SLinus Torvalds {
1221be62a1a8SMiklos Szeredi struct dentry *dentry = file_dentry(file);
12222b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
122313884ff2STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode);
122423db8620SAl Viro struct nfs_open_dir_context *dir_ctx = file->private_data;
12256b75cf9eSTrond Myklebust struct nfs_readdir_descriptor *desc;
1226230bc98fSTrond Myklebust unsigned int cache_hits, cache_misses;
1227b0365ccbSTrond Myklebust bool force_clear;
12286b75cf9eSTrond Myklebust int res;
12291da177e4SLinus Torvalds
12306de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
12316de1472fSAl Viro file, (long long)ctx->pos);
123291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
123391d5b470SChuck Lever
12341da177e4SLinus Torvalds /*
123523db8620SAl Viro * ctx->pos points to the dirent entry number.
1236f0dd2136STrond Myklebust * *desc->dir_cookie has the cookie for the next entry. We have
123700a92642SOlivier Galibert * to either find the entry with the appropriate number or
123800a92642SOlivier Galibert * revalidate the cookie.
12391da177e4SLinus Torvalds */
1240d09e673fSTrond Myklebust nfs_revalidate_mapping(inode, file->f_mapping);
12416b75cf9eSTrond Myklebust
12426b75cf9eSTrond Myklebust res = -ENOMEM;
12436b75cf9eSTrond Myklebust desc = kzalloc(sizeof(*desc), GFP_KERNEL);
12446b75cf9eSTrond Myklebust if (!desc)
12456b75cf9eSTrond Myklebust goto out;
12466b75cf9eSTrond Myklebust desc->file = file;
12476b75cf9eSTrond Myklebust desc->ctx = ctx;
124861f02e0aSAnna Schumaker desc->folio_index_max = -1;
1249fccca7fcSTrond Myklebust
12502e7a4641STrond Myklebust spin_lock(&file->f_lock);
12512e7a4641STrond Myklebust desc->dir_cookie = dir_ctx->dir_cookie;
125261f02e0aSAnna Schumaker desc->folio_index = dir_ctx->page_index;
1253728dd0abSTrond Myklebust desc->last_cookie = dir_ctx->last_cookie;
12542e7a4641STrond Myklebust desc->attr_gencount = dir_ctx->attr_gencount;
1255e1d2699bSTrond Myklebust desc->eof = dir_ctx->eof;
1256580f2367STrond Myklebust nfs_set_dtsize(desc, dir_ctx->dtsize);
1257b593c09fSTrond Myklebust memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
1258230bc98fSTrond Myklebust cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0);
1259230bc98fSTrond Myklebust cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0);
1260b0365ccbSTrond Myklebust force_clear = dir_ctx->force_clear;
12612e7a4641STrond Myklebust spin_unlock(&file->f_lock);
1262565277f6STrond Myklebust
1263e1d2699bSTrond Myklebust if (desc->eof) {
1264e1d2699bSTrond Myklebust res = 0;
1265e1d2699bSTrond Myklebust goto out_free;
1266e1d2699bSTrond Myklebust }
1267e1d2699bSTrond Myklebust
1268230bc98fSTrond Myklebust desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses);
1269b0365ccbSTrond Myklebust force_clear = nfs_readdir_handle_cache_misses(inode, desc, cache_misses,
1270b0365ccbSTrond Myklebust force_clear);
1271b0365ccbSTrond Myklebust desc->clear_cache = force_clear;
1272ff81dfb5STrond Myklebust
127347c716cbSTrond Myklebust do {
12741da177e4SLinus Torvalds res = readdir_search_pagecache(desc);
127500a92642SOlivier Galibert
12761da177e4SLinus Torvalds if (res == -EBADCOOKIE) {
1277ece0b423STrond Myklebust res = 0;
12781da177e4SLinus Torvalds /* This means either end of directory */
12792e7a4641STrond Myklebust if (desc->dir_cookie && !desc->eof) {
12801da177e4SLinus Torvalds /* Or that the server has 'lost' a cookie */
128123db8620SAl Viro res = uncached_readdir(desc);
1282ece0b423STrond Myklebust if (res == 0)
12831da177e4SLinus Torvalds continue;
12849fff59edSTrond Myklebust if (res == -EBADCOOKIE || res == -ENOTSYNC)
12859fff59edSTrond Myklebust res = 0;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds break;
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds if (res == -ETOOSMALL && desc->plus) {
12901da177e4SLinus Torvalds nfs_zap_caches(inode);
1291a7a3b1e9SBenjamin Coddington desc->plus = false;
1292a7a3b1e9SBenjamin Coddington desc->eof = false;
12931da177e4SLinus Torvalds continue;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds if (res < 0)
12961da177e4SLinus Torvalds break;
12971da177e4SLinus Torvalds
129813884ff2STrond Myklebust nfs_do_filldir(desc, nfsi->cookieverf);
129961f02e0aSAnna Schumaker nfs_readdir_folio_unlock_and_put_cached(desc);
130061f02e0aSAnna Schumaker if (desc->folio_index == desc->folio_index_max)
1301b0365ccbSTrond Myklebust desc->clear_cache = force_clear;
1302e1d2699bSTrond Myklebust } while (!desc->eob && !desc->eof);
13032e7a4641STrond Myklebust
13042e7a4641STrond Myklebust spin_lock(&file->f_lock);
13052e7a4641STrond Myklebust dir_ctx->dir_cookie = desc->dir_cookie;
1306728dd0abSTrond Myklebust dir_ctx->last_cookie = desc->last_cookie;
13072e7a4641STrond Myklebust dir_ctx->attr_gencount = desc->attr_gencount;
130861f02e0aSAnna Schumaker dir_ctx->page_index = desc->folio_index;
1309b0365ccbSTrond Myklebust dir_ctx->force_clear = force_clear;
1310e1d2699bSTrond Myklebust dir_ctx->eof = desc->eof;
1311580f2367STrond Myklebust dir_ctx->dtsize = desc->dtsize;
1312b593c09fSTrond Myklebust memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
13132e7a4641STrond Myklebust spin_unlock(&file->f_lock);
1314e1d2699bSTrond Myklebust out_free:
13156b75cf9eSTrond Myklebust kfree(desc);
13166b75cf9eSTrond Myklebust
1317fccca7fcSTrond Myklebust out:
13186de1472fSAl Viro dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
13191da177e4SLinus Torvalds return res;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds
nfs_llseek_dir(struct file * filp,loff_t offset,int whence)1322965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1323f0dd2136STrond Myklebust {
1324480c2006SBryan Schumaker struct nfs_open_dir_context *dir_ctx = filp->private_data;
1325b84e06c5SChuck Lever
13266de1472fSAl Viro dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
13276de1472fSAl Viro filp, offset, whence);
1328b84e06c5SChuck Lever
1329965c8e59SAndrew Morton switch (whence) {
1330f0dd2136STrond Myklebust default:
1331b2b1ff3dSTrond Myklebust return -EINVAL;
1332b2b1ff3dSTrond Myklebust case SEEK_SET:
1333b2b1ff3dSTrond Myklebust if (offset < 0)
1334b2b1ff3dSTrond Myklebust return -EINVAL;
133583f2c45eSTrond Myklebust spin_lock(&filp->f_lock);
1336b2b1ff3dSTrond Myklebust break;
1337b2b1ff3dSTrond Myklebust case SEEK_CUR:
1338b2b1ff3dSTrond Myklebust if (offset == 0)
1339b2b1ff3dSTrond Myklebust return filp->f_pos;
134083f2c45eSTrond Myklebust spin_lock(&filp->f_lock);
1341b2b1ff3dSTrond Myklebust offset += filp->f_pos;
1342b2b1ff3dSTrond Myklebust if (offset < 0) {
134383f2c45eSTrond Myklebust spin_unlock(&filp->f_lock);
1344b2b1ff3dSTrond Myklebust return -EINVAL;
1345b2b1ff3dSTrond Myklebust }
1346f0dd2136STrond Myklebust }
1347f0dd2136STrond Myklebust if (offset != filp->f_pos) {
1348f0dd2136STrond Myklebust filp->f_pos = offset;
1349728dd0abSTrond Myklebust dir_ctx->page_index = 0;
1350f648022fSTrond Myklebust if (!nfs_readdir_use_cookie(filp)) {
13519c3f4d98STrond Myklebust dir_ctx->dir_cookie = 0;
1352f648022fSTrond Myklebust dir_ctx->last_cookie = 0;
1353f648022fSTrond Myklebust } else {
1354728dd0abSTrond Myklebust dir_ctx->dir_cookie = offset;
1355f648022fSTrond Myklebust dir_ctx->last_cookie = offset;
1356f648022fSTrond Myklebust }
1357e1d2699bSTrond Myklebust dir_ctx->eof = false;
1358f0dd2136STrond Myklebust }
135983f2c45eSTrond Myklebust spin_unlock(&filp->f_lock);
1360f0dd2136STrond Myklebust return offset;
1361f0dd2136STrond Myklebust }
1362f0dd2136STrond Myklebust
13631da177e4SLinus Torvalds /*
13641da177e4SLinus Torvalds * All directory operations under NFS are synchronous, so fsync()
13651da177e4SLinus Torvalds * is a dummy operation.
13661da177e4SLinus Torvalds */
nfs_fsync_dir(struct file * filp,loff_t start,loff_t end,int datasync)136702c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
136802c24a82SJosef Bacik int datasync)
13691da177e4SLinus Torvalds {
13706de1472fSAl Viro dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
13711e7cb3dcSChuck Lever
137211decaf8STrond Myklebust nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
13731da177e4SLinus Torvalds return 0;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds
1376bfc69a45STrond Myklebust /**
1377bfc69a45STrond Myklebust * nfs_force_lookup_revalidate - Mark the directory as having changed
1378302fad7bSTrond Myklebust * @dir: pointer to directory inode
1379bfc69a45STrond Myklebust *
1380bfc69a45STrond Myklebust * This forces the revalidation code in nfs_lookup_revalidate() to do a
1381bfc69a45STrond Myklebust * full lookup on all child dentries of 'dir' whenever a change occurs
1382bfc69a45STrond Myklebust * on the server that might have invalidated our dcache.
1383bfc69a45STrond Myklebust *
1384efeda80dSTrond Myklebust * Note that we reserve bit '0' as a tag to let us know when a dentry
1385efeda80dSTrond Myklebust * was revalidated while holding a delegation on its inode.
1386efeda80dSTrond Myklebust *
1387bfc69a45STrond Myklebust * The caller should be holding dir->i_lock
1388bfc69a45STrond Myklebust */
nfs_force_lookup_revalidate(struct inode * dir)1389bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1390bfc69a45STrond Myklebust {
1391efeda80dSTrond Myklebust NFS_I(dir)->cache_change_attribute += 2;
1392bfc69a45STrond Myklebust }
139389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1394bfc69a45STrond Myklebust
1395efeda80dSTrond Myklebust /**
1396efeda80dSTrond Myklebust * nfs_verify_change_attribute - Detects NFS remote directory changes
1397efeda80dSTrond Myklebust * @dir: pointer to parent directory inode
1398efeda80dSTrond Myklebust * @verf: previously saved change attribute
1399efeda80dSTrond Myklebust *
1400efeda80dSTrond Myklebust * Return "false" if the verifiers doesn't match the change attribute.
1401efeda80dSTrond Myklebust * This would usually indicate that the directory contents have changed on
1402efeda80dSTrond Myklebust * the server, and that any dentries need revalidating.
1403efeda80dSTrond Myklebust */
nfs_verify_change_attribute(struct inode * dir,unsigned long verf)1404efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1405efeda80dSTrond Myklebust {
1406efeda80dSTrond Myklebust return (verf & ~1UL) == nfs_save_change_attribute(dir);
1407efeda80dSTrond Myklebust }
1408efeda80dSTrond Myklebust
nfs_set_verifier_delegated(unsigned long * verf)1409efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1410efeda80dSTrond Myklebust {
1411efeda80dSTrond Myklebust *verf |= 1UL;
1412efeda80dSTrond Myklebust }
1413efeda80dSTrond Myklebust
1414efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
nfs_unset_verifier_delegated(unsigned long * verf)1415efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1416efeda80dSTrond Myklebust {
1417efeda80dSTrond Myklebust *verf &= ~1UL;
1418efeda80dSTrond Myklebust }
1419efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1420efeda80dSTrond Myklebust
nfs_test_verifier_delegated(unsigned long verf)1421efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1422efeda80dSTrond Myklebust {
1423efeda80dSTrond Myklebust return verf & 1;
1424efeda80dSTrond Myklebust }
1425efeda80dSTrond Myklebust
nfs_verifier_is_delegated(struct dentry * dentry)1426efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1427efeda80dSTrond Myklebust {
1428efeda80dSTrond Myklebust return nfs_test_verifier_delegated(dentry->d_time);
1429efeda80dSTrond Myklebust }
1430efeda80dSTrond Myklebust
nfs_set_verifier_locked(struct dentry * dentry,unsigned long verf)1431efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1432efeda80dSTrond Myklebust {
1433efeda80dSTrond Myklebust struct inode *inode = d_inode(dentry);
1434cec08f45STrond Myklebust struct inode *dir = d_inode(dentry->d_parent);
1435efeda80dSTrond Myklebust
1436cec08f45STrond Myklebust if (!nfs_verify_change_attribute(dir, verf))
1437cec08f45STrond Myklebust return;
1438efeda80dSTrond Myklebust if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1439efeda80dSTrond Myklebust nfs_set_verifier_delegated(&verf);
1440efeda80dSTrond Myklebust dentry->d_time = verf;
1441efeda80dSTrond Myklebust }
1442efeda80dSTrond Myklebust
1443efeda80dSTrond Myklebust /**
1444efeda80dSTrond Myklebust * nfs_set_verifier - save a parent directory verifier in the dentry
1445efeda80dSTrond Myklebust * @dentry: pointer to dentry
1446efeda80dSTrond Myklebust * @verf: verifier to save
1447efeda80dSTrond Myklebust *
1448efeda80dSTrond Myklebust * Saves the parent directory verifier in @dentry. If the inode has
1449efeda80dSTrond Myklebust * a delegation, we also tag the dentry as having been revalidated
1450efeda80dSTrond Myklebust * while holding a delegation so that we know we don't have to
1451efeda80dSTrond Myklebust * look it up again after a directory change.
1452efeda80dSTrond Myklebust */
nfs_set_verifier(struct dentry * dentry,unsigned long verf)1453efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1454efeda80dSTrond Myklebust {
1455efeda80dSTrond Myklebust
1456efeda80dSTrond Myklebust spin_lock(&dentry->d_lock);
1457efeda80dSTrond Myklebust nfs_set_verifier_locked(dentry, verf);
1458efeda80dSTrond Myklebust spin_unlock(&dentry->d_lock);
1459efeda80dSTrond Myklebust }
1460efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1461efeda80dSTrond Myklebust
1462efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1463efeda80dSTrond Myklebust /**
1464efeda80dSTrond Myklebust * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1465efeda80dSTrond Myklebust * @inode: pointer to inode
1466efeda80dSTrond Myklebust *
1467efeda80dSTrond Myklebust * Iterates through the dentries in the inode alias list and clears
1468efeda80dSTrond Myklebust * the tag used to indicate that the dentry has been revalidated
1469efeda80dSTrond Myklebust * while holding a delegation.
1470efeda80dSTrond Myklebust * This function is intended for use when the delegation is being
1471efeda80dSTrond Myklebust * returned or revoked.
1472efeda80dSTrond Myklebust */
nfs_clear_verifier_delegated(struct inode * inode)1473efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1474efeda80dSTrond Myklebust {
1475efeda80dSTrond Myklebust struct dentry *alias;
1476efeda80dSTrond Myklebust
1477efeda80dSTrond Myklebust if (!inode)
1478efeda80dSTrond Myklebust return;
1479efeda80dSTrond Myklebust spin_lock(&inode->i_lock);
1480efeda80dSTrond Myklebust hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1481efeda80dSTrond Myklebust spin_lock(&alias->d_lock);
1482efeda80dSTrond Myklebust nfs_unset_verifier_delegated(&alias->d_time);
1483efeda80dSTrond Myklebust spin_unlock(&alias->d_lock);
1484efeda80dSTrond Myklebust }
1485efeda80dSTrond Myklebust spin_unlock(&inode->i_lock);
1486efeda80dSTrond Myklebust }
1487efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1488efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1489efeda80dSTrond Myklebust
nfs_dentry_verify_change(struct inode * dir,struct dentry * dentry)14908ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
14918ce37abdSTrond Myklebust {
14928ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
14938ce37abdSTrond Myklebust d_really_is_negative(dentry))
14948ce37abdSTrond Myklebust return dentry->d_time == inode_peek_iversion_raw(dir);
14958ce37abdSTrond Myklebust return nfs_verify_change_attribute(dir, dentry->d_time);
14968ce37abdSTrond Myklebust }
14978ce37abdSTrond Myklebust
14981da177e4SLinus Torvalds /*
14991da177e4SLinus Torvalds * A check for whether or not the parent directory has changed.
15001da177e4SLinus Torvalds * In the case it has, we assume that the dentries are untrustworthy
15011da177e4SLinus Torvalds * and may need to be looked up again.
1502912a108dSNeilBrown * If rcu_walk prevents us from performing a full check, return 0.
15031da177e4SLinus Torvalds */
nfs_check_verifier(struct inode * dir,struct dentry * dentry,int rcu_walk)1504912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1505912a108dSNeilBrown int rcu_walk)
15061da177e4SLinus Torvalds {
15071da177e4SLinus Torvalds if (IS_ROOT(dentry))
15081da177e4SLinus Torvalds return 1;
15094eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
15104eec952eSTrond Myklebust return 0;
15118ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry))
15126ecc5e8fSTrond Myklebust return 0;
1513f2c77f4eSTrond Myklebust /* Revalidate nfsi->cache_change_attribute before we declare a match */
15141cd9cb05STrond Myklebust if (nfs_mapping_need_revalidate_inode(dir)) {
1515912a108dSNeilBrown if (rcu_walk)
1516f2c77f4eSTrond Myklebust return 0;
15171cd9cb05STrond Myklebust if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
15181cd9cb05STrond Myklebust return 0;
15191cd9cb05STrond Myklebust }
15208ce37abdSTrond Myklebust if (!nfs_dentry_verify_change(dir, dentry))
1521f2c77f4eSTrond Myklebust return 0;
1522f2c77f4eSTrond Myklebust return 1;
15231da177e4SLinus Torvalds }
15241da177e4SLinus Torvalds
15251da177e4SLinus Torvalds /*
1526a12802caSTrond Myklebust * Use intent information to check whether or not we're going to do
1527a12802caSTrond Myklebust * an O_EXCL create using this path component.
1528a12802caSTrond Myklebust */
nfs_is_exclusive_create(struct inode * dir,unsigned int flags)1529fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1530a12802caSTrond Myklebust {
1531a12802caSTrond Myklebust if (NFS_PROTO(dir)->version == 2)
1532a12802caSTrond Myklebust return 0;
1533fa3c56bbSAl Viro return flags & LOOKUP_EXCL;
1534a12802caSTrond Myklebust }
1535a12802caSTrond Myklebust
1536a12802caSTrond Myklebust /*
15371d6757fbSTrond Myklebust * Inode and filehandle revalidation for lookups.
15381d6757fbSTrond Myklebust *
15391d6757fbSTrond Myklebust * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
15401d6757fbSTrond Myklebust * or if the intent information indicates that we're about to open this
15411d6757fbSTrond Myklebust * particular file and the "nocto" mount flag is not set.
15421d6757fbSTrond Myklebust *
15431d6757fbSTrond Myklebust */
154465a0c149STrond Myklebust static
nfs_lookup_verify_inode(struct inode * inode,unsigned int flags)1545fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode);
154865a0c149STrond Myklebust int ret;
15491da177e4SLinus Torvalds
155036d43a43SDavid Howells if (IS_AUTOMOUNT(inode))
15514e99a1ffSTrond Myklebust return 0;
155247921921STrond Myklebust
155347921921STrond Myklebust if (flags & LOOKUP_OPEN) {
155447921921STrond Myklebust switch (inode->i_mode & S_IFMT) {
155547921921STrond Myklebust case S_IFREG:
155647921921STrond Myklebust /* A NFSv4 OPEN will revalidate later */
155747921921STrond Myklebust if (server->caps & NFS_CAP_ATOMIC_OPEN)
155847921921STrond Myklebust goto out;
1559df561f66SGustavo A. R. Silva fallthrough;
156047921921STrond Myklebust case S_IFDIR:
156147921921STrond Myklebust if (server->flags & NFS_MOUNT_NOCTO)
156247921921STrond Myklebust break;
156347921921STrond Myklebust /* NFS close-to-open cache consistency validation */
156447921921STrond Myklebust goto out_force;
156547921921STrond Myklebust }
156647921921STrond Myklebust }
156747921921STrond Myklebust
15681da177e4SLinus Torvalds /* VFS wants an on-the-wire revalidation */
1569fa3c56bbSAl Viro if (flags & LOOKUP_REVAL)
15701da177e4SLinus Torvalds goto out_force;
157165a0c149STrond Myklebust out:
157243245ecaSOlga Kornievskaia if (inode->i_nlink > 0 ||
157343245ecaSOlga Kornievskaia (inode->i_nlink == 0 &&
157443245ecaSOlga Kornievskaia test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
157543245ecaSOlga Kornievskaia return 0;
157643245ecaSOlga Kornievskaia else
157743245ecaSOlga Kornievskaia return -ESTALE;
15781da177e4SLinus Torvalds out_force:
15791fa1e384SNeilBrown if (flags & LOOKUP_RCU)
15801fa1e384SNeilBrown return -ECHILD;
158165a0c149STrond Myklebust ret = __nfs_revalidate_inode(server, inode);
158265a0c149STrond Myklebust if (ret != 0)
158365a0c149STrond Myklebust return ret;
158465a0c149STrond Myklebust goto out;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds
nfs_mark_dir_for_revalidate(struct inode * inode)158782e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode)
158882e7ca13STrond Myklebust {
158982e7ca13STrond Myklebust spin_lock(&inode->i_lock);
1590a6a361c4STrond Myklebust nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
159182e7ca13STrond Myklebust spin_unlock(&inode->i_lock);
159282e7ca13STrond Myklebust }
159382e7ca13STrond Myklebust
15941da177e4SLinus Torvalds /*
15951da177e4SLinus Torvalds * We judge how long we want to trust negative
15961da177e4SLinus Torvalds * dentries by looking at the parent inode mtime.
15971da177e4SLinus Torvalds *
15981da177e4SLinus Torvalds * If parent mtime has changed, we revalidate, else we wait for a
15991da177e4SLinus Torvalds * period corresponding to the parent's attribute cache timeout value.
1600912a108dSNeilBrown *
1601912a108dSNeilBrown * If LOOKUP_RCU prevents us from performing a full check, return 1
1602912a108dSNeilBrown * suggesting a reval is needed.
16039f6d44d4STrond Myklebust *
16049f6d44d4STrond Myklebust * Note that when creating a new file, or looking up a rename target,
16059f6d44d4STrond Myklebust * then it shouldn't be necessary to revalidate a negative dentry.
16061da177e4SLinus Torvalds */
16071da177e4SLinus Torvalds static inline
nfs_neg_need_reval(struct inode * dir,struct dentry * dentry,unsigned int flags)16081da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1609fa3c56bbSAl Viro unsigned int flags)
16101da177e4SLinus Torvalds {
16119f6d44d4STrond Myklebust if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
16121da177e4SLinus Torvalds return 0;
16134eec952eSTrond Myklebust if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
16144eec952eSTrond Myklebust return 1;
161598ca3ee6STrond Myklebust /* Case insensitive server? Revalidate negative dentries */
161698ca3ee6STrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
161798ca3ee6STrond Myklebust return 1;
1618912a108dSNeilBrown return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds
16215ceb9d7fSTrond Myklebust static int
nfs_lookup_revalidate_done(struct inode * dir,struct dentry * dentry,struct inode * inode,int error)16225ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
16235ceb9d7fSTrond Myklebust struct inode *inode, int error)
16241da177e4SLinus Torvalds {
16255ceb9d7fSTrond Myklebust switch (error) {
16265ceb9d7fSTrond Myklebust case 1:
16272eef8a31STrond Myklebust break;
1628*3146a6afSScott Mayhew case -ETIMEDOUT:
1629*3146a6afSScott Mayhew if (inode && (IS_ROOT(dentry) ||
1630*3146a6afSScott Mayhew NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL))
1631*3146a6afSScott Mayhew error = 1;
1632*3146a6afSScott Mayhew break;
1633*3146a6afSScott Mayhew case -ESTALE:
1634*3146a6afSScott Mayhew case -ENOENT:
1635*3146a6afSScott Mayhew error = 0;
1636*3146a6afSScott Mayhew fallthrough;
1637*3146a6afSScott Mayhew default:
1638a3f432bfSJ. Bruce Fields /*
1639a3f432bfSJ. Bruce Fields * We can't d_drop the root of a disconnected tree:
1640a3f432bfSJ. Bruce Fields * its d_hash is on the s_anon list and d_drop() would hide
1641a3f432bfSJ. Bruce Fields * it from shrink_dcache_for_unmount(), leading to busy
1642a3f432bfSJ. Bruce Fields * inodes on unmount and further oopses.
1643a3f432bfSJ. Bruce Fields */
164447397915STrond Myklebust if (inode && IS_ROOT(dentry))
16452eef8a31STrond Myklebust error = 1;
16462eef8a31STrond Myklebust break;
16475ceb9d7fSTrond Myklebust }
16482eef8a31STrond Myklebust trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1649e1fb4d05STrond Myklebust return error;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds
16525ceb9d7fSTrond Myklebust static int
nfs_lookup_revalidate_negative(struct inode * dir,struct dentry * dentry,unsigned int flags)16535ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
16545ceb9d7fSTrond Myklebust unsigned int flags)
16555ceb9d7fSTrond Myklebust {
16565ceb9d7fSTrond Myklebust int ret = 1;
16575ceb9d7fSTrond Myklebust if (nfs_neg_need_reval(dir, dentry, flags)) {
16585ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU)
16595ceb9d7fSTrond Myklebust return -ECHILD;
16605ceb9d7fSTrond Myklebust ret = 0;
16615ceb9d7fSTrond Myklebust }
16625ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
16635ceb9d7fSTrond Myklebust }
16645ceb9d7fSTrond Myklebust
16655ceb9d7fSTrond Myklebust static int
nfs_lookup_revalidate_delegated(struct inode * dir,struct dentry * dentry,struct inode * inode)16665ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
16675ceb9d7fSTrond Myklebust struct inode *inode)
16685ceb9d7fSTrond Myklebust {
16695ceb9d7fSTrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16705ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
16715ceb9d7fSTrond Myklebust }
16725ceb9d7fSTrond Myklebust
nfs_lookup_revalidate_dentry(struct inode * dir,struct dentry * dentry,struct inode * inode,unsigned int flags)16730b3cc71bSTrond Myklebust static int nfs_lookup_revalidate_dentry(struct inode *dir,
16740b3cc71bSTrond Myklebust struct dentry *dentry,
16750b3cc71bSTrond Myklebust struct inode *inode, unsigned int flags)
16765ceb9d7fSTrond Myklebust {
16775ceb9d7fSTrond Myklebust struct nfs_fh *fhandle;
16785ceb9d7fSTrond Myklebust struct nfs_fattr *fattr;
1679a1147b82STrond Myklebust unsigned long dir_verifier;
16805ceb9d7fSTrond Myklebust int ret;
16815ceb9d7fSTrond Myklebust
16820b3cc71bSTrond Myklebust trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
16830b3cc71bSTrond Myklebust
16845ceb9d7fSTrond Myklebust ret = -ENOMEM;
16855ceb9d7fSTrond Myklebust fhandle = nfs_alloc_fhandle();
16869558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
16879558a007SAnna Schumaker if (fhandle == NULL || fattr == NULL)
16885ceb9d7fSTrond Myklebust goto out;
16895ceb9d7fSTrond Myklebust
1690a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir);
16919558a007SAnna Schumaker ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1692*3146a6afSScott Mayhew if (ret < 0)
16935ceb9d7fSTrond Myklebust goto out;
16940b3cc71bSTrond Myklebust
16950b3cc71bSTrond Myklebust /* Request help from readdirplus */
16960b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags);
16970b3cc71bSTrond Myklebust
16985ceb9d7fSTrond Myklebust ret = 0;
16995ceb9d7fSTrond Myklebust if (nfs_compare_fh(NFS_FH(inode), fhandle))
17005ceb9d7fSTrond Myklebust goto out;
17015ceb9d7fSTrond Myklebust if (nfs_refresh_inode(inode, fattr) < 0)
17025ceb9d7fSTrond Myklebust goto out;
17035ceb9d7fSTrond Myklebust
1704dd225cb3SAnna Schumaker nfs_setsecurity(inode, fattr);
1705a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier);
17065ceb9d7fSTrond Myklebust
17075ceb9d7fSTrond Myklebust ret = 1;
17085ceb9d7fSTrond Myklebust out:
17095ceb9d7fSTrond Myklebust nfs_free_fattr(fattr);
17105ceb9d7fSTrond Myklebust nfs_free_fhandle(fhandle);
171182e7ca13STrond Myklebust
171282e7ca13STrond Myklebust /*
171382e7ca13STrond Myklebust * If the lookup failed despite the dentry change attribute being
171482e7ca13STrond Myklebust * a match, then we should revalidate the directory cache.
171582e7ca13STrond Myklebust */
17168ce37abdSTrond Myklebust if (!ret && nfs_dentry_verify_change(dir, dentry))
171782e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir);
17185ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
17195ceb9d7fSTrond Myklebust }
17205ceb9d7fSTrond Myklebust
17215ceb9d7fSTrond Myklebust /*
17225ceb9d7fSTrond Myklebust * This is called every time the dcache has a lookup hit,
17235ceb9d7fSTrond Myklebust * and we should check whether we can really trust that
17245ceb9d7fSTrond Myklebust * lookup.
17255ceb9d7fSTrond Myklebust *
17265ceb9d7fSTrond Myklebust * NOTE! The hit can be a negative hit too, don't assume
17275ceb9d7fSTrond Myklebust * we have an inode!
17285ceb9d7fSTrond Myklebust *
17295ceb9d7fSTrond Myklebust * If the parent directory is seen to have changed, we throw out the
17305ceb9d7fSTrond Myklebust * cached dentry and do a new lookup.
17315ceb9d7fSTrond Myklebust */
17325ceb9d7fSTrond Myklebust static int
nfs_do_lookup_revalidate(struct inode * dir,struct dentry * dentry,unsigned int flags)17335ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
17345ceb9d7fSTrond Myklebust unsigned int flags)
17355ceb9d7fSTrond Myklebust {
17365ceb9d7fSTrond Myklebust struct inode *inode;
1737*3146a6afSScott Mayhew int error = 0;
17385ceb9d7fSTrond Myklebust
17395ceb9d7fSTrond Myklebust nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
17405ceb9d7fSTrond Myklebust inode = d_inode(dentry);
17415ceb9d7fSTrond Myklebust
17425ceb9d7fSTrond Myklebust if (!inode)
17435ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_negative(dir, dentry, flags);
17445ceb9d7fSTrond Myklebust
17455ceb9d7fSTrond Myklebust if (is_bad_inode(inode)) {
17465ceb9d7fSTrond Myklebust dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
17475ceb9d7fSTrond Myklebust __func__, dentry);
17485ceb9d7fSTrond Myklebust goto out_bad;
17495ceb9d7fSTrond Myklebust }
17505ceb9d7fSTrond Myklebust
17516ca0a6f8STrond Myklebust if ((flags & LOOKUP_RENAME_TARGET) && d_count(dentry) < 2 &&
17526ca0a6f8STrond Myklebust nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
17536ca0a6f8STrond Myklebust goto out_bad;
17546ca0a6f8STrond Myklebust
1755efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry))
17565ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode);
17575ceb9d7fSTrond Myklebust
17585ceb9d7fSTrond Myklebust /* Force a full look up iff the parent directory has changed */
17595ceb9d7fSTrond Myklebust if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
17605ceb9d7fSTrond Myklebust nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
17615ceb9d7fSTrond Myklebust error = nfs_lookup_verify_inode(inode, flags);
17625ceb9d7fSTrond Myklebust if (error) {
17635ceb9d7fSTrond Myklebust if (error == -ESTALE)
176482e7ca13STrond Myklebust nfs_mark_dir_for_revalidate(dir);
17655ceb9d7fSTrond Myklebust goto out_bad;
17665ceb9d7fSTrond Myklebust }
17675ceb9d7fSTrond Myklebust goto out_valid;
17685ceb9d7fSTrond Myklebust }
17695ceb9d7fSTrond Myklebust
17705ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU)
17715ceb9d7fSTrond Myklebust return -ECHILD;
17725ceb9d7fSTrond Myklebust
17735ceb9d7fSTrond Myklebust if (NFS_STALE(inode))
17745ceb9d7fSTrond Myklebust goto out_bad;
17755ceb9d7fSTrond Myklebust
17760b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
17775ceb9d7fSTrond Myklebust out_valid:
17785ceb9d7fSTrond Myklebust return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
17795ceb9d7fSTrond Myklebust out_bad:
17805ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU)
17815ceb9d7fSTrond Myklebust return -ECHILD;
1782*3146a6afSScott Mayhew return nfs_lookup_revalidate_done(dir, dentry, inode, error);
17835ceb9d7fSTrond Myklebust }
17845ceb9d7fSTrond Myklebust
17855ceb9d7fSTrond Myklebust static int
__nfs_lookup_revalidate(struct dentry * dentry,unsigned int flags,int (* reval)(struct inode *,struct dentry *,unsigned int))1786c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1787c7944ebbSTrond Myklebust int (*reval)(struct inode *, struct dentry *, unsigned int))
17885ceb9d7fSTrond Myklebust {
17895ceb9d7fSTrond Myklebust struct dentry *parent;
17905ceb9d7fSTrond Myklebust struct inode *dir;
17915ceb9d7fSTrond Myklebust int ret;
17925ceb9d7fSTrond Myklebust
17935ceb9d7fSTrond Myklebust if (flags & LOOKUP_RCU) {
17943c59366cSNeilBrown if (dentry->d_fsdata == NFS_FSDATA_BLOCKED)
17953c59366cSNeilBrown return -ECHILD;
17965ceb9d7fSTrond Myklebust parent = READ_ONCE(dentry->d_parent);
17975ceb9d7fSTrond Myklebust dir = d_inode_rcu(parent);
17985ceb9d7fSTrond Myklebust if (!dir)
17995ceb9d7fSTrond Myklebust return -ECHILD;
1800c7944ebbSTrond Myklebust ret = reval(dir, dentry, flags);
18015ceb9d7fSTrond Myklebust if (parent != READ_ONCE(dentry->d_parent))
18025ceb9d7fSTrond Myklebust return -ECHILD;
18035ceb9d7fSTrond Myklebust } else {
1804b21cae46SNeilBrown /* Wait for unlink to complete - see unblock_revalidate() */
18053c59366cSNeilBrown wait_var_event(&dentry->d_fsdata,
1806b21cae46SNeilBrown smp_load_acquire(&dentry->d_fsdata)
1807b21cae46SNeilBrown != NFS_FSDATA_BLOCKED);
18085ceb9d7fSTrond Myklebust parent = dget_parent(dentry);
1809c7944ebbSTrond Myklebust ret = reval(d_inode(parent), dentry, flags);
18105ceb9d7fSTrond Myklebust dput(parent);
18115ceb9d7fSTrond Myklebust }
18125ceb9d7fSTrond Myklebust return ret;
18135ceb9d7fSTrond Myklebust }
18145ceb9d7fSTrond Myklebust
nfs_lookup_revalidate(struct dentry * dentry,unsigned int flags)1815c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1816c7944ebbSTrond Myklebust {
1817c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1818c7944ebbSTrond Myklebust }
1819c7944ebbSTrond Myklebust
block_revalidate(struct dentry * dentry)1820b21cae46SNeilBrown static void block_revalidate(struct dentry *dentry)
1821b21cae46SNeilBrown {
1822b21cae46SNeilBrown /* old devname - just in case */
1823b21cae46SNeilBrown kfree(dentry->d_fsdata);
1824b21cae46SNeilBrown
1825b21cae46SNeilBrown /* Any new reference that could lead to an open
1826b21cae46SNeilBrown * will take ->d_lock in lookup_open() -> d_lookup().
1827b21cae46SNeilBrown * Holding this lock ensures we cannot race with
1828b21cae46SNeilBrown * __nfs_lookup_revalidate() and removes and need
1829b21cae46SNeilBrown * for further barriers.
1830b21cae46SNeilBrown */
1831b21cae46SNeilBrown lockdep_assert_held(&dentry->d_lock);
1832b21cae46SNeilBrown
1833b21cae46SNeilBrown dentry->d_fsdata = NFS_FSDATA_BLOCKED;
1834b21cae46SNeilBrown }
1835b21cae46SNeilBrown
unblock_revalidate(struct dentry * dentry)1836b21cae46SNeilBrown static void unblock_revalidate(struct dentry *dentry)
1837b21cae46SNeilBrown {
1838b21cae46SNeilBrown /* store_release ensures wait_var_event() sees the update */
1839b21cae46SNeilBrown smp_store_release(&dentry->d_fsdata, NULL);
1840b21cae46SNeilBrown wake_up_var(&dentry->d_fsdata);
1841b21cae46SNeilBrown }
1842b21cae46SNeilBrown
18431da177e4SLinus Torvalds /*
18442b0143b5SDavid Howells * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1845ecf3d1f1SJeff Layton * when we don't really care about the dentry name. This is called when a
1846ecf3d1f1SJeff Layton * pathwalk ends on a dentry that was not found via a normal lookup in the
1847ecf3d1f1SJeff Layton * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1848ecf3d1f1SJeff Layton *
1849ecf3d1f1SJeff Layton * In this situation, we just want to verify that the inode itself is OK
1850ecf3d1f1SJeff Layton * since the dentry might have changed on the server.
1851ecf3d1f1SJeff Layton */
nfs_weak_revalidate(struct dentry * dentry,unsigned int flags)1852ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1853ecf3d1f1SJeff Layton {
18542b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
18559cdd1d3fSTrond Myklebust int error = 0;
1856ecf3d1f1SJeff Layton
1857ecf3d1f1SJeff Layton /*
1858ecf3d1f1SJeff Layton * I believe we can only get a negative dentry here in the case of a
1859ecf3d1f1SJeff Layton * procfs-style symlink. Just assume it's correct for now, but we may
1860ecf3d1f1SJeff Layton * eventually need to do something more here.
1861ecf3d1f1SJeff Layton */
1862ecf3d1f1SJeff Layton if (!inode) {
18636de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
18646de1472fSAl Viro __func__, dentry);
1865ecf3d1f1SJeff Layton return 1;
1866ecf3d1f1SJeff Layton }
1867ecf3d1f1SJeff Layton
1868ecf3d1f1SJeff Layton if (is_bad_inode(inode)) {
18696de1472fSAl Viro dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
18706de1472fSAl Viro __func__, dentry);
1871ecf3d1f1SJeff Layton return 0;
1872ecf3d1f1SJeff Layton }
1873ecf3d1f1SJeff Layton
1874b688741cSNeilBrown error = nfs_lookup_verify_inode(inode, flags);
1875ecf3d1f1SJeff Layton dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1876ecf3d1f1SJeff Layton __func__, inode->i_ino, error ? "invalid" : "valid");
1877ecf3d1f1SJeff Layton return !error;
1878ecf3d1f1SJeff Layton }
1879ecf3d1f1SJeff Layton
1880ecf3d1f1SJeff Layton /*
18811da177e4SLinus Torvalds * This is called from dput() when d_count is going to 0.
18821da177e4SLinus Torvalds */
nfs_dentry_delete(const struct dentry * dentry)1883fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
18841da177e4SLinus Torvalds {
18856de1472fSAl Viro dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
18866de1472fSAl Viro dentry, dentry->d_flags);
18871da177e4SLinus Torvalds
188877f11192STrond Myklebust /* Unhash any dentry with a stale inode */
18892b0143b5SDavid Howells if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
189077f11192STrond Myklebust return 1;
189177f11192STrond Myklebust
18921da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
18931da177e4SLinus Torvalds /* Unhash it, so that ->d_iput() would be called */
18941da177e4SLinus Torvalds return 1;
18951da177e4SLinus Torvalds }
18961751e8a6SLinus Torvalds if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
18971da177e4SLinus Torvalds /* Unhash it, so that ancestors of killed async unlink
18981da177e4SLinus Torvalds * files will be cleaned up during umount */
18991da177e4SLinus Torvalds return 1;
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds return 0;
19021da177e4SLinus Torvalds
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds
19051f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
nfs_drop_nlink(struct inode * inode)19061b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
19071b83d707STrond Myklebust {
19081b83d707STrond Myklebust spin_lock(&inode->i_lock);
19091f018458STrond Myklebust /* drop the inode if we're reasonably sure this is the last link */
191059a707b0STrond Myklebust if (inode->i_nlink > 0)
191159a707b0STrond Myklebust drop_nlink(inode);
191259a707b0STrond Myklebust NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1913ac46b3d7STrond Myklebust nfs_set_cache_invalid(
1914ac46b3d7STrond Myklebust inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
19151301e421STrond Myklebust NFS_INO_INVALID_NLINK);
19161b83d707STrond Myklebust spin_unlock(&inode->i_lock);
19171b83d707STrond Myklebust }
19181b83d707STrond Myklebust
19191da177e4SLinus Torvalds /*
19201da177e4SLinus Torvalds * Called when the dentry loses inode.
19211da177e4SLinus Torvalds * We use it to clean up silly-renamed files.
19221da177e4SLinus Torvalds */
nfs_dentry_iput(struct dentry * dentry,struct inode * inode)19231da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
19241da177e4SLinus Torvalds {
19251da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1926e4eff1a6STrond Myklebust nfs_complete_unlink(dentry, inode);
19271f018458STrond Myklebust nfs_drop_nlink(inode);
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds iput(inode);
19301da177e4SLinus Torvalds }
19311da177e4SLinus Torvalds
nfs_d_release(struct dentry * dentry)1932b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1933b1942c5fSAl Viro {
1934b1942c5fSAl Viro /* free cached devname value, if it survived that far */
1935b1942c5fSAl Viro if (unlikely(dentry->d_fsdata)) {
1936b1942c5fSAl Viro if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1937b1942c5fSAl Viro WARN_ON(1);
1938b1942c5fSAl Viro else
1939b1942c5fSAl Viro kfree(dentry->d_fsdata);
1940b1942c5fSAl Viro }
1941b1942c5fSAl Viro }
1942b1942c5fSAl Viro
1943f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
19441da177e4SLinus Torvalds .d_revalidate = nfs_lookup_revalidate,
1945ecf3d1f1SJeff Layton .d_weak_revalidate = nfs_weak_revalidate,
19461da177e4SLinus Torvalds .d_delete = nfs_dentry_delete,
19471da177e4SLinus Torvalds .d_iput = nfs_dentry_iput,
194836d43a43SDavid Howells .d_automount = nfs_d_automount,
1949b1942c5fSAl Viro .d_release = nfs_d_release,
19501da177e4SLinus Torvalds };
1951ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
19521da177e4SLinus Torvalds
nfs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1953597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds struct dentry *res;
19561da177e4SLinus Torvalds struct inode *inode = NULL;
1957e1fb4d05STrond Myklebust struct nfs_fh *fhandle = NULL;
1958e1fb4d05STrond Myklebust struct nfs_fattr *fattr = NULL;
1959a1147b82STrond Myklebust unsigned long dir_verifier;
19601da177e4SLinus Torvalds int error;
19611da177e4SLinus Torvalds
19626de1472fSAl Viro dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
196391d5b470SChuck Lever nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
19641da177e4SLinus Torvalds
1965130f9ab7SAl Viro if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1966130f9ab7SAl Viro return ERR_PTR(-ENAMETOOLONG);
19671da177e4SLinus Torvalds
1968fd684071STrond Myklebust /*
1969fd684071STrond Myklebust * If we're doing an exclusive create, optimize away the lookup
1970fd684071STrond Myklebust * but don't hash the dentry.
1971fd684071STrond Myklebust */
19729f6d44d4STrond Myklebust if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1973130f9ab7SAl Viro return NULL;
19741da177e4SLinus Torvalds
1975e1fb4d05STrond Myklebust res = ERR_PTR(-ENOMEM);
1976e1fb4d05STrond Myklebust fhandle = nfs_alloc_fhandle();
19779558a007SAnna Schumaker fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1978e1fb4d05STrond Myklebust if (fhandle == NULL || fattr == NULL)
1979e1fb4d05STrond Myklebust goto out;
1980e1fb4d05STrond Myklebust
1981a1147b82STrond Myklebust dir_verifier = nfs_save_change_attribute(dir);
19826e0d0be7STrond Myklebust trace_nfs_lookup_enter(dir, dentry, flags);
19839558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
19848ce37abdSTrond Myklebust if (error == -ENOENT) {
19858ce37abdSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
19868ce37abdSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir);
19871da177e4SLinus Torvalds goto no_entry;
19888ce37abdSTrond Myklebust }
19891da177e4SLinus Torvalds if (error < 0) {
19901da177e4SLinus Torvalds res = ERR_PTR(error);
19919558a007SAnna Schumaker goto out;
19921da177e4SLinus Torvalds }
1993cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1994bf0c84f1SNamhyung Kim res = ERR_CAST(inode);
199503f28e3aSTrond Myklebust if (IS_ERR(res))
19969558a007SAnna Schumaker goto out;
199754ceac45SDavid Howells
199863519fbcSTrond Myklebust /* Notify readdir to use READDIRPLUS */
19990b3cc71bSTrond Myklebust nfs_lookup_advise_force_readdirplus(dir, flags);
2000d69ee9b8STrond Myklebust
20011da177e4SLinus Torvalds no_entry:
200241d28bcaSAl Viro res = d_splice_alias(inode, dentry);
20039eaef27bSTrond Myklebust if (res != NULL) {
20049eaef27bSTrond Myklebust if (IS_ERR(res))
20059558a007SAnna Schumaker goto out;
20061da177e4SLinus Torvalds dentry = res;
20079eaef27bSTrond Myklebust }
2008a1147b82STrond Myklebust nfs_set_verifier(dentry, dir_verifier);
20091da177e4SLinus Torvalds out:
20109558a007SAnna Schumaker trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
2011e1fb4d05STrond Myklebust nfs_free_fattr(fattr);
2012e1fb4d05STrond Myklebust nfs_free_fhandle(fhandle);
20131da177e4SLinus Torvalds return res;
20141da177e4SLinus Torvalds }
2015ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
20161da177e4SLinus Torvalds
nfs_d_prune_case_insensitive_aliases(struct inode * inode)201700bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
201800bdadc7STrond Myklebust {
201900bdadc7STrond Myklebust /* Case insensitive server? Revalidate dentries */
202000bdadc7STrond Myklebust if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
202100bdadc7STrond Myklebust d_prune_aliases(inode);
202200bdadc7STrond Myklebust }
202300bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
202400bdadc7STrond Myklebust
202589d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
20260b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
20271da177e4SLinus Torvalds
2028f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
20290ef97dcfSMiklos Szeredi .d_revalidate = nfs4_lookup_revalidate,
2030b688741cSNeilBrown .d_weak_revalidate = nfs_weak_revalidate,
20311da177e4SLinus Torvalds .d_delete = nfs_dentry_delete,
20321da177e4SLinus Torvalds .d_iput = nfs_dentry_iput,
203336d43a43SDavid Howells .d_automount = nfs_d_automount,
2034b1942c5fSAl Viro .d_release = nfs_d_release,
20351da177e4SLinus Torvalds };
203689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
20371da177e4SLinus Torvalds
create_nfs_open_context(struct dentry * dentry,int open_flags,struct file * filp)2038532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
2039cd9a1c0eSTrond Myklebust {
2040532d4defSNeilBrown return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
2041cd9a1c0eSTrond Myklebust }
2042cd9a1c0eSTrond Myklebust
do_open(struct inode * inode,struct file * filp)2043cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
2044cd9a1c0eSTrond Myklebust {
2045f1fe29b4SDavid Howells nfs_fscache_open_file(inode, filp);
2046cd9a1c0eSTrond Myklebust return 0;
2047cd9a1c0eSTrond Myklebust }
2048cd9a1c0eSTrond Myklebust
nfs_finish_open(struct nfs_open_context * ctx,struct dentry * dentry,struct file * file,unsigned open_flags)2049d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
20500dd2b474SMiklos Szeredi struct dentry *dentry,
2051b452a458SAl Viro struct file *file, unsigned open_flags)
2052cd9a1c0eSTrond Myklebust {
20530dd2b474SMiklos Szeredi int err;
20540dd2b474SMiklos Szeredi
2055be12af3eSAl Viro err = finish_open(file, dentry, do_open);
205630d90494SAl Viro if (err)
2057d9585277SAl Viro goto out;
20581f24cd31SAl Viro if (S_ISREG(file_inode(file)->i_mode))
205930d90494SAl Viro nfs_file_set_open_context(file, ctx);
2060eaa2b82cSNeilBrown else
20619821421aSTrond Myklebust err = -EOPENSTALE;
2062cd9a1c0eSTrond Myklebust out:
2063d9585277SAl Viro return err;
2064cd9a1c0eSTrond Myklebust }
2065cd9a1c0eSTrond Myklebust
nfs_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned open_flags,umode_t mode)206673a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
206730d90494SAl Viro struct file *file, unsigned open_flags,
206844907d79SAl Viro umode_t mode)
20691da177e4SLinus Torvalds {
2070c94c0953SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2071cd9a1c0eSTrond Myklebust struct nfs_open_context *ctx;
20720dd2b474SMiklos Szeredi struct dentry *res;
20730dd2b474SMiklos Szeredi struct iattr attr = { .ia_valid = ATTR_OPEN };
2074f46e0bd3STrond Myklebust struct inode *inode;
20751472b83eSTrond Myklebust unsigned int lookup_flags = 0;
207668eaba4cSTrond Myklebust unsigned long dir_verifier;
2077c94c0953SAl Viro bool switched = false;
207873a09dd9SAl Viro int created = 0;
2079898f635cSTrond Myklebust int err;
20801da177e4SLinus Torvalds
20810dd2b474SMiklos Szeredi /* Expect a negative dentry */
20822b0143b5SDavid Howells BUG_ON(d_inode(dentry));
20830dd2b474SMiklos Szeredi
20841e8968c5SNiels de Vos dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
20856de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry);
20861e7cb3dcSChuck Lever
20879597c13bSJeff Layton err = nfs_check_flags(open_flags);
20889597c13bSJeff Layton if (err)
20899597c13bSJeff Layton return err;
20909597c13bSJeff Layton
20910dd2b474SMiklos Szeredi /* NFS only supports OPEN on regular files */
20920dd2b474SMiklos Szeredi if ((open_flags & O_DIRECTORY)) {
209300699ad8SAl Viro if (!d_in_lookup(dentry)) {
20940dd2b474SMiklos Szeredi /*
20950dd2b474SMiklos Szeredi * Hashed negative dentry with O_DIRECTORY: dentry was
20960dd2b474SMiklos Szeredi * revalidated and is fine, no need to perform lookup
20970dd2b474SMiklos Szeredi * again
20980dd2b474SMiklos Szeredi */
2099d9585277SAl Viro return -ENOENT;
21000dd2b474SMiklos Szeredi }
21011472b83eSTrond Myklebust lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
21021da177e4SLinus Torvalds goto no_open;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds
21050dd2b474SMiklos Szeredi if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
2106d9585277SAl Viro return -ENAMETOOLONG;
21071da177e4SLinus Torvalds
21080dd2b474SMiklos Szeredi if (open_flags & O_CREAT) {
2109dff25ddbSAndreas Gruenbacher struct nfs_server *server = NFS_SERVER(dir);
2110dff25ddbSAndreas Gruenbacher
2111dff25ddbSAndreas Gruenbacher if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
2112dff25ddbSAndreas Gruenbacher mode &= ~current_umask();
2113dff25ddbSAndreas Gruenbacher
2114536e43d1STrond Myklebust attr.ia_valid |= ATTR_MODE;
2115dff25ddbSAndreas Gruenbacher attr.ia_mode = mode;
21160dd2b474SMiklos Szeredi }
2117536e43d1STrond Myklebust if (open_flags & O_TRUNC) {
2118536e43d1STrond Myklebust attr.ia_valid |= ATTR_SIZE;
2119536e43d1STrond Myklebust attr.ia_size = 0;
2120cd9a1c0eSTrond Myklebust }
2121cd9a1c0eSTrond Myklebust
2122c94c0953SAl Viro if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
2123c94c0953SAl Viro d_drop(dentry);
2124c94c0953SAl Viro switched = true;
2125c94c0953SAl Viro dentry = d_alloc_parallel(dentry->d_parent,
2126c94c0953SAl Viro &dentry->d_name, &wq);
2127c94c0953SAl Viro if (IS_ERR(dentry))
2128c94c0953SAl Viro return PTR_ERR(dentry);
2129c94c0953SAl Viro if (unlikely(!d_in_lookup(dentry)))
2130c94c0953SAl Viro return finish_no_open(file, dentry);
2131c94c0953SAl Viro }
2132c94c0953SAl Viro
2133532d4defSNeilBrown ctx = create_nfs_open_context(dentry, open_flags, file);
21340dd2b474SMiklos Szeredi err = PTR_ERR(ctx);
21350dd2b474SMiklos Szeredi if (IS_ERR(ctx))
2136d9585277SAl Viro goto out;
21370dd2b474SMiklos Szeredi
21386e0d0be7STrond Myklebust trace_nfs_atomic_open_enter(dir, ctx, open_flags);
213973a09dd9SAl Viro inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
214073a09dd9SAl Viro if (created)
214173a09dd9SAl Viro file->f_mode |= FMODE_CREATED;
2142275bb307STrond Myklebust if (IS_ERR(inode)) {
21430dd2b474SMiklos Szeredi err = PTR_ERR(inode);
21446e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
21452d9db750STrond Myklebust put_nfs_open_context(ctx);
2146d20cb71dSAl Viro d_drop(dentry);
21470dd2b474SMiklos Szeredi switch (err) {
21481da177e4SLinus Torvalds case -ENOENT:
2149774d9513SPeng Tao d_splice_alias(NULL, dentry);
215068eaba4cSTrond Myklebust if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
215168eaba4cSTrond Myklebust dir_verifier = inode_peek_iversion_raw(dir);
215268eaba4cSTrond Myklebust else
215368eaba4cSTrond Myklebust dir_verifier = nfs_save_change_attribute(dir);
215468eaba4cSTrond Myklebust nfs_set_verifier(dentry, dir_verifier);
21550dd2b474SMiklos Szeredi break;
21561788ea6eSJeff Layton case -EISDIR:
21576f926b5bSTrond Myklebust case -ENOTDIR:
21586f926b5bSTrond Myklebust goto no_open;
21591da177e4SLinus Torvalds case -ELOOP:
21600dd2b474SMiklos Szeredi if (!(open_flags & O_NOFOLLOW))
21611da177e4SLinus Torvalds goto no_open;
21620dd2b474SMiklos Szeredi break;
21631da177e4SLinus Torvalds /* case -EINVAL: */
21641da177e4SLinus Torvalds default:
21650dd2b474SMiklos Szeredi break;
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds goto out;
21681da177e4SLinus Torvalds }
21695ee3d10fSDave Wysochanski file->f_mode |= FMODE_CAN_ODIRECT;
21700dd2b474SMiklos Szeredi
2171b452a458SAl Viro err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
21726e0d0be7STrond Myklebust trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
21732d9db750STrond Myklebust put_nfs_open_context(ctx);
2174d9585277SAl Viro out:
2175c94c0953SAl Viro if (unlikely(switched)) {
2176c94c0953SAl Viro d_lookup_done(dentry);
2177c94c0953SAl Viro dput(dentry);
2178c94c0953SAl Viro }
2179d9585277SAl Viro return err;
21800dd2b474SMiklos Szeredi
21811da177e4SLinus Torvalds no_open:
21821472b83eSTrond Myklebust res = nfs_lookup(dir, dentry, lookup_flags);
2183ac795161STrond Myklebust if (!res) {
2184ac795161STrond Myklebust inode = d_inode(dentry);
2185ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2186e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2187ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR);
21881751fc1dSTrond Myklebust else if (inode && S_ISREG(inode->i_mode))
21891751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE);
2190ac795161STrond Myklebust } else if (!IS_ERR(res)) {
2191ac795161STrond Myklebust inode = d_inode(res);
2192ac795161STrond Myklebust if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2193e0caaf75STrond Myklebust !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2194ac795161STrond Myklebust dput(res);
2195ac795161STrond Myklebust res = ERR_PTR(-ENOTDIR);
21961751fc1dSTrond Myklebust } else if (inode && S_ISREG(inode->i_mode)) {
21971751fc1dSTrond Myklebust dput(res);
21981751fc1dSTrond Myklebust res = ERR_PTR(-EOPENSTALE);
2199ac795161STrond Myklebust }
2200ac795161STrond Myklebust }
2201c94c0953SAl Viro if (switched) {
2202c94c0953SAl Viro d_lookup_done(dentry);
2203c94c0953SAl Viro if (!res)
2204c94c0953SAl Viro res = dentry;
2205c94c0953SAl Viro else
2206c94c0953SAl Viro dput(dentry);
2207c94c0953SAl Viro }
22080dd2b474SMiklos Szeredi if (IS_ERR(res))
2209c94c0953SAl Viro return PTR_ERR(res);
2210e45198a6SAl Viro return finish_no_open(file, res);
22111da177e4SLinus Torvalds }
221289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
22131da177e4SLinus Torvalds
2214c7944ebbSTrond Myklebust static int
nfs4_do_lookup_revalidate(struct inode * dir,struct dentry * dentry,unsigned int flags)2215c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2216c7944ebbSTrond Myklebust unsigned int flags)
22171da177e4SLinus Torvalds {
2218657e94b6SNick Piggin struct inode *inode;
22191da177e4SLinus Torvalds
2220fa3c56bbSAl Viro if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2221c7944ebbSTrond Myklebust goto full_reval;
2222eda72afbSMiklos Szeredi if (d_mountpoint(dentry))
2223c7944ebbSTrond Myklebust goto full_reval;
22242b484297STrond Myklebust
22252b0143b5SDavid Howells inode = d_inode(dentry);
22262b484297STrond Myklebust
22271da177e4SLinus Torvalds /* We can't create new files in nfs_open_revalidate(), so we
22281da177e4SLinus Torvalds * optimize away revalidation of negative dentries.
22291da177e4SLinus Torvalds */
2230c7944ebbSTrond Myklebust if (inode == NULL)
2231c7944ebbSTrond Myklebust goto full_reval;
223249317a7fSNeilBrown
2233efeda80dSTrond Myklebust if (nfs_verifier_is_delegated(dentry))
2234c7944ebbSTrond Myklebust return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2235216d5d06STrond Myklebust
22361da177e4SLinus Torvalds /* NFS only supports OPEN on regular files */
22371da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode))
2238c7944ebbSTrond Myklebust goto full_reval;
2239c7944ebbSTrond Myklebust
22401da177e4SLinus Torvalds /* We cannot do exclusive creation on a positive dentry */
2241c7944ebbSTrond Myklebust if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2242c7944ebbSTrond Myklebust goto reval_dentry;
2243c7944ebbSTrond Myklebust
2244c7944ebbSTrond Myklebust /* Check if the directory changed */
2245c7944ebbSTrond Myklebust if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2246c7944ebbSTrond Myklebust goto reval_dentry;
22471da177e4SLinus Torvalds
22480ef97dcfSMiklos Szeredi /* Let f_op->open() actually open (and revalidate) the file */
2249c7944ebbSTrond Myklebust return 1;
2250c7944ebbSTrond Myklebust reval_dentry:
2251c7944ebbSTrond Myklebust if (flags & LOOKUP_RCU)
2252c7944ebbSTrond Myklebust return -ECHILD;
22530b3cc71bSTrond Myklebust return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
22540ef97dcfSMiklos Szeredi
2255c7944ebbSTrond Myklebust full_reval:
2256c7944ebbSTrond Myklebust return nfs_do_lookup_revalidate(dir, dentry, flags);
2257c7944ebbSTrond Myklebust }
2258535918f1STrond Myklebust
nfs4_lookup_revalidate(struct dentry * dentry,unsigned int flags)2259c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2260c7944ebbSTrond Myklebust {
2261c7944ebbSTrond Myklebust return __nfs_lookup_revalidate(dentry, flags,
2262c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate);
2263c0204fd2STrond Myklebust }
2264c0204fd2STrond Myklebust
22651da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
22661da177e4SLinus Torvalds
2267406cd915SBenjamin Coddington struct dentry *
nfs_add_or_obtain(struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)2268406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2269cc6f3298SAnna Schumaker struct nfs_fattr *fattr)
22701da177e4SLinus Torvalds {
2271fab728e1STrond Myklebust struct dentry *parent = dget_parent(dentry);
22722b0143b5SDavid Howells struct inode *dir = d_inode(parent);
22731da177e4SLinus Torvalds struct inode *inode;
2274b0c6108eSAl Viro struct dentry *d;
2275406cd915SBenjamin Coddington int error;
22761da177e4SLinus Torvalds
2277fab728e1STrond Myklebust d_drop(dentry);
2278fab728e1STrond Myklebust
22791da177e4SLinus Torvalds if (fhandle->size == 0) {
22809558a007SAnna Schumaker error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
22811da177e4SLinus Torvalds if (error)
2282fab728e1STrond Myklebust goto out_error;
22831da177e4SLinus Torvalds }
22845724ab37STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
22851da177e4SLinus Torvalds if (!(fattr->valid & NFS_ATTR_FATTR)) {
22861da177e4SLinus Torvalds struct nfs_server *server = NFS_SB(dentry->d_sb);
2287a841b54dSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, fhandle,
22882ef61e0eSAnna Schumaker fattr, NULL);
22891da177e4SLinus Torvalds if (error < 0)
2290fab728e1STrond Myklebust goto out_error;
22911da177e4SLinus Torvalds }
2292cf7ab00aSAnna Schumaker inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2293b0c6108eSAl Viro d = d_splice_alias(inode, dentry);
2294fab728e1STrond Myklebust out:
2295fab728e1STrond Myklebust dput(parent);
2296406cd915SBenjamin Coddington return d;
2297fab728e1STrond Myklebust out_error:
2298406cd915SBenjamin Coddington d = ERR_PTR(error);
2299406cd915SBenjamin Coddington goto out;
2300406cd915SBenjamin Coddington }
2301406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2302406cd915SBenjamin Coddington
2303406cd915SBenjamin Coddington /*
2304406cd915SBenjamin Coddington * Code common to create, mkdir, and mknod.
2305406cd915SBenjamin Coddington */
nfs_instantiate(struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)2306406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2307d91bfc46SAnna Schumaker struct nfs_fattr *fattr)
2308406cd915SBenjamin Coddington {
2309406cd915SBenjamin Coddington struct dentry *d;
2310406cd915SBenjamin Coddington
2311cc6f3298SAnna Schumaker d = nfs_add_or_obtain(dentry, fhandle, fattr);
2312406cd915SBenjamin Coddington if (IS_ERR(d))
2313406cd915SBenjamin Coddington return PTR_ERR(d);
2314406cd915SBenjamin Coddington
2315406cd915SBenjamin Coddington /* Callers don't care */
2316406cd915SBenjamin Coddington dput(d);
2317406cd915SBenjamin Coddington return 0;
23181da177e4SLinus Torvalds }
2319ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
23201da177e4SLinus Torvalds
23211da177e4SLinus Torvalds /*
23221da177e4SLinus Torvalds * Following a failed create operation, we drop the dentry rather
23231da177e4SLinus Torvalds * than retain a negative dentry. This avoids a problem in the event
23241da177e4SLinus Torvalds * that the operation succeeded on the server, but an error in the
23251da177e4SLinus Torvalds * reply path made it appear to have failed.
23261da177e4SLinus Torvalds */
nfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)23276c960e68SChristian Brauner int nfs_create(struct mnt_idmap *idmap, struct inode *dir,
2328549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds struct iattr attr;
2331ebfc3b49SAl Viro int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
23321da177e4SLinus Torvalds int error;
23331da177e4SLinus Torvalds
23341e8968c5SNiels de Vos dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
23356de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry);
23361da177e4SLinus Torvalds
23371da177e4SLinus Torvalds attr.ia_mode = mode;
23381da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE;
23391da177e4SLinus Torvalds
23408b0ad3d4STrond Myklebust trace_nfs_create_enter(dir, dentry, open_flags);
23418867fe58SMiklos Szeredi error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
23428b0ad3d4STrond Myklebust trace_nfs_create_exit(dir, dentry, open_flags, error);
23431da177e4SLinus Torvalds if (error != 0)
23441da177e4SLinus Torvalds goto out_err;
23451da177e4SLinus Torvalds return 0;
23461da177e4SLinus Torvalds out_err:
23471da177e4SLinus Torvalds d_drop(dentry);
23481da177e4SLinus Torvalds return error;
23491da177e4SLinus Torvalds }
2350ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
23511da177e4SLinus Torvalds
23521da177e4SLinus Torvalds /*
23531da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations.
23541da177e4SLinus Torvalds */
2355597d9289SBryan Schumaker int
nfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)23565ebb29beSChristian Brauner nfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
2357549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev)
23581da177e4SLinus Torvalds {
23591da177e4SLinus Torvalds struct iattr attr;
23601da177e4SLinus Torvalds int status;
23611da177e4SLinus Torvalds
23621e8968c5SNiels de Vos dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
23636de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry);
23641da177e4SLinus Torvalds
23651da177e4SLinus Torvalds attr.ia_mode = mode;
23661da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE;
23671da177e4SLinus Torvalds
23681ca42382STrond Myklebust trace_nfs_mknod_enter(dir, dentry);
23691da177e4SLinus Torvalds status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
23701ca42382STrond Myklebust trace_nfs_mknod_exit(dir, dentry, status);
23711da177e4SLinus Torvalds if (status != 0)
23721da177e4SLinus Torvalds goto out_err;
23731da177e4SLinus Torvalds return 0;
23741da177e4SLinus Torvalds out_err:
23751da177e4SLinus Torvalds d_drop(dentry);
23761da177e4SLinus Torvalds return status;
23771da177e4SLinus Torvalds }
2378ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
23791da177e4SLinus Torvalds
23801da177e4SLinus Torvalds /*
23811da177e4SLinus Torvalds * See comments for nfs_proc_create regarding failed operations.
23821da177e4SLinus Torvalds */
nfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)2383c54bd91eSChristian Brauner int nfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
2384549c7297SChristian Brauner struct dentry *dentry, umode_t mode)
23851da177e4SLinus Torvalds {
23861da177e4SLinus Torvalds struct iattr attr;
23871da177e4SLinus Torvalds int error;
23881da177e4SLinus Torvalds
23891e8968c5SNiels de Vos dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
23906de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry);
23911da177e4SLinus Torvalds
23921da177e4SLinus Torvalds attr.ia_valid = ATTR_MODE;
23931da177e4SLinus Torvalds attr.ia_mode = mode | S_IFDIR;
23941da177e4SLinus Torvalds
23951ca42382STrond Myklebust trace_nfs_mkdir_enter(dir, dentry);
23961da177e4SLinus Torvalds error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
23971ca42382STrond Myklebust trace_nfs_mkdir_exit(dir, dentry, error);
23981da177e4SLinus Torvalds if (error != 0)
23991da177e4SLinus Torvalds goto out_err;
24001da177e4SLinus Torvalds return 0;
24011da177e4SLinus Torvalds out_err:
24021da177e4SLinus Torvalds d_drop(dentry);
24031da177e4SLinus Torvalds return error;
24041da177e4SLinus Torvalds }
2405ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
24061da177e4SLinus Torvalds
nfs_dentry_handle_enoent(struct dentry * dentry)2407d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2408d45b9d8bSTrond Myklebust {
2409dc3f4198SAl Viro if (simple_positive(dentry))
2410d45b9d8bSTrond Myklebust d_delete(dentry);
2411d45b9d8bSTrond Myklebust }
2412d45b9d8bSTrond Myklebust
nfs_dentry_remove_handle_error(struct inode * dir,struct dentry * dentry,int error)24139019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir,
24149019fb39STrond Myklebust struct dentry *dentry, int error)
24159019fb39STrond Myklebust {
24169019fb39STrond Myklebust switch (error) {
24179019fb39STrond Myklebust case -ENOENT:
2418f16857e6SNeilBrown if (d_really_is_positive(dentry))
24199019fb39STrond Myklebust d_delete(dentry);
242000bdadc7STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
242100bdadc7STrond Myklebust break;
24229019fb39STrond Myklebust case 0:
242300bdadc7STrond Myklebust nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
24249019fb39STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
24259019fb39STrond Myklebust }
24269019fb39STrond Myklebust }
24279019fb39STrond Myklebust
nfs_rmdir(struct inode * dir,struct dentry * dentry)2428597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
24291da177e4SLinus Torvalds {
24301da177e4SLinus Torvalds int error;
24311da177e4SLinus Torvalds
24321e8968c5SNiels de Vos dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
24336de1472fSAl Viro dir->i_sb->s_id, dir->i_ino, dentry);
24341da177e4SLinus Torvalds
24351ca42382STrond Myklebust trace_nfs_rmdir_enter(dir, dentry);
24362b0143b5SDavid Howells if (d_really_is_positive(dentry)) {
2437884be175SAl Viro down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
24381da177e4SLinus Torvalds error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
24391da177e4SLinus Torvalds /* Ensure the VFS deletes this inode */
2440ba6c0592STrond Myklebust switch (error) {
2441ba6c0592STrond Myklebust case 0:
24422b0143b5SDavid Howells clear_nlink(d_inode(dentry));
2443ba6c0592STrond Myklebust break;
2444ba6c0592STrond Myklebust case -ENOENT:
2445d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry);
2446ba6c0592STrond Myklebust }
2447884be175SAl Viro up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2448ba6c0592STrond Myklebust } else
2449ba6c0592STrond Myklebust error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
24509019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error);
24511ca42382STrond Myklebust trace_nfs_rmdir_exit(dir, dentry, error);
24521da177e4SLinus Torvalds
24531da177e4SLinus Torvalds return error;
24541da177e4SLinus Torvalds }
2455ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
24561da177e4SLinus Torvalds
24571da177e4SLinus Torvalds /*
24581da177e4SLinus Torvalds * Remove a file after making sure there are no pending writes,
24591da177e4SLinus Torvalds * and after checking that the file has only one user.
24601da177e4SLinus Torvalds *
24611da177e4SLinus Torvalds * We invalidate the attribute cache and free the inode prior to the operation
24621da177e4SLinus Torvalds * to avoid possible races if the server reuses the inode.
24631da177e4SLinus Torvalds */
nfs_safe_remove(struct dentry * dentry)24641da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
24651da177e4SLinus Torvalds {
24662b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent);
24672b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
24681da177e4SLinus Torvalds int error = -EBUSY;
24691da177e4SLinus Torvalds
24706de1472fSAl Viro dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
24711da177e4SLinus Torvalds
24721da177e4SLinus Torvalds /* If the dentry was sillyrenamed, we simply call d_delete() */
24731da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
24741da177e4SLinus Torvalds error = 0;
24751da177e4SLinus Torvalds goto out;
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds
24781ca42382STrond Myklebust trace_nfs_remove_enter(dir, dentry);
24791da177e4SLinus Torvalds if (inode != NULL) {
2480912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry);
24811da177e4SLinus Torvalds if (error == 0)
24821b83d707STrond Myklebust nfs_drop_nlink(inode);
24831da177e4SLinus Torvalds } else
2484912678dbSTrond Myklebust error = NFS_PROTO(dir)->remove(dir, dentry);
2485d45b9d8bSTrond Myklebust if (error == -ENOENT)
2486d45b9d8bSTrond Myklebust nfs_dentry_handle_enoent(dentry);
24871ca42382STrond Myklebust trace_nfs_remove_exit(dir, dentry, error);
24881da177e4SLinus Torvalds out:
24891da177e4SLinus Torvalds return error;
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds
24921da177e4SLinus Torvalds /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
24931da177e4SLinus Torvalds * belongs to an active ".nfs..." file and we return -EBUSY.
24941da177e4SLinus Torvalds *
24951da177e4SLinus Torvalds * If sillyrename() returns 0, we do nothing, otherwise we unlink.
24961da177e4SLinus Torvalds */
nfs_unlink(struct inode * dir,struct dentry * dentry)2497597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
24981da177e4SLinus Torvalds {
24991da177e4SLinus Torvalds int error;
25001da177e4SLinus Torvalds
25011e8968c5SNiels de Vos dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
25026de1472fSAl Viro dir->i_ino, dentry);
25031da177e4SLinus Torvalds
25041ca42382STrond Myklebust trace_nfs_unlink_enter(dir, dentry);
25051da177e4SLinus Torvalds spin_lock(&dentry->d_lock);
250643245ecaSOlga Kornievskaia if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
250743245ecaSOlga Kornievskaia &NFS_I(d_inode(dentry))->flags)) {
25081da177e4SLinus Torvalds spin_unlock(&dentry->d_lock);
2509ccfeb506STrond Myklebust /* Start asynchronous writeout of the inode */
25102b0143b5SDavid Howells write_inode_now(d_inode(dentry), 0);
25111da177e4SLinus Torvalds error = nfs_sillyrename(dir, dentry);
25121ca42382STrond Myklebust goto out;
25131da177e4SLinus Torvalds }
25143c59366cSNeilBrown /* We must prevent any concurrent open until the unlink
25153c59366cSNeilBrown * completes. ->d_revalidate will wait for ->d_fsdata
25163c59366cSNeilBrown * to clear. We set it here to ensure no lookup succeeds until
25173c59366cSNeilBrown * the unlink is complete on the server.
25183c59366cSNeilBrown */
25193c59366cSNeilBrown error = -ETXTBSY;
25203c59366cSNeilBrown if (WARN_ON(dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
25212067231aSSun Ke WARN_ON(dentry->d_fsdata == NFS_FSDATA_BLOCKED)) {
25222067231aSSun Ke spin_unlock(&dentry->d_lock);
25233c59366cSNeilBrown goto out;
25242067231aSSun Ke }
2525b21cae46SNeilBrown block_revalidate(dentry);
25263c59366cSNeilBrown
25271da177e4SLinus Torvalds spin_unlock(&dentry->d_lock);
25281da177e4SLinus Torvalds error = nfs_safe_remove(dentry);
25299019fb39STrond Myklebust nfs_dentry_remove_handle_error(dir, dentry, error);
2530b21cae46SNeilBrown unblock_revalidate(dentry);
25311ca42382STrond Myklebust out:
25321ca42382STrond Myklebust trace_nfs_unlink_exit(dir, dentry, error);
25331da177e4SLinus Torvalds return error;
25341da177e4SLinus Torvalds }
2535ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
25361da177e4SLinus Torvalds
2537873101b3SChuck Lever /*
2538873101b3SChuck Lever * To create a symbolic link, most file systems instantiate a new inode,
2539873101b3SChuck Lever * add a page to it containing the path, then write it out to the disk
2540873101b3SChuck Lever * using prepare_write/commit_write.
2541873101b3SChuck Lever *
2542873101b3SChuck Lever * Unfortunately the NFS client can't create the in-core inode first
2543873101b3SChuck Lever * because it needs a file handle to create an in-core inode (see
2544873101b3SChuck Lever * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
2545873101b3SChuck Lever * symlink request has completed on the server.
2546873101b3SChuck Lever *
2547873101b3SChuck Lever * So instead we allocate a raw page, copy the symname into it, then do
2548873101b3SChuck Lever * the SYMLINK request with the page as the buffer. If it succeeds, we
2549873101b3SChuck Lever * now have a new file handle and can instantiate an in-core NFS inode
2550873101b3SChuck Lever * and move the raw page into its mapping.
2551873101b3SChuck Lever */
nfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)25527a77db95SChristian Brauner int nfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
2553549c7297SChristian Brauner struct dentry *dentry, const char *symname)
25541da177e4SLinus Torvalds {
2555873101b3SChuck Lever struct page *page;
2556873101b3SChuck Lever char *kaddr;
25571da177e4SLinus Torvalds struct iattr attr;
2558873101b3SChuck Lever unsigned int pathlen = strlen(symname);
25591da177e4SLinus Torvalds int error;
25601da177e4SLinus Torvalds
25611e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
25626de1472fSAl Viro dir->i_ino, dentry, symname);
25631da177e4SLinus Torvalds
2564873101b3SChuck Lever if (pathlen > PAGE_SIZE)
2565873101b3SChuck Lever return -ENAMETOOLONG;
25661da177e4SLinus Torvalds
2567873101b3SChuck Lever attr.ia_mode = S_IFLNK | S_IRWXUGO;
2568873101b3SChuck Lever attr.ia_valid = ATTR_MODE;
25691da177e4SLinus Torvalds
2570e8ecde25SAl Viro page = alloc_page(GFP_USER);
257176566991STrond Myklebust if (!page)
2572873101b3SChuck Lever return -ENOMEM;
2573873101b3SChuck Lever
2574e8ecde25SAl Viro kaddr = page_address(page);
2575873101b3SChuck Lever memcpy(kaddr, symname, pathlen);
2576873101b3SChuck Lever if (pathlen < PAGE_SIZE)
2577873101b3SChuck Lever memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2578873101b3SChuck Lever
25791ca42382STrond Myklebust trace_nfs_symlink_enter(dir, dentry);
258094a6d753SChuck Lever error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
25811ca42382STrond Myklebust trace_nfs_symlink_exit(dir, dentry, error);
2582873101b3SChuck Lever if (error != 0) {
25831e8968c5SNiels de Vos dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2584873101b3SChuck Lever dir->i_sb->s_id, dir->i_ino,
25856de1472fSAl Viro dentry, symname, error);
25861da177e4SLinus Torvalds d_drop(dentry);
2587873101b3SChuck Lever __free_page(page);
25881da177e4SLinus Torvalds return error;
25891da177e4SLinus Torvalds }
25901da177e4SLinus Torvalds
2591342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2592342a67f0STrond Myklebust
2593873101b3SChuck Lever /*
2594873101b3SChuck Lever * No big deal if we can't add this page to the page cache here.
2595873101b3SChuck Lever * READLINK will get the missing page from the server if needed.
2596873101b3SChuck Lever */
25972b0143b5SDavid Howells if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2598873101b3SChuck Lever GFP_KERNEL)) {
2599873101b3SChuck Lever SetPageUptodate(page);
2600873101b3SChuck Lever unlock_page(page);
2601a0b54addSRafael Aquini /*
2602a0b54addSRafael Aquini * add_to_page_cache_lru() grabs an extra page refcount.
2603a0b54addSRafael Aquini * Drop it here to avoid leaking this page later.
2604a0b54addSRafael Aquini */
260509cbfeafSKirill A. Shutemov put_page(page);
2606873101b3SChuck Lever } else
2607873101b3SChuck Lever __free_page(page);
2608873101b3SChuck Lever
2609873101b3SChuck Lever return 0;
2610873101b3SChuck Lever }
2611ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2612873101b3SChuck Lever
2613597d9289SBryan Schumaker int
nfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)26141da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
26151da177e4SLinus Torvalds {
26162b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry);
26171da177e4SLinus Torvalds int error;
26181da177e4SLinus Torvalds
26196de1472fSAl Viro dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
26206de1472fSAl Viro old_dentry, dentry);
26211da177e4SLinus Torvalds
26221fd1085bSTrond Myklebust trace_nfs_link_enter(inode, dir, dentry);
26239697d234STrond Myklebust d_drop(dentry);
262420497503STrond Myklebust if (S_ISREG(inode->i_mode))
262520497503STrond Myklebust nfs_sync_inode(inode);
26261da177e4SLinus Torvalds error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2627cf809556STrond Myklebust if (error == 0) {
2628342a67f0STrond Myklebust nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
26297de9c6eeSAl Viro ihold(inode);
26309697d234STrond Myklebust d_add(dentry, inode);
2631cf809556STrond Myklebust }
26321fd1085bSTrond Myklebust trace_nfs_link_exit(inode, dir, dentry, error);
26331da177e4SLinus Torvalds return error;
26341da177e4SLinus Torvalds }
2635ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
26361da177e4SLinus Torvalds
26373c59366cSNeilBrown static void
nfs_unblock_rename(struct rpc_task * task,struct nfs_renamedata * data)26383c59366cSNeilBrown nfs_unblock_rename(struct rpc_task *task, struct nfs_renamedata *data)
26393c59366cSNeilBrown {
26403c59366cSNeilBrown struct dentry *new_dentry = data->new_dentry;
26413c59366cSNeilBrown
2642b21cae46SNeilBrown unblock_revalidate(new_dentry);
26433c59366cSNeilBrown }
26443c59366cSNeilBrown
26451da177e4SLinus Torvalds /*
26461da177e4SLinus Torvalds * RENAME
26471da177e4SLinus Torvalds * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
26481da177e4SLinus Torvalds * different file handle for the same inode after a rename (e.g. when
26491da177e4SLinus Torvalds * moving to a different directory). A fail-safe method to do so would
26501da177e4SLinus Torvalds * be to look up old_dir/old_name, create a link to new_dir/new_name and
26511da177e4SLinus Torvalds * rename the old file using the sillyrename stuff. This way, the original
26521da177e4SLinus Torvalds * file in old_dir will go away when the last process iput()s the inode.
26531da177e4SLinus Torvalds *
26541da177e4SLinus Torvalds * FIXED.
26551da177e4SLinus Torvalds *
26561da177e4SLinus Torvalds * It actually works quite well. One needs to have the possibility for
26571da177e4SLinus Torvalds * at least one ".nfs..." file in each directory the file ever gets
26581da177e4SLinus Torvalds * moved or linked to which happens automagically with the new
26591da177e4SLinus Torvalds * implementation that only depends on the dcache stuff instead of
26601da177e4SLinus Torvalds * using the inode layer
26611da177e4SLinus Torvalds *
26621da177e4SLinus Torvalds * Unfortunately, things are a little more complicated than indicated
26631da177e4SLinus Torvalds * above. For a cross-directory move, we want to make sure we can get
26641da177e4SLinus Torvalds * rid of the old inode after the operation. This means there must be
26651da177e4SLinus Torvalds * no pending writes (if it's a file), and the use count must be 1.
26661da177e4SLinus Torvalds * If these conditions are met, we can drop the dentries before doing
26671da177e4SLinus Torvalds * the rename.
26681da177e4SLinus Torvalds */
nfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)2669e18275aeSChristian Brauner int nfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
2670549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir,
2671549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags)
26721da177e4SLinus Torvalds {
26732b0143b5SDavid Howells struct inode *old_inode = d_inode(old_dentry);
26742b0143b5SDavid Howells struct inode *new_inode = d_inode(new_dentry);
26753c59366cSNeilBrown struct dentry *dentry = NULL;
267680a491fdSJeff Layton struct rpc_task *task;
26773c59366cSNeilBrown bool must_unblock = false;
26781da177e4SLinus Torvalds int error = -EBUSY;
26791da177e4SLinus Torvalds
26801cd66c93SMiklos Szeredi if (flags)
26811cd66c93SMiklos Szeredi return -EINVAL;
26821cd66c93SMiklos Szeredi
26836de1472fSAl Viro dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
26846de1472fSAl Viro old_dentry, new_dentry,
268584d08fa8SAl Viro d_count(new_dentry));
26861da177e4SLinus Torvalds
268770ded201STrond Myklebust trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
26881da177e4SLinus Torvalds /*
268928f79a1aSMiklos Szeredi * For non-directories, check whether the target is busy and if so,
269028f79a1aSMiklos Szeredi * make a copy of the dentry and then do a silly-rename. If the
269128f79a1aSMiklos Szeredi * silly-rename succeeds, the copied dentry is hashed and becomes
269228f79a1aSMiklos Szeredi * the new target.
26931da177e4SLinus Torvalds */
269427226104SMiklos Szeredi if (new_inode && !S_ISDIR(new_inode->i_mode)) {
26953c59366cSNeilBrown /* We must prevent any concurrent open until the unlink
26963c59366cSNeilBrown * completes. ->d_revalidate will wait for ->d_fsdata
26973c59366cSNeilBrown * to clear. We set it here to ensure no lookup succeeds until
26983c59366cSNeilBrown * the unlink is complete on the server.
269927226104SMiklos Szeredi */
27003c59366cSNeilBrown error = -ETXTBSY;
27013c59366cSNeilBrown if (WARN_ON(new_dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
27023c59366cSNeilBrown WARN_ON(new_dentry->d_fsdata == NFS_FSDATA_BLOCKED))
27033c59366cSNeilBrown goto out;
270427226104SMiklos Szeredi
27053c59366cSNeilBrown spin_lock(&new_dentry->d_lock);
270684d08fa8SAl Viro if (d_count(new_dentry) > 2) {
27071da177e4SLinus Torvalds int err;
270827226104SMiklos Szeredi
27093c59366cSNeilBrown spin_unlock(&new_dentry->d_lock);
27103c59366cSNeilBrown
27111da177e4SLinus Torvalds /* copy the target dentry's name */
27121da177e4SLinus Torvalds dentry = d_alloc(new_dentry->d_parent,
27131da177e4SLinus Torvalds &new_dentry->d_name);
27141da177e4SLinus Torvalds if (!dentry)
27151da177e4SLinus Torvalds goto out;
27161da177e4SLinus Torvalds
27171da177e4SLinus Torvalds /* silly-rename the existing target ... */
27181da177e4SLinus Torvalds err = nfs_sillyrename(new_dir, new_dentry);
271924e93025SMiklos Szeredi if (err)
27201da177e4SLinus Torvalds goto out;
272124e93025SMiklos Szeredi
272224e93025SMiklos Szeredi new_dentry = dentry;
272324e93025SMiklos Szeredi new_inode = NULL;
27243c59366cSNeilBrown } else {
2725b21cae46SNeilBrown block_revalidate(new_dentry);
27263c59366cSNeilBrown must_unblock = true;
27273c59366cSNeilBrown spin_unlock(&new_dentry->d_lock);
2728b1e4adf4STrond Myklebust }
27293c59366cSNeilBrown
273027226104SMiklos Szeredi }
27311da177e4SLinus Torvalds
27326ff9d99bSTrond Myklebust if (S_ISREG(old_inode->i_mode))
27336ff9d99bSTrond Myklebust nfs_sync_inode(old_inode);
27343c59366cSNeilBrown task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry,
27353c59366cSNeilBrown must_unblock ? nfs_unblock_rename : NULL);
273680a491fdSJeff Layton if (IS_ERR(task)) {
2737b21cae46SNeilBrown if (must_unblock)
2738b21cae46SNeilBrown unblock_revalidate(new_dentry);
273980a491fdSJeff Layton error = PTR_ERR(task);
274080a491fdSJeff Layton goto out;
274180a491fdSJeff Layton }
274280a491fdSJeff Layton
274380a491fdSJeff Layton error = rpc_wait_for_completion_task(task);
2744818a8dbeSBenjamin Coddington if (error != 0) {
2745818a8dbeSBenjamin Coddington ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2746818a8dbeSBenjamin Coddington /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2747818a8dbeSBenjamin Coddington smp_wmb();
2748818a8dbeSBenjamin Coddington } else
274980a491fdSJeff Layton error = task->tk_status;
275080a491fdSJeff Layton rpc_put_task(task);
275159a707b0STrond Myklebust /* Ensure the inode attributes are revalidated */
275259a707b0STrond Myklebust if (error == 0) {
275359a707b0STrond Myklebust spin_lock(&old_inode->i_lock);
275459a707b0STrond Myklebust NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2755ac46b3d7STrond Myklebust nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2756ac46b3d7STrond Myklebust NFS_INO_INVALID_CTIME |
2757ac46b3d7STrond Myklebust NFS_INO_REVAL_FORCED);
275859a707b0STrond Myklebust spin_unlock(&old_inode->i_lock);
275959a707b0STrond Myklebust }
27601da177e4SLinus Torvalds out:
276170ded201STrond Myklebust trace_nfs_rename_exit(old_dir, old_dentry,
276270ded201STrond Myklebust new_dir, new_dentry, error);
2763d9f29500SBenjamin Coddington if (!error) {
2764d9f29500SBenjamin Coddington if (new_inode != NULL)
2765d9f29500SBenjamin Coddington nfs_drop_nlink(new_inode);
2766d9f29500SBenjamin Coddington /*
2767d9f29500SBenjamin Coddington * The d_move() should be here instead of in an async RPC completion
2768d9f29500SBenjamin Coddington * handler because we need the proper locks to move the dentry. If
2769d9f29500SBenjamin Coddington * we're interrupted by a signal, the async RPC completion handler
2770d9f29500SBenjamin Coddington * should mark the directories for revalidation.
2771d9f29500SBenjamin Coddington */
2772d9f29500SBenjamin Coddington d_move(old_dentry, new_dentry);
2773d803224cSTrond Myklebust nfs_set_verifier(old_dentry,
2774d9f29500SBenjamin Coddington nfs_save_change_attribute(new_dir));
2775d9f29500SBenjamin Coddington } else if (error == -ENOENT)
2776d9f29500SBenjamin Coddington nfs_dentry_handle_enoent(old_dentry);
2777d9f29500SBenjamin Coddington
27781da177e4SLinus Torvalds /* new dentry created? */
27791da177e4SLinus Torvalds if (dentry)
27801da177e4SLinus Torvalds dput(dentry);
27811da177e4SLinus Torvalds return error;
27821da177e4SLinus Torvalds }
2783ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
27841da177e4SLinus Torvalds
2785cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2786cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2787cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2788cfcea3e8STrond Myklebust
2789a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
27903a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
27913a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
27923a505845STrond Myklebust
nfs_access_free_entry(struct nfs_access_entry * entry)27931c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
27941c3c07e9STrond Myklebust {
27956238aec8SNeilBrown put_group_info(entry->group_info);
2796f682a398SNeilBrown kfree_rcu(entry, rcu_head);
27974e857c58SPeter Zijlstra smp_mb__before_atomic();
2798cfcea3e8STrond Myklebust atomic_long_dec(&nfs_access_nr_entries);
27994e857c58SPeter Zijlstra smp_mb__after_atomic();
28001c3c07e9STrond Myklebust }
28011c3c07e9STrond Myklebust
nfs_access_free_list(struct list_head * head)28021a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
28031a81bb8aSTrond Myklebust {
28041a81bb8aSTrond Myklebust struct nfs_access_entry *cache;
28051a81bb8aSTrond Myklebust
28061a81bb8aSTrond Myklebust while (!list_empty(head)) {
28071a81bb8aSTrond Myklebust cache = list_entry(head->next, struct nfs_access_entry, lru);
28081a81bb8aSTrond Myklebust list_del(&cache->lru);
28091a81bb8aSTrond Myklebust nfs_access_free_entry(cache);
28101a81bb8aSTrond Myklebust }
28111a81bb8aSTrond Myklebust }
28121a81bb8aSTrond Myklebust
28133a505845STrond Myklebust static unsigned long
nfs_do_access_cache_scan(unsigned int nr_to_scan)28143a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2815979df72eSTrond Myklebust {
2816979df72eSTrond Myklebust LIST_HEAD(head);
2817aa510da5STrond Myklebust struct nfs_inode *nfsi, *next;
2818979df72eSTrond Myklebust struct nfs_access_entry *cache;
28191ab6c499SDave Chinner long freed = 0;
2820979df72eSTrond Myklebust
2821a50f7951STrond Myklebust spin_lock(&nfs_access_lru_lock);
2822aa510da5STrond Myklebust list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2823979df72eSTrond Myklebust struct inode *inode;
2824979df72eSTrond Myklebust
2825979df72eSTrond Myklebust if (nr_to_scan-- == 0)
2826979df72eSTrond Myklebust break;
28279c7e7e23STrond Myklebust inode = &nfsi->vfs_inode;
2828979df72eSTrond Myklebust spin_lock(&inode->i_lock);
2829979df72eSTrond Myklebust if (list_empty(&nfsi->access_cache_entry_lru))
2830979df72eSTrond Myklebust goto remove_lru_entry;
2831979df72eSTrond Myklebust cache = list_entry(nfsi->access_cache_entry_lru.next,
2832979df72eSTrond Myklebust struct nfs_access_entry, lru);
2833979df72eSTrond Myklebust list_move(&cache->lru, &head);
2834979df72eSTrond Myklebust rb_erase(&cache->rb_node, &nfsi->access_cache);
28351ab6c499SDave Chinner freed++;
2836979df72eSTrond Myklebust if (!list_empty(&nfsi->access_cache_entry_lru))
2837979df72eSTrond Myklebust list_move_tail(&nfsi->access_cache_inode_lru,
2838979df72eSTrond Myklebust &nfs_access_lru_list);
2839979df72eSTrond Myklebust else {
2840979df72eSTrond Myklebust remove_lru_entry:
2841979df72eSTrond Myklebust list_del_init(&nfsi->access_cache_inode_lru);
28424e857c58SPeter Zijlstra smp_mb__before_atomic();
2843979df72eSTrond Myklebust clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
28444e857c58SPeter Zijlstra smp_mb__after_atomic();
2845979df72eSTrond Myklebust }
284659844a9bSTrond Myklebust spin_unlock(&inode->i_lock);
2847979df72eSTrond Myklebust }
2848979df72eSTrond Myklebust spin_unlock(&nfs_access_lru_lock);
28491a81bb8aSTrond Myklebust nfs_access_free_list(&head);
28501ab6c499SDave Chinner return freed;
28511ab6c499SDave Chinner }
28521ab6c499SDave Chinner
28531ab6c499SDave Chinner unsigned long
nfs_access_cache_scan(struct shrinker * shrink,struct shrink_control * sc)28543a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
28553a505845STrond Myklebust {
28563a505845STrond Myklebust int nr_to_scan = sc->nr_to_scan;
28573a505845STrond Myklebust gfp_t gfp_mask = sc->gfp_mask;
28583a505845STrond Myklebust
28593a505845STrond Myklebust if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
28603a505845STrond Myklebust return SHRINK_STOP;
28613a505845STrond Myklebust return nfs_do_access_cache_scan(nr_to_scan);
28623a505845STrond Myklebust }
28633a505845STrond Myklebust
28643a505845STrond Myklebust
28653a505845STrond Myklebust unsigned long
nfs_access_cache_count(struct shrinker * shrink,struct shrink_control * sc)28661ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
28671ab6c499SDave Chinner {
286855f841ceSGlauber Costa return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2869979df72eSTrond Myklebust }
2870979df72eSTrond Myklebust
28713a505845STrond Myklebust static void
nfs_access_cache_enforce_limit(void)28723a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
28733a505845STrond Myklebust {
28743a505845STrond Myklebust long nr_entries = atomic_long_read(&nfs_access_nr_entries);
28753a505845STrond Myklebust unsigned long diff;
28763a505845STrond Myklebust unsigned int nr_to_scan;
28773a505845STrond Myklebust
28783a505845STrond Myklebust if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
28793a505845STrond Myklebust return;
28803a505845STrond Myklebust nr_to_scan = 100;
28813a505845STrond Myklebust diff = nr_entries - nfs_access_max_cachesize;
28823a505845STrond Myklebust if (diff < nr_to_scan)
28833a505845STrond Myklebust nr_to_scan = diff;
28843a505845STrond Myklebust nfs_do_access_cache_scan(nr_to_scan);
28853a505845STrond Myklebust }
28863a505845STrond Myklebust
__nfs_access_zap_cache(struct nfs_inode * nfsi,struct list_head * head)28871a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
28881c3c07e9STrond Myklebust {
28891c3c07e9STrond Myklebust struct rb_root *root_node = &nfsi->access_cache;
28901a81bb8aSTrond Myklebust struct rb_node *n;
28911c3c07e9STrond Myklebust struct nfs_access_entry *entry;
28921c3c07e9STrond Myklebust
28931c3c07e9STrond Myklebust /* Unhook entries from the cache */
28941c3c07e9STrond Myklebust while ((n = rb_first(root_node)) != NULL) {
28951c3c07e9STrond Myklebust entry = rb_entry(n, struct nfs_access_entry, rb_node);
28961c3c07e9STrond Myklebust rb_erase(n, root_node);
28971a81bb8aSTrond Myklebust list_move(&entry->lru, head);
28981c3c07e9STrond Myklebust }
28991c3c07e9STrond Myklebust nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
29001c3c07e9STrond Myklebust }
29011c3c07e9STrond Myklebust
nfs_access_zap_cache(struct inode * inode)29021c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
29031c3c07e9STrond Myklebust {
29041a81bb8aSTrond Myklebust LIST_HEAD(head);
29051a81bb8aSTrond Myklebust
29061a81bb8aSTrond Myklebust if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
29071a81bb8aSTrond Myklebust return;
2908cfcea3e8STrond Myklebust /* Remove from global LRU init */
2909cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock);
29101a81bb8aSTrond Myklebust if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2911cfcea3e8STrond Myklebust list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2912cfcea3e8STrond Myklebust
29131c3c07e9STrond Myklebust spin_lock(&inode->i_lock);
29141a81bb8aSTrond Myklebust __nfs_access_zap_cache(NFS_I(inode), &head);
29151a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock);
29161a81bb8aSTrond Myklebust spin_unlock(&nfs_access_lru_lock);
29171a81bb8aSTrond Myklebust nfs_access_free_list(&head);
29181c3c07e9STrond Myklebust }
29191c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
29201c3c07e9STrond Myklebust
access_cmp(const struct cred * a,const struct nfs_access_entry * b)29216238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
29226238aec8SNeilBrown {
29236238aec8SNeilBrown struct group_info *ga, *gb;
29246238aec8SNeilBrown int g;
29256238aec8SNeilBrown
29266238aec8SNeilBrown if (uid_lt(a->fsuid, b->fsuid))
29276238aec8SNeilBrown return -1;
29286238aec8SNeilBrown if (uid_gt(a->fsuid, b->fsuid))
29296238aec8SNeilBrown return 1;
29306238aec8SNeilBrown
29316238aec8SNeilBrown if (gid_lt(a->fsgid, b->fsgid))
29326238aec8SNeilBrown return -1;
29336238aec8SNeilBrown if (gid_gt(a->fsgid, b->fsgid))
29346238aec8SNeilBrown return 1;
29356238aec8SNeilBrown
29366238aec8SNeilBrown ga = a->group_info;
29376238aec8SNeilBrown gb = b->group_info;
29386238aec8SNeilBrown if (ga == gb)
29396238aec8SNeilBrown return 0;
29406238aec8SNeilBrown if (ga == NULL)
29416238aec8SNeilBrown return -1;
29426238aec8SNeilBrown if (gb == NULL)
29436238aec8SNeilBrown return 1;
29446238aec8SNeilBrown if (ga->ngroups < gb->ngroups)
29456238aec8SNeilBrown return -1;
29466238aec8SNeilBrown if (ga->ngroups > gb->ngroups)
29476238aec8SNeilBrown return 1;
29486238aec8SNeilBrown
29496238aec8SNeilBrown for (g = 0; g < ga->ngroups; g++) {
29506238aec8SNeilBrown if (gid_lt(ga->gid[g], gb->gid[g]))
29516238aec8SNeilBrown return -1;
29526238aec8SNeilBrown if (gid_gt(ga->gid[g], gb->gid[g]))
29536238aec8SNeilBrown return 1;
29546238aec8SNeilBrown }
29556238aec8SNeilBrown return 0;
29566238aec8SNeilBrown }
29576238aec8SNeilBrown
nfs_access_search_rbtree(struct inode * inode,const struct cred * cred)2958b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
29591c3c07e9STrond Myklebust {
29601c3c07e9STrond Myklebust struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
29611c3c07e9STrond Myklebust
29621c3c07e9STrond Myklebust while (n != NULL) {
2963b68572e0SNeilBrown struct nfs_access_entry *entry =
2964b68572e0SNeilBrown rb_entry(n, struct nfs_access_entry, rb_node);
29656238aec8SNeilBrown int cmp = access_cmp(cred, entry);
29661c3c07e9STrond Myklebust
2967b68572e0SNeilBrown if (cmp < 0)
29681c3c07e9STrond Myklebust n = n->rb_left;
2969b68572e0SNeilBrown else if (cmp > 0)
29701c3c07e9STrond Myklebust n = n->rb_right;
29711c3c07e9STrond Myklebust else
29721c3c07e9STrond Myklebust return entry;
29731c3c07e9STrond Myklebust }
29741c3c07e9STrond Myklebust return NULL;
29751c3c07e9STrond Myklebust }
29761c3c07e9STrond Myklebust
nfs_access_login_time(const struct task_struct * task,const struct cred * cred)29770eb43812STrond Myklebust static u64 nfs_access_login_time(const struct task_struct *task,
29780eb43812STrond Myklebust const struct cred *cred)
29790eb43812STrond Myklebust {
29800eb43812STrond Myklebust const struct task_struct *parent;
29815e9a7b9cSTrond Myklebust const struct cred *pcred;
29820eb43812STrond Myklebust u64 ret;
29830eb43812STrond Myklebust
29840eb43812STrond Myklebust rcu_read_lock();
29850eb43812STrond Myklebust for (;;) {
29860eb43812STrond Myklebust parent = rcu_dereference(task->real_parent);
29874ffac001SScott Mayhew pcred = __task_cred(parent);
29885e9a7b9cSTrond Myklebust if (parent == task || cred_fscmp(pcred, cred) != 0)
29890eb43812STrond Myklebust break;
29900eb43812STrond Myklebust task = parent;
29910eb43812STrond Myklebust }
29920eb43812STrond Myklebust ret = task->start_time;
29930eb43812STrond Myklebust rcu_read_unlock();
29940eb43812STrond Myklebust return ret;
29950eb43812STrond Myklebust }
29960eb43812STrond Myklebust
nfs_access_get_cached_locked(struct inode * inode,const struct cred * cred,u32 * mask,bool may_block)2997b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
29981da177e4SLinus Torvalds {
299955296809SChuck Lever struct nfs_inode *nfsi = NFS_I(inode);
30000eb43812STrond Myklebust u64 login_time = nfs_access_login_time(current, cred);
30011c3c07e9STrond Myklebust struct nfs_access_entry *cache;
300257b69181STrond Myklebust bool retry = true;
300357b69181STrond Myklebust int err;
30041da177e4SLinus Torvalds
30051c3c07e9STrond Myklebust spin_lock(&inode->i_lock);
300657b69181STrond Myklebust for(;;) {
30071c3c07e9STrond Myklebust if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
30081c3c07e9STrond Myklebust goto out_zap;
30091c3c07e9STrond Myklebust cache = nfs_access_search_rbtree(inode, cred);
301057b69181STrond Myklebust err = -ENOENT;
30111c3c07e9STrond Myklebust if (cache == NULL)
30121c3c07e9STrond Myklebust goto out;
301357b69181STrond Myklebust /* Found an entry, is our attribute cache valid? */
301421c3ba7eSTrond Myklebust if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
301557b69181STrond Myklebust break;
30165c965db8STrond Myklebust if (!retry)
30175c965db8STrond Myklebust break;
301857b69181STrond Myklebust err = -ECHILD;
301957b69181STrond Myklebust if (!may_block)
302057b69181STrond Myklebust goto out;
302157b69181STrond Myklebust spin_unlock(&inode->i_lock);
302257b69181STrond Myklebust err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
302357b69181STrond Myklebust if (err)
302457b69181STrond Myklebust return err;
302557b69181STrond Myklebust spin_lock(&inode->i_lock);
302657b69181STrond Myklebust retry = false;
302757b69181STrond Myklebust }
30280eb43812STrond Myklebust err = -ENOENT;
30290eb43812STrond Myklebust if ((s64)(login_time - cache->timestamp) > 0)
30300eb43812STrond Myklebust goto out;
3031b5e7b59cSNeilBrown *mask = cache->mask;
3032cfcea3e8STrond Myklebust list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
30331c3c07e9STrond Myklebust err = 0;
30341c3c07e9STrond Myklebust out:
30351c3c07e9STrond Myklebust spin_unlock(&inode->i_lock);
30361c3c07e9STrond Myklebust return err;
30371c3c07e9STrond Myklebust out_zap:
30381a81bb8aSTrond Myklebust spin_unlock(&inode->i_lock);
30391a81bb8aSTrond Myklebust nfs_access_zap_cache(inode);
30401c3c07e9STrond Myklebust return -ENOENT;
30411c3c07e9STrond Myklebust }
30421c3c07e9STrond Myklebust
nfs_access_get_cached_rcu(struct inode * inode,const struct cred * cred,u32 * mask)3043b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
3044f682a398SNeilBrown {
3045f682a398SNeilBrown /* Only check the most recently returned cache entry,
3046f682a398SNeilBrown * but do it without locking.
3047f682a398SNeilBrown */
3048f682a398SNeilBrown struct nfs_inode *nfsi = NFS_I(inode);
3049029085b8SChengen Du u64 login_time = nfs_access_login_time(current, cred);
3050f682a398SNeilBrown struct nfs_access_entry *cache;
3051f682a398SNeilBrown int err = -ECHILD;
3052f682a398SNeilBrown struct list_head *lh;
3053f682a398SNeilBrown
3054f682a398SNeilBrown rcu_read_lock();
3055f682a398SNeilBrown if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
3056f682a398SNeilBrown goto out;
30579f01eb5dSMadhuparna Bhowmik lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
3058f682a398SNeilBrown cache = list_entry(lh, struct nfs_access_entry, lru);
3059f682a398SNeilBrown if (lh == &nfsi->access_cache_entry_lru ||
30606238aec8SNeilBrown access_cmp(cred, cache) != 0)
3061f682a398SNeilBrown cache = NULL;
3062f682a398SNeilBrown if (cache == NULL)
3063f682a398SNeilBrown goto out;
3064029085b8SChengen Du if ((s64)(login_time - cache->timestamp) > 0)
3065029085b8SChengen Du goto out;
306621c3ba7eSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
3067f682a398SNeilBrown goto out;
3068b5e7b59cSNeilBrown *mask = cache->mask;
306921c3ba7eSTrond Myklebust err = 0;
3070f682a398SNeilBrown out:
3071f682a398SNeilBrown rcu_read_unlock();
3072f682a398SNeilBrown return err;
3073f682a398SNeilBrown }
3074f682a398SNeilBrown
nfs_access_get_cached(struct inode * inode,const struct cred * cred,u32 * mask,bool may_block)3075b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
3076b5e7b59cSNeilBrown u32 *mask, bool may_block)
3077d2ae4f8bSFrank van der Linden {
3078d2ae4f8bSFrank van der Linden int status;
3079d2ae4f8bSFrank van der Linden
3080b5e7b59cSNeilBrown status = nfs_access_get_cached_rcu(inode, cred, mask);
3081d2ae4f8bSFrank van der Linden if (status != 0)
3082b5e7b59cSNeilBrown status = nfs_access_get_cached_locked(inode, cred, mask,
3083d2ae4f8bSFrank van der Linden may_block);
3084d2ae4f8bSFrank van der Linden
3085d2ae4f8bSFrank van der Linden return status;
3086d2ae4f8bSFrank van der Linden }
3087d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
3088d2ae4f8bSFrank van der Linden
nfs_access_add_rbtree(struct inode * inode,struct nfs_access_entry * set,const struct cred * cred)308973fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode,
309073fbb3faSNeilBrown struct nfs_access_entry *set,
309173fbb3faSNeilBrown const struct cred *cred)
30921c3c07e9STrond Myklebust {
3093cfcea3e8STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode);
3094cfcea3e8STrond Myklebust struct rb_root *root_node = &nfsi->access_cache;
30951c3c07e9STrond Myklebust struct rb_node **p = &root_node->rb_node;
30961c3c07e9STrond Myklebust struct rb_node *parent = NULL;
30971c3c07e9STrond Myklebust struct nfs_access_entry *entry;
3098b68572e0SNeilBrown int cmp;
30991c3c07e9STrond Myklebust
31001c3c07e9STrond Myklebust spin_lock(&inode->i_lock);
31011c3c07e9STrond Myklebust while (*p != NULL) {
31021c3c07e9STrond Myklebust parent = *p;
31031c3c07e9STrond Myklebust entry = rb_entry(parent, struct nfs_access_entry, rb_node);
31046238aec8SNeilBrown cmp = access_cmp(cred, entry);
31051c3c07e9STrond Myklebust
3106b68572e0SNeilBrown if (cmp < 0)
31071c3c07e9STrond Myklebust p = &parent->rb_left;
3108b68572e0SNeilBrown else if (cmp > 0)
31091c3c07e9STrond Myklebust p = &parent->rb_right;
31101c3c07e9STrond Myklebust else
31111c3c07e9STrond Myklebust goto found;
31121c3c07e9STrond Myklebust }
31131c3c07e9STrond Myklebust rb_link_node(&set->rb_node, parent, p);
31141c3c07e9STrond Myklebust rb_insert_color(&set->rb_node, root_node);
3115cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
31161c3c07e9STrond Myklebust spin_unlock(&inode->i_lock);
31171c3c07e9STrond Myklebust return;
31181c3c07e9STrond Myklebust found:
31191c3c07e9STrond Myklebust rb_replace_node(parent, &set->rb_node, root_node);
3120cfcea3e8STrond Myklebust list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
3121cfcea3e8STrond Myklebust list_del(&entry->lru);
31221c3c07e9STrond Myklebust spin_unlock(&inode->i_lock);
31231c3c07e9STrond Myklebust nfs_access_free_entry(entry);
31241da177e4SLinus Torvalds }
31251da177e4SLinus Torvalds
nfs_access_add_cache(struct inode * inode,struct nfs_access_entry * set,const struct cred * cred)312673fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
312773fbb3faSNeilBrown const struct cred *cred)
31281da177e4SLinus Torvalds {
31291c3c07e9STrond Myklebust struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
31301c3c07e9STrond Myklebust if (cache == NULL)
31311c3c07e9STrond Myklebust return;
31321c3c07e9STrond Myklebust RB_CLEAR_NODE(&cache->rb_node);
31336238aec8SNeilBrown cache->fsuid = cred->fsuid;
31346238aec8SNeilBrown cache->fsgid = cred->fsgid;
31356238aec8SNeilBrown cache->group_info = get_group_info(cred->group_info);
31361da177e4SLinus Torvalds cache->mask = set->mask;
313721fd9e87SChengen Du cache->timestamp = ktime_get_ns();
31381c3c07e9STrond Myklebust
3139f682a398SNeilBrown /* The above field assignments must be visible
3140f682a398SNeilBrown * before this item appears on the lru. We cannot easily
3141f682a398SNeilBrown * use rcu_assign_pointer, so just force the memory barrier.
3142f682a398SNeilBrown */
3143f682a398SNeilBrown smp_wmb();
314473fbb3faSNeilBrown nfs_access_add_rbtree(inode, cache, cred);
3145cfcea3e8STrond Myklebust
3146cfcea3e8STrond Myklebust /* Update accounting */
31474e857c58SPeter Zijlstra smp_mb__before_atomic();
3148cfcea3e8STrond Myklebust atomic_long_inc(&nfs_access_nr_entries);
31494e857c58SPeter Zijlstra smp_mb__after_atomic();
3150cfcea3e8STrond Myklebust
3151cfcea3e8STrond Myklebust /* Add inode to global LRU list */
31521a81bb8aSTrond Myklebust if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
3153cfcea3e8STrond Myklebust spin_lock(&nfs_access_lru_lock);
31541a81bb8aSTrond Myklebust if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
31551a81bb8aSTrond Myklebust list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
31561a81bb8aSTrond Myklebust &nfs_access_lru_list);
3157cfcea3e8STrond Myklebust spin_unlock(&nfs_access_lru_lock);
3158cfcea3e8STrond Myklebust }
31593a505845STrond Myklebust nfs_access_cache_enforce_limit();
31601da177e4SLinus Torvalds }
31616168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
31626168f62cSWeston Andros Adamson
31633c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
31643c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
31653c181827SAnna Schumaker NFS_ACCESS_EXTEND | \
31663c181827SAnna Schumaker NFS_ACCESS_DELETE)
31673c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
31683c181827SAnna Schumaker NFS_ACCESS_EXTEND)
3169ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
31703c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
31713c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
317215d4b73aSTrond Myklebust static int
nfs_access_calc_mask(u32 access_result,umode_t umode)3173ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
317415d4b73aSTrond Myklebust {
317515d4b73aSTrond Myklebust int mask = 0;
317615d4b73aSTrond Myklebust
317715d4b73aSTrond Myklebust if (access_result & NFS_MAY_READ)
317815d4b73aSTrond Myklebust mask |= MAY_READ;
3179ecbb903cSTrond Myklebust if (S_ISDIR(umode)) {
3180ecbb903cSTrond Myklebust if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
318115d4b73aSTrond Myklebust mask |= MAY_WRITE;
3182ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
318315d4b73aSTrond Myklebust mask |= MAY_EXEC;
3184ecbb903cSTrond Myklebust } else if (S_ISREG(umode)) {
3185ecbb903cSTrond Myklebust if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
3186ecbb903cSTrond Myklebust mask |= MAY_WRITE;
3187ecbb903cSTrond Myklebust if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
318815d4b73aSTrond Myklebust mask |= MAY_EXEC;
3189ecbb903cSTrond Myklebust } else if (access_result & NFS_MAY_WRITE)
3190ecbb903cSTrond Myklebust mask |= MAY_WRITE;
319115d4b73aSTrond Myklebust return mask;
319215d4b73aSTrond Myklebust }
319315d4b73aSTrond Myklebust
nfs_access_set_mask(struct nfs_access_entry * entry,u32 access_result)31946168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
31956168f62cSWeston Andros Adamson {
3196bd8b2441STrond Myklebust entry->mask = access_result;
31976168f62cSWeston Andros Adamson }
31986168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
31991da177e4SLinus Torvalds
nfs_do_access(struct inode * inode,const struct cred * cred,int mask)3200b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
32011da177e4SLinus Torvalds {
32021da177e4SLinus Torvalds struct nfs_access_entry cache;
320357b69181STrond Myklebust bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3204e8194b7dSTrond Myklebust int cache_mask = -1;
32051da177e4SLinus Torvalds int status;
32061da177e4SLinus Torvalds
3207f4ce1299STrond Myklebust trace_nfs_access_enter(inode);
3208f4ce1299STrond Myklebust
3209b5e7b59cSNeilBrown status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
32101da177e4SLinus Torvalds if (status == 0)
3211f4ce1299STrond Myklebust goto out_cached;
32121da177e4SLinus Torvalds
3213f3324a2aSNeilBrown status = -ECHILD;
321457b69181STrond Myklebust if (!may_block)
3215f3324a2aSNeilBrown goto out;
3216f3324a2aSNeilBrown
32171750d929SAnna Schumaker /*
32181750d929SAnna Schumaker * Determine which access bits we want to ask for...
32191750d929SAnna Schumaker */
322084631f84STrond Myklebust cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
322184631f84STrond Myklebust nfs_access_xattr_mask(NFS_SERVER(inode));
32221750d929SAnna Schumaker if (S_ISDIR(inode->i_mode))
32231750d929SAnna Schumaker cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
32241750d929SAnna Schumaker else
32251750d929SAnna Schumaker cache.mask |= NFS_ACCESS_EXECUTE;
322673fbb3faSNeilBrown status = NFS_PROTO(inode)->access(inode, &cache, cred);
3227a71ee337SSuresh Jayaraman if (status != 0) {
3228a71ee337SSuresh Jayaraman if (status == -ESTALE) {
3229a71ee337SSuresh Jayaraman if (!S_ISDIR(inode->i_mode))
323093ce4af7STrond Myklebust nfs_set_inode_stale(inode);
323193ce4af7STrond Myklebust else
323293ce4af7STrond Myklebust nfs_zap_caches(inode);
3233a71ee337SSuresh Jayaraman }
3234f4ce1299STrond Myklebust goto out;
3235a71ee337SSuresh Jayaraman }
323673fbb3faSNeilBrown nfs_access_add_cache(inode, &cache, cred);
3237f4ce1299STrond Myklebust out_cached:
3238ecbb903cSTrond Myklebust cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3239bd8b2441STrond Myklebust if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3240f4ce1299STrond Myklebust status = -EACCES;
32411da177e4SLinus Torvalds out:
3242e8194b7dSTrond Myklebust trace_nfs_access_exit(inode, mask, cache_mask, status);
3243f4ce1299STrond Myklebust return status;
32441da177e4SLinus Torvalds }
32451da177e4SLinus Torvalds
nfs_open_permission_mask(int openflags)3246af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
3247af22f94aSTrond Myklebust {
3248af22f94aSTrond Myklebust int mask = 0;
3249af22f94aSTrond Myklebust
3250f8d9a897SWeston Andros Adamson if (openflags & __FMODE_EXEC) {
3251f8d9a897SWeston Andros Adamson /* ONLY check exec rights */
3252f8d9a897SWeston Andros Adamson mask = MAY_EXEC;
3253f8d9a897SWeston Andros Adamson } else {
32548a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_WRONLY)
3255af22f94aSTrond Myklebust mask |= MAY_READ;
32568a5e929dSAl Viro if ((openflags & O_ACCMODE) != O_RDONLY)
3257af22f94aSTrond Myklebust mask |= MAY_WRITE;
3258f8d9a897SWeston Andros Adamson }
3259f8d9a897SWeston Andros Adamson
3260af22f94aSTrond Myklebust return mask;
3261af22f94aSTrond Myklebust }
3262af22f94aSTrond Myklebust
nfs_may_open(struct inode * inode,const struct cred * cred,int openflags)3263b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3264af22f94aSTrond Myklebust {
3265af22f94aSTrond Myklebust return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3266af22f94aSTrond Myklebust }
326789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
3268af22f94aSTrond Myklebust
nfs_execute_ok(struct inode * inode,int mask)32695c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
32705c5fc09aSTrond Myklebust {
32715c5fc09aSTrond Myklebust struct nfs_server *server = NFS_SERVER(inode);
327221c3ba7eSTrond Myklebust int ret = 0;
32735c5fc09aSTrond Myklebust
32743825827eSTrond Myklebust if (S_ISDIR(inode->i_mode))
32753825827eSTrond Myklebust return 0;
3276720869ebSTrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
32775c5fc09aSTrond Myklebust if (mask & MAY_NOT_BLOCK)
327821c3ba7eSTrond Myklebust return -ECHILD;
327921c3ba7eSTrond Myklebust ret = __nfs_revalidate_inode(server, inode);
328021c3ba7eSTrond Myklebust }
32815c5fc09aSTrond Myklebust if (ret == 0 && !execute_ok(inode))
32825c5fc09aSTrond Myklebust ret = -EACCES;
32835c5fc09aSTrond Myklebust return ret;
32845c5fc09aSTrond Myklebust }
32855c5fc09aSTrond Myklebust
nfs_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)32864609e1f1SChristian Brauner int nfs_permission(struct mnt_idmap *idmap,
3287549c7297SChristian Brauner struct inode *inode,
3288549c7297SChristian Brauner int mask)
32891da177e4SLinus Torvalds {
3290b68572e0SNeilBrown const struct cred *cred = current_cred();
32911da177e4SLinus Torvalds int res = 0;
32921da177e4SLinus Torvalds
329391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSACCESS);
329491d5b470SChuck Lever
3295e6305c43SAl Viro if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
32961da177e4SLinus Torvalds goto out;
32971da177e4SLinus Torvalds /* Is this sys_access() ? */
32989cfcac81SEric Paris if (mask & (MAY_ACCESS | MAY_CHDIR))
32991da177e4SLinus Torvalds goto force_lookup;
33001da177e4SLinus Torvalds
33011da177e4SLinus Torvalds switch (inode->i_mode & S_IFMT) {
33021da177e4SLinus Torvalds case S_IFLNK:
33031da177e4SLinus Torvalds goto out;
33041da177e4SLinus Torvalds case S_IFREG:
3305762674f8STrond Myklebust if ((mask & MAY_OPEN) &&
3306762674f8STrond Myklebust nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3307762674f8STrond Myklebust return 0;
33081da177e4SLinus Torvalds break;
33091da177e4SLinus Torvalds case S_IFDIR:
33101da177e4SLinus Torvalds /*
33111da177e4SLinus Torvalds * Optimize away all write operations, since the server
33121da177e4SLinus Torvalds * will check permissions when we perform the op.
33131da177e4SLinus Torvalds */
33141da177e4SLinus Torvalds if ((mask & MAY_WRITE) && !(mask & MAY_READ))
33151da177e4SLinus Torvalds goto out;
33161da177e4SLinus Torvalds }
33171da177e4SLinus Torvalds
33181da177e4SLinus Torvalds force_lookup:
33191da177e4SLinus Torvalds if (!NFS_PROTO(inode)->access)
33201da177e4SLinus Torvalds goto out_notsup;
33211da177e4SLinus Torvalds
33221da177e4SLinus Torvalds res = nfs_do_access(inode, cred, mask);
33231da177e4SLinus Torvalds out:
33245c5fc09aSTrond Myklebust if (!res && (mask & MAY_EXEC))
33255c5fc09aSTrond Myklebust res = nfs_execute_ok(inode, mask);
3326f696a365SMiklos Szeredi
33271e8968c5SNiels de Vos dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
33281e7cb3dcSChuck Lever inode->i_sb->s_id, inode->i_ino, mask, res);
33291da177e4SLinus Torvalds return res;
33301da177e4SLinus Torvalds out_notsup:
3331d51ac1a8SNeilBrown if (mask & MAY_NOT_BLOCK)
3332d51ac1a8SNeilBrown return -ECHILD;
3333d51ac1a8SNeilBrown
3334720869ebSTrond Myklebust res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3335720869ebSTrond Myklebust NFS_INO_INVALID_OTHER);
33361da177e4SLinus Torvalds if (res == 0)
33374609e1f1SChristian Brauner res = generic_permission(&nop_mnt_idmap, inode, mask);
33381e7cb3dcSChuck Lever goto out;
33391da177e4SLinus Torvalds }
3340ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
3341