blob: 7bc7cf6b26f091d95851d2286f63865a2129fa10 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/nfs/dir.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
6 *
7 * nfs directory handling functions
8 *
9 * 10 Apr 1996 Added silly rename for unlink --okir
10 * 28 Sep 1996 Improved directory cache --okir
11 * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
12 * Re-implemented silly rename for unlink, newly implemented
13 * silly rename for nfs_rename() following the suggestions
14 * of Olaf Kirch (okir) found in this file.
15 * Following Linus comments on my original hack, this version
16 * depends only on the dcache stuff and doesn't touch the inode
17 * layer (iput() and friends).
18 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
19 */
20
Jakub Kicinskib6459412021-12-28 16:49:13 -080021#include <linux/compat.h>
Bryan Schumakerddda8e02012-07-30 16:05:23 -040022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/time.h>
24#include <linux/errno.h>
25#include <linux/stat.h>
26#include <linux/fcntl.h>
27#include <linux/string.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/sunrpc/clnt.h>
32#include <linux/nfs_fs.h>
33#include <linux/nfs_mount.h>
34#include <linux/pagemap.h>
Chuck Lever873101b2006-08-22 20:06:23 -040035#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/namei.h>
David Howells54ceac42006-08-22 20:06:13 -040037#include <linux/mount.h>
Mel Gormana0b8cab32013-07-03 15:02:32 -070038#include <linux/swap.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040039#include <linux/sched.h>
Catalin Marinas04e4bd12010-11-11 12:53:47 +000040#include <linux/kmemleak.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000041#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050044#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080045#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040046#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Trond Myklebustf4ce1292013-08-19 18:59:33 -040048#include "nfstrace.h"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* #define NFS_DEBUG_VERBOSE 1 */
51
52static int nfs_opendir(struct inode *, struct file *);
Bryan Schumaker480c2002011-03-23 14:48:29 -040053static int nfs_closedir(struct inode *, struct file *);
Al Viro23db8622013-05-17 16:34:50 -040054static int nfs_readdir(struct file *, struct dir_context *);
Josef Bacik02c24a82011-07-16 20:44:56 -040055static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000056static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Trond Myklebust11de3b12010-12-01 14:17:06 -050057static void nfs_readdir_clear_array(struct page*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080059const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000060 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .read = generic_read_dir,
Trond Myklebust93a6ab72020-02-02 17:53:56 -050062 .iterate_shared = nfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .open = nfs_opendir,
Bryan Schumaker480c2002011-03-23 14:48:29 -040064 .release = nfs_closedir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .fsync = nfs_fsync_dir,
66};
67
Trond Myklebust11de3b12010-12-01 14:17:06 -050068const struct address_space_operations nfs_dir_aops = {
69 .freepage = nfs_readdir_clear_array,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040070};
71
Trond Myklebust93b89592020-11-01 15:24:41 -050072static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir)
Bryan Schumaker480c2002011-03-23 14:48:29 -040073{
Trond Myklebust311324a2014-02-07 17:02:08 -050074 struct nfs_inode *nfsi = NFS_I(dir);
Bryan Schumaker480c2002011-03-23 14:48:29 -040075 struct nfs_open_dir_context *ctx;
76 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
77 if (ctx != NULL) {
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -040078 ctx->duped = 0;
Trond Myklebust311324a2014-02-07 17:02:08 -050079 ctx->attr_gencount = nfsi->attr_gencount;
Bryan Schumaker480c2002011-03-23 14:48:29 -040080 ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -040081 ctx->dup_cookie = 0;
Trond Myklebustff81dfb2021-09-28 14:33:44 -040082 ctx->page_index = 0;
Trond Myklebuste1d26992022-01-18 22:10:52 -050083 ctx->eof = false;
Trond Myklebust311324a2014-02-07 17:02:08 -050084 spin_lock(&dir->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -040085 if (list_empty(&nfsi->open_files) &&
86 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
Trond Myklebustac46b3d2021-03-08 14:42:54 -050087 nfs_set_cache_invalid(dir,
88 NFS_INO_INVALID_DATA |
89 NFS_INO_REVAL_FORCED);
Trond Myklebust311324a2014-02-07 17:02:08 -050090 list_add(&ctx->list, &nfsi->open_files);
Trond Myklebustff81dfb2021-09-28 14:33:44 -040091 clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
Trond Myklebust311324a2014-02-07 17:02:08 -050092 spin_unlock(&dir->i_lock);
Trond Myklebust0c030802011-07-30 12:45:35 -040093 return ctx;
94 }
95 return ERR_PTR(-ENOMEM);
Bryan Schumaker480c2002011-03-23 14:48:29 -040096}
97
Trond Myklebust311324a2014-02-07 17:02:08 -050098static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
Bryan Schumaker480c2002011-03-23 14:48:29 -040099{
Trond Myklebust311324a2014-02-07 17:02:08 -0500100 spin_lock(&dir->i_lock);
101 list_del(&ctx->list);
102 spin_unlock(&dir->i_lock);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400103 kfree(ctx);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*
107 * Open file
108 */
109static int
110nfs_opendir(struct inode *inode, struct file *filp)
111{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400112 int res = 0;
113 struct nfs_open_dir_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Al Viro6de14722013-09-16 10:53:17 -0400115 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400116
117 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500118
Trond Myklebust93b89592020-11-01 15:24:41 -0500119 ctx = alloc_nfs_open_dir_context(inode);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400120 if (IS_ERR(ctx)) {
121 res = PTR_ERR(ctx);
122 goto out;
123 }
124 filp->private_data = ctx;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400125out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return res;
127}
128
Bryan Schumaker480c2002011-03-23 14:48:29 -0400129static int
130nfs_closedir(struct inode *inode, struct file *filp)
131{
Al Viroa4555892014-10-21 20:11:25 -0400132 put_nfs_open_dir_context(file_inode(filp), filp->private_data);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400133 return 0;
134}
135
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400136struct nfs_cache_array_entry {
137 u64 cookie;
138 u64 ino;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500139 const char *name;
140 unsigned int name_len;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500141 unsigned char d_type;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400142};
143
144struct nfs_cache_array {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400145 u64 last_cookie;
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500146 unsigned int size;
147 unsigned char page_full : 1,
Trond Myklebust762567b2020-11-04 08:32:19 -0500148 page_is_eof : 1,
149 cookies_are_ordered : 1;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -0500150 struct nfs_cache_array_entry array[];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400151};
152
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500153struct nfs_readdir_descriptor {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct file *file;
155 struct page *page;
Al Viro23db8622013-05-17 16:34:50 -0400156 struct dir_context *ctx;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500157 pgoff_t page_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500158 u64 dir_cookie;
Trond Myklebust0aded702010-11-30 21:56:32 -0500159 u64 last_cookie;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500160 u64 dup_cookie;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000161 loff_t current_index;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500162 loff_t prev_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400163
Trond Myklebustb593c092020-11-02 20:06:12 -0500164 __be32 verf[NFS_DIR_VERIFIER_SIZE];
Trond Myklebusta1147b82020-02-05 09:01:52 -0500165 unsigned long dir_verifier;
Neil Brown1f4eab72007-04-16 09:35:27 +1000166 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400167 unsigned long gencount;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500168 unsigned long attr_gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400169 unsigned int cache_entry_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500170 signed char duped;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400171 bool plus;
Trond Myklebuste1d26992022-01-18 22:10:52 -0500172 bool eob;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400173 bool eof;
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500176static void nfs_readdir_array_init(struct nfs_cache_array *array)
177{
178 memset(array, 0, sizeof(struct nfs_cache_array));
179}
180
181static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie)
Trond Myklebust4b310312020-02-02 17:53:53 -0500182{
183 struct nfs_cache_array *array;
184
185 array = kmap_atomic(page);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500186 nfs_readdir_array_init(array);
187 array->last_cookie = last_cookie;
Trond Myklebust762567b2020-11-04 08:32:19 -0500188 array->cookies_are_ordered = 1;
Trond Myklebust4b310312020-02-02 17:53:53 -0500189 kunmap_atomic(array);
190}
191
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400192/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400193 * we are freeing strings created by nfs_add_to_readdir_array()
194 */
195static
Trond Myklebust11de3b12010-12-01 14:17:06 -0500196void nfs_readdir_clear_array(struct page *page)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400197{
Trond Myklebust11de3b12010-12-01 14:17:06 -0500198 struct nfs_cache_array *array;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400199 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500200
Cong Wang2b86ce22011-11-25 23:14:33 +0800201 array = kmap_atomic(page);
Benjamin Coddingtonb044f642017-03-10 17:07:46 -0500202 for (i = 0; i < array->size; i++)
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500203 kfree(array->array[i].name);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500204 nfs_readdir_array_init(array);
Cong Wang2b86ce22011-11-25 23:14:33 +0800205 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400206}
207
Trond Myklebust35df59d2020-11-06 20:38:47 -0500208static struct page *
209nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
210{
211 struct page *page = alloc_page(gfp_flags);
212 if (page)
213 nfs_readdir_page_init_array(page, last_cookie);
214 return page;
215}
216
217static void nfs_readdir_page_array_free(struct page *page)
218{
219 if (page) {
220 nfs_readdir_clear_array(page);
221 put_page(page);
222 }
223}
224
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500225static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
226{
227 array->page_is_eof = 1;
228 array->page_full = 1;
229}
230
231static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
232{
233 return array->page_full;
234}
235
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400236/*
237 * the caller is responsible for freeing qstr.name
238 * when called by nfs_readdir_add_to_array, the strings will be freed in
239 * nfs_clear_readdir_array()
240 */
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500241static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400242{
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500243 const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
244
Catalin Marinas04e4bd12010-11-11 12:53:47 +0000245 /*
246 * Avoid a kmemleak false positive. The pointer to the name is stored
247 * in a page cache page which kmemleak does not scan.
248 */
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500249 if (ret != NULL)
250 kmemleak_not_leak(ret);
251 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400252}
253
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500254/*
255 * Check that the next array entry lies entirely within the page bounds
256 */
257static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
258{
259 struct nfs_cache_array_entry *cache_entry;
260
261 if (array->page_full)
262 return -ENOSPC;
263 cache_entry = &array->array[array->size + 1];
264 if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
265 array->page_full = 1;
266 return -ENOSPC;
267 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400268 return 0;
269}
270
271static
272int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
273{
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500274 struct nfs_cache_array *array;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400275 struct nfs_cache_array_entry *cache_entry;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500276 const char *name;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400277 int ret;
278
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500279 name = nfs_readdir_copy_name(entry->name, entry->len);
280 if (!name)
281 return -ENOMEM;
Trond Myklebust30200932010-11-20 15:18:22 -0500282
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500283 array = kmap_atomic(page);
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500284 ret = nfs_readdir_array_can_expand(array);
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500285 if (ret) {
286 kfree(name);
Trond Myklebust30200932010-11-20 15:18:22 -0500287 goto out;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500288 }
Trond Myklebust30200932010-11-20 15:18:22 -0500289
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500290 cache_entry = &array->array[array->size];
Trond Myklebust4a201d62010-10-23 14:53:23 -0400291 cache_entry->cookie = entry->prev_cookie;
292 cache_entry->ino = entry->ino;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500293 cache_entry->d_type = entry->d_type;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500294 cache_entry->name_len = entry->len;
295 cache_entry->name = name;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400296 array->last_cookie = entry->cookie;
Trond Myklebust762567b2020-11-04 08:32:19 -0500297 if (array->last_cookie <= cache_entry->cookie)
298 array->cookies_are_ordered = 0;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500299 array->size++;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500300 if (entry->eof != 0)
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500301 nfs_readdir_array_set_eof(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400302out:
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500303 kunmap_atomic(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400304 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400305}
306
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500307static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
308 pgoff_t index, u64 last_cookie)
309{
310 struct page *page;
311
312 page = grab_cache_page(mapping, index);
313 if (page && !PageUptodate(page)) {
314 nfs_readdir_page_init_array(page, last_cookie);
315 if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
316 nfs_zap_mapping(mapping->host, mapping);
317 SetPageUptodate(page);
318 }
319
320 return page;
321}
322
323static u64 nfs_readdir_page_last_cookie(struct page *page)
324{
325 struct nfs_cache_array *array;
326 u64 ret;
327
328 array = kmap_atomic(page);
329 ret = array->last_cookie;
330 kunmap_atomic(array);
331 return ret;
332}
333
334static bool nfs_readdir_page_needs_filling(struct page *page)
335{
336 struct nfs_cache_array *array;
337 bool ret;
338
339 array = kmap_atomic(page);
340 ret = !nfs_readdir_array_is_full(array);
341 kunmap_atomic(array);
342 return ret;
343}
344
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500345static void nfs_readdir_page_set_eof(struct page *page)
346{
347 struct nfs_cache_array *array;
348
349 array = kmap_atomic(page);
350 nfs_readdir_array_set_eof(array);
351 kunmap_atomic(array);
352}
353
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500354static void nfs_readdir_page_unlock_and_put(struct page *page)
355{
356 unlock_page(page);
357 put_page(page);
358}
359
360static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
361 pgoff_t index, u64 cookie)
362{
363 struct page *page;
364
365 page = nfs_readdir_page_get_locked(mapping, index, cookie);
366 if (page) {
367 if (nfs_readdir_page_last_cookie(page) == cookie)
368 return page;
369 nfs_readdir_page_unlock_and_put(page);
370 }
371 return NULL;
372}
373
Trond Myklebust59e356a2020-02-03 14:49:33 -0500374static inline
375int is_32bit_api(void)
376{
377#ifdef CONFIG_COMPAT
378 return in_compat_syscall();
379#else
380 return (BITS_PER_LONG == 32);
381#endif
382}
383
384static
385bool nfs_readdir_use_cookie(const struct file *filp)
386{
387 if ((filp->f_mode & FMODE_32BITHASH) ||
388 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
389 return false;
390 return true;
391}
392
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500393static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
394 struct nfs_readdir_descriptor *desc)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400395{
Al Viro23db8622013-05-17 16:34:50 -0400396 loff_t diff = desc->ctx->pos - desc->current_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400397 unsigned int index;
398
399 if (diff < 0)
400 goto out_eof;
401 if (diff >= array->size) {
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500402 if (array->page_is_eof)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400403 goto out_eof;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400404 return -EAGAIN;
405 }
406
407 index = (unsigned int)diff;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500408 desc->dir_cookie = array->array[index].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400409 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400410 return 0;
411out_eof:
Thomas Meyer6089dd02017-10-07 16:02:21 +0200412 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400413 return -EBADCOOKIE;
414}
415
Jeff Layton4db72b42014-01-28 13:47:46 -0500416static bool
417nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
418{
Trond Myklebust2929bc32021-09-28 12:37:05 -0400419 if (nfsi->cache_validity & (NFS_INO_INVALID_CHANGE |
420 NFS_INO_INVALID_DATA))
Jeff Layton4db72b42014-01-28 13:47:46 -0500421 return false;
422 smp_rmb();
423 return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
424}
425
Trond Myklebust762567b2020-11-04 08:32:19 -0500426static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
427 u64 cookie)
428{
429 if (!array->cookies_are_ordered)
430 return true;
431 /* Optimisation for monotonically increasing cookies */
432 if (cookie >= array->last_cookie)
433 return false;
434 if (array->size && cookie < array->array[0].cookie)
435 return false;
436 return true;
437}
438
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500439static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
440 struct nfs_readdir_descriptor *desc)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400441{
442 int i;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400443 loff_t new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400444 int status = -EAGAIN;
445
Trond Myklebust762567b2020-11-04 08:32:19 -0500446 if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
447 goto check_eof;
448
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400449 for (i = 0; i < array->size; i++) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500450 if (array->array[i].cookie == desc->dir_cookie) {
Al Viro496ad9a2013-01-23 17:07:38 -0500451 struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
Trond Myklebust0c030802011-07-30 12:45:35 -0400452
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400453 new_pos = desc->current_index + i;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500454 if (desc->attr_gencount != nfsi->attr_gencount ||
Jeff Layton4db72b42014-01-28 13:47:46 -0500455 !nfs_readdir_inode_mapping_valid(nfsi)) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500456 desc->duped = 0;
457 desc->attr_gencount = nfsi->attr_gencount;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500458 } else if (new_pos < desc->prev_index) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500459 if (desc->duped > 0
460 && desc->dup_cookie == desc->dir_cookie) {
Trond Myklebust0c030802011-07-30 12:45:35 -0400461 if (printk_ratelimit()) {
Al Viro6de14722013-09-16 10:53:17 -0400462 pr_notice("NFS: directory %pD2 contains a readdir loop."
Trond Myklebust0c030802011-07-30 12:45:35 -0400463 "Please contact your server vendor. "
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500464 "The file: %s has duplicate cookie %llu\n",
465 desc->file, array->array[i].name, desc->dir_cookie);
Trond Myklebust0c030802011-07-30 12:45:35 -0400466 }
467 status = -ELOOP;
468 goto out;
469 }
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500470 desc->dup_cookie = desc->dir_cookie;
471 desc->duped = -1;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400472 }
Trond Myklebust59e356a2020-02-03 14:49:33 -0500473 if (nfs_readdir_use_cookie(desc->file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500474 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500475 else
476 desc->ctx->pos = new_pos;
477 desc->prev_index = new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400478 desc->cache_entry_index = i;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500479 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400480 }
481 }
Trond Myklebust762567b2020-11-04 08:32:19 -0500482check_eof:
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500483 if (array->page_is_eof) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500484 status = -EBADCOOKIE;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500485 if (desc->dir_cookie == array->last_cookie)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200486 desc->eof = true;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500487 }
Trond Myklebust0c030802011-07-30 12:45:35 -0400488out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400489 return status;
490}
491
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500492static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400493{
494 struct nfs_cache_array *array;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500495 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400496
Trond Myklebusted092222020-11-01 13:34:32 -0500497 array = kmap_atomic(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400498
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500499 if (desc->dir_cookie == 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400500 status = nfs_readdir_search_for_pos(array, desc);
501 else
502 status = nfs_readdir_search_for_cookie(array, desc);
503
Trond Myklebust47c716c2010-12-07 12:44:56 -0500504 if (status == -EAGAIN) {
Trond Myklebust0aded702010-11-30 21:56:32 -0500505 desc->last_cookie = array->last_cookie;
Trond Myklebuste47c0852011-03-23 08:43:09 -0400506 desc->current_index += array->size;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500507 desc->page_index++;
508 }
Trond Myklebusted092222020-11-01 13:34:32 -0500509 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400510 return status;
511}
512
513/* Fill a page with xdr information before transferring to the cache page */
Trond Myklebust93b89592020-11-01 15:24:41 -0500514static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
Trond Myklebustb593c092020-11-02 20:06:12 -0500515 __be32 *verf, u64 cookie,
516 struct page **pages, size_t bufsize,
517 __be32 *verf_res)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400518{
Trond Myklebust82e22a52020-11-02 17:34:23 -0500519 struct inode *inode = file_inode(desc->file);
Trond Myklebust82e22a52020-11-02 17:34:23 -0500520 struct nfs_readdir_arg arg = {
521 .dentry = file_dentry(desc->file),
522 .cred = desc->file->f_cred,
Trond Myklebustb593c092020-11-02 20:06:12 -0500523 .verf = verf,
Trond Myklebust82e22a52020-11-02 17:34:23 -0500524 .cookie = cookie,
525 .pages = pages,
526 .page_len = bufsize,
527 .plus = desc->plus,
528 };
529 struct nfs_readdir_res res = {
530 .verf = verf_res,
531 };
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400532 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int error;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 again:
536 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400537 gencount = nfs_inc_attr_generation_counter();
Trond Myklebusta1147b82020-02-05 09:01:52 -0500538 desc->dir_verifier = nfs_save_change_attribute(inode);
Trond Myklebust82e22a52020-11-02 17:34:23 -0500539 error = NFS_PROTO(inode)->readdir(&arg, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (error < 0) {
541 /* We requested READDIRPLUS, but the server doesn't grok it */
542 if (error == -ENOTSUPP && desc->plus) {
543 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200544 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Trond Myklebust82e22a52020-11-02 17:34:23 -0500545 desc->plus = arg.plus = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto again;
547 }
548 goto error;
549 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000550 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400551 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400552error:
553 return error;
554}
555
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500556static int xdr_decode(struct nfs_readdir_descriptor *desc,
Chuck Lever573c4e12010-12-14 14:58:11 +0000557 struct nfs_entry *entry, struct xdr_stream *xdr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400558{
Trond Myklebust59e356a2020-02-03 14:49:33 -0500559 struct inode *inode = file_inode(desc->file);
Chuck Lever573c4e12010-12-14 14:58:11 +0000560 int error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400561
Trond Myklebust59e356a2020-02-03 14:49:33 -0500562 error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
Chuck Lever573c4e12010-12-14 14:58:11 +0000563 if (error)
564 return error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400565 entry->fattr->time_start = desc->timestamp;
566 entry->fattr->gencount = desc->gencount;
567 return 0;
568}
569
Trond Myklebustfa923362015-02-23 18:51:32 -0500570/* Match file and dirent using either filehandle or fileid
571 * Note: caller is responsible for checking the fsid
572 */
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400573static
574int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
575{
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400576 struct inode *inode;
Trond Myklebustfa923362015-02-23 18:51:32 -0500577 struct nfs_inode *nfsi;
578
David Howells2b0143b2015-03-17 22:25:59 +0000579 if (d_really_is_negative(dentry))
580 return 0;
Trond Myklebustfa923362015-02-23 18:51:32 -0500581
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400582 inode = d_inode(dentry);
583 if (is_bad_inode(inode) || NFS_STALE(inode))
584 return 0;
585
586 nfsi = NFS_I(inode);
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400587 if (entry->fattr->fileid != nfsi->fileid)
588 return 0;
589 if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
590 return 0;
591 return 1;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400592}
593
594static
Al Viro23db8622013-05-17 16:34:50 -0400595bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400596{
597 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
598 return false;
599 if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
600 return true;
Al Viro23db8622013-05-17 16:34:50 -0400601 if (ctx->pos == 0)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400602 return true;
603 return false;
604}
605
606/*
Trond Myklebust63519fb2016-11-19 11:21:54 -0500607 * This function is called by the lookup and getattr code to request the
608 * use of readdirplus to accelerate any future lookups in the same
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400609 * directory.
610 */
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400611void nfs_advise_use_readdirplus(struct inode *dir)
612{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500613 struct nfs_inode *nfsi = NFS_I(dir);
614
615 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
616 !list_empty(&nfsi->open_files))
617 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400618}
619
Trond Myklebust311324a2014-02-07 17:02:08 -0500620/*
621 * This function is mainly for use by nfs_getattr().
622 *
623 * If this is an 'ls -l', we want to force use of readdirplus.
624 * Do this by checking if there is an active file descriptor
625 * and calling nfs_advise_use_readdirplus, then forcing a
626 * cache flush.
627 */
628void nfs_force_use_readdirplus(struct inode *dir)
629{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500630 struct nfs_inode *nfsi = NFS_I(dir);
631
632 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
633 !list_empty(&nfsi->open_files)) {
634 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Trond Myklebustff81dfb2021-09-28 14:33:44 -0400635 set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
Trond Myklebust311324a2014-02-07 17:02:08 -0500636 }
637}
638
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400639static
Trond Myklebusta1147b82020-02-05 09:01:52 -0500640void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
641 unsigned long dir_verifier)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400642{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700643 struct qstr filename = QSTR_INIT(entry->name, entry->len);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400644 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400645 struct dentry *dentry;
646 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400647 struct inode *inode;
David Quigleyaa9c2662013-05-22 12:50:44 -0400648 int status;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400649
Trond Myklebustfa923362015-02-23 18:51:32 -0500650 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
651 return;
Trond Myklebust6c441c22015-02-22 16:35:36 -0500652 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
653 return;
Trond Myklebust78d04af2016-09-20 14:34:24 -0400654 if (filename.len == 0)
655 return;
656 /* Validate that the name doesn't contain any illegal '\0' */
657 if (strnlen(filename.name, filename.len) != filename.len)
658 return;
659 /* ...or '/' */
660 if (strnchr(filename.name, filename.len, '/'))
661 return;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400662 if (filename.name[0] == '.') {
663 if (filename.len == 1)
664 return;
665 if (filename.len == 2 && filename.name[1] == '.')
666 return;
667 }
Linus Torvalds8387ff22016-06-10 07:51:30 -0700668 filename.hash = full_name_hash(parent, filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400669
Trond Myklebust4a201d62010-10-23 14:53:23 -0400670 dentry = d_lookup(parent, &filename);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400671again:
672 if (!dentry) {
673 dentry = d_alloc_parallel(parent, &filename, &wq);
674 if (IS_ERR(dentry))
675 return;
676 }
677 if (!d_in_lookup(dentry)) {
Trond Myklebust6c441c22015-02-22 16:35:36 -0500678 /* Is there a mountpoint here? If so, just exit */
679 if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
680 &entry->fattr->fsid))
681 goto out;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400682 if (nfs_same_file(dentry, entry)) {
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400683 if (!entry->fh->size)
684 goto out;
Trond Myklebusta1147b82020-02-05 09:01:52 -0500685 nfs_set_verifier(dentry, dir_verifier);
David Howells2b0143b2015-03-17 22:25:59 +0000686 status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -0400687 if (!status)
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400688 nfs_setsecurity(d_inode(dentry), entry->fattr);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400689 goto out;
690 } else {
Eric W. Biederman5542aa22014-02-13 09:46:25 -0800691 d_invalidate(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400692 dput(dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400693 dentry = NULL;
694 goto again;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400695 }
696 }
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400697 if (!entry->fh->size) {
698 d_lookup_done(dentry);
699 goto out;
700 }
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400701
Anna Schumakercf7ab002021-10-22 13:11:11 -0400702 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
Al Viro41d28bc2014-10-12 22:24:21 -0400703 alias = d_splice_alias(inode, dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400704 d_lookup_done(dentry);
705 if (alias) {
706 if (IS_ERR(alias))
707 goto out;
708 dput(dentry);
709 dentry = alias;
710 }
Trond Myklebusta1147b82020-02-05 09:01:52 -0500711 nfs_set_verifier(dentry, dir_verifier);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400712out:
713 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400714}
715
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400716/* Perform conversion from xdr to cache array */
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500717static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
718 struct nfs_entry *entry,
719 struct page **xdr_pages,
Trond Myklebust35df59d2020-11-06 20:38:47 -0500720 unsigned int buflen,
721 struct page **arrays,
722 size_t narrays)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400723{
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500724 struct address_space *mapping = desc->file->f_mapping;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400725 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400726 struct xdr_buf buf;
Trond Myklebust35df59d2020-11-06 20:38:47 -0500727 struct page *scratch, *new, *page = *arrays;
Trond Myklebust5c346852010-11-20 12:43:45 -0500728 int status;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400729
Trond Myklebust66502392011-01-08 17:45:38 -0500730 scratch = alloc_page(GFP_KERNEL);
731 if (scratch == NULL)
732 return -ENOMEM;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400733
Benny Halevyf7da7a12011-05-19 14:16:47 -0400734 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
Chuck Lever0ae4c3e2020-11-11 15:52:47 -0500735 xdr_set_scratch_page(&stream, scratch);
Bryan Schumaker99424382010-10-21 16:33:16 -0400736
737 do {
Anna Schumakerb1db9a42021-10-22 13:11:01 -0400738 if (entry->fattr->label)
739 entry->fattr->label->len = NFS4_MAXLABELLEN;
Jeffrey Mitchelld33030e2020-09-15 16:42:52 -0500740
Bryan Schumaker99424382010-10-21 16:33:16 -0400741 status = xdr_decode(desc, entry, &stream);
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500742 if (status != 0)
Bryan Schumaker99424382010-10-21 16:33:16 -0400743 break;
Trond Myklebust5c346852010-11-20 12:43:45 -0500744
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400745 if (desc->plus)
Trond Myklebusta1147b82020-02-05 09:01:52 -0500746 nfs_prime_dcache(file_dentry(desc->file), entry,
747 desc->dir_verifier);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500748
Max Kellermanndb531db2019-07-12 16:18:06 +0200749 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500750 if (status != -ENOSPC)
751 continue;
Bryan Schumaker99424382010-10-21 16:33:16 -0400752
Trond Myklebust35df59d2020-11-06 20:38:47 -0500753 if (page->mapping != mapping) {
754 if (!--narrays)
755 break;
756 new = nfs_readdir_page_array_alloc(entry->prev_cookie,
757 GFP_KERNEL);
758 if (!new)
759 break;
760 arrays++;
761 *arrays = page = new;
762 } else {
763 new = nfs_readdir_page_get_next(mapping,
764 page->index + 1,
765 entry->prev_cookie);
766 if (!new)
767 break;
768 if (page != *arrays)
769 nfs_readdir_page_unlock_and_put(page);
770 page = new;
771 }
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500772 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500773 } while (!status && !entry->eof);
Bryan Schumaker99424382010-10-21 16:33:16 -0400774
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500775 switch (status) {
776 case -EBADCOOKIE:
777 if (entry->eof) {
778 nfs_readdir_page_set_eof(page);
779 status = 0;
780 }
781 break;
782 case -ENOSPC:
783 case -EAGAIN:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200784 status = 0;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500785 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400786 }
Trond Myklebust66502392011-01-08 17:45:38 -0500787
Trond Myklebust35df59d2020-11-06 20:38:47 -0500788 if (page != *arrays)
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500789 nfs_readdir_page_unlock_and_put(page);
790
Trond Myklebust66502392011-01-08 17:45:38 -0500791 put_page(scratch);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500792 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400793}
794
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500795static void nfs_readdir_free_pages(struct page **pages, size_t npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400796{
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500797 while (npages--)
798 put_page(pages[npages]);
799 kfree(pages);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400800}
801
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400802/*
zhangliguangbf211ca2019-02-16 08:38:40 +0800803 * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
804 * to nfs_readdir_free_pages()
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400805 */
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500806static struct page **nfs_readdir_alloc_pages(size_t npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400807{
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500808 struct page **pages;
809 size_t i;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400810
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500811 pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
812 if (!pages)
813 return NULL;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400814 for (i = 0; i < npages; i++) {
815 struct page *page = alloc_page(GFP_KERNEL);
816 if (page == NULL)
817 goto out_freepages;
818 pages[i] = page;
819 }
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500820 return pages;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400821
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400822out_freepages:
Anna Schumakerc7e96682015-07-13 14:01:25 -0400823 nfs_readdir_free_pages(pages, i);
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500824 return NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400825}
826
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500827static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
Trond Myklebust35df59d2020-11-06 20:38:47 -0500828 __be32 *verf_arg, __be32 *verf_res,
829 struct page **arrays, size_t narrays)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400830{
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500831 struct page **pages;
Trond Myklebust35df59d2020-11-06 20:38:47 -0500832 struct page *page = *arrays;
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500833 struct nfs_entry *entry;
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500834 size_t array_size;
Trond Myklebustb593c092020-11-02 20:06:12 -0500835 struct inode *inode = file_inode(desc->file);
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500836 size_t dtsize = NFS_SERVER(inode)->dtsize;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500837 int status = -ENOMEM;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400838
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500839 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
840 if (!entry)
841 return -ENOMEM;
842 entry->cookie = nfs_readdir_page_last_cookie(page);
843 entry->fh = nfs_alloc_fhandle();
Anna Schumakerb1db9a42021-10-22 13:11:01 -0400844 entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500845 entry->server = NFS_SERVER(inode);
846 if (entry->fh == NULL || entry->fattr == NULL)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400847 goto out;
848
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500849 array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
850 pages = nfs_readdir_alloc_pages(array_size);
851 if (!pages)
Anna Schumakerb1db9a42021-10-22 13:11:01 -0400852 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400853
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400854 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500855 unsigned int pglen;
Trond Myklebustb593c092020-11-02 20:06:12 -0500856 status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie,
857 pages, dtsize,
858 verf_res);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400859 if (status < 0)
860 break;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500861
Trond Myklebustac396122010-11-15 20:26:22 -0500862 pglen = status;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500863 if (pglen == 0) {
864 nfs_readdir_page_set_eof(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500865 break;
866 }
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500867
Nagendra S Tomaree3707a2021-03-16 10:25:14 +0000868 verf_arg = verf_res;
869
Trond Myklebust35df59d2020-11-06 20:38:47 -0500870 status = nfs_readdir_page_filler(desc, entry, pages, pglen,
871 arrays, narrays);
trondmy@kernel.orgd9c4e392022-01-18 19:25:42 -0500872 } while (!status && nfs_readdir_page_needs_filling(page) &&
873 page_mapping(page));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400874
Anna Schumakerc7e96682015-07-13 14:01:25 -0400875 nfs_readdir_free_pages(pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400876out:
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500877 nfs_free_fattr(entry->fattr);
878 nfs_free_fhandle(entry->fh);
879 kfree(entry);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400880 return status;
881}
882
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500883static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300885 put_page(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500889static void
890nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500892 unlock_page(desc->page);
893 nfs_readdir_page_put(desc);
894}
895
896static struct page *
897nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
898{
899 return nfs_readdir_page_get_locked(desc->file->f_mapping,
900 desc->page_index,
901 desc->last_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400905 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Trond Myklebust114de382020-02-02 17:53:54 -0500906 * and locks the page to prevent removal from the page cache.
Olivier Galibert00a92642005-06-22 17:16:29 +0000907 */
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500908static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000909{
Dai Ngo227823d2020-01-22 20:45:39 -0500910 struct inode *inode = file_inode(desc->file);
911 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb593c092020-11-02 20:06:12 -0500912 __be32 verf[NFS_DIR_VERIFIER_SIZE];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400913 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000914
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500915 desc->page = nfs_readdir_page_get_cached(desc);
916 if (!desc->page)
917 return -ENOMEM;
918 if (nfs_readdir_page_needs_filling(desc->page)) {
Trond Myklebust35df59d2020-11-06 20:38:47 -0500919 res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
920 &desc->page, 1);
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500921 if (res < 0) {
922 nfs_readdir_page_unlock_and_put_cached(desc);
923 if (res == -EBADCOOKIE || res == -ENOTSYNC) {
924 invalidate_inode_pages2(desc->file->f_mapping);
925 desc->page_index = 0;
926 return -EAGAIN;
927 }
928 return res;
Dai Ngo227823d2020-01-22 20:45:39 -0500929 }
Trond Myklebustf892c412021-03-17 08:46:19 -0400930 /*
931 * Set the cookie verifier if the page cache was empty
932 */
933 if (desc->page_index == 0)
934 memcpy(nfsi->cookieverf, verf,
935 sizeof(nfsi->cookieverf));
Trond Myklebust114de382020-02-02 17:53:54 -0500936 }
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500937 res = nfs_readdir_search_array(desc);
Trond Myklebustff81dfb2021-09-28 14:33:44 -0400938 if (res == 0)
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500939 return 0;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500940 nfs_readdir_page_unlock_and_put_cached(desc);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400941 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000942}
943
Trond Myklebust794092c2020-11-06 20:47:05 -0500944static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc)
945{
946 struct address_space *mapping = desc->file->f_mapping;
947 struct inode *dir = file_inode(desc->file);
948 unsigned int dtsize = NFS_SERVER(dir)->dtsize;
949 loff_t size = i_size_read(dir);
950
951 /*
952 * Default to uncached readdir if the page cache is empty, and
953 * we're looking for a non-zero cookie in a large directory.
954 */
955 return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize;
956}
957
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400958/* Search for desc->dir_cookie from the beginning of the page cache */
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500959static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500961 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000962
Trond Myklebust794092c2020-11-06 20:47:05 -0500963 if (nfs_readdir_dont_search_cache(desc))
964 return -EBADCOOKIE;
965
Trond Myklebust47c716c2010-12-07 12:44:56 -0500966 do {
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500967 if (desc->page_index == 0) {
968 desc->current_index = 0;
969 desc->prev_index = 0;
970 desc->last_cookie = 0;
971 }
Trond Myklebust114de382020-02-02 17:53:54 -0500972 res = find_and_lock_cache_page(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500973 } while (res == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return res;
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*
978 * Once we've found the start of the dirent within a page: fill 'er up...
979 */
Trond Myklebust13884ff2021-03-16 07:57:40 -0400980static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
981 const __be32 *verf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 struct file *file = desc->file;
Trond Myklebustdbeaf8c2020-11-01 18:20:03 -0500984 struct nfs_cache_array *array;
985 unsigned int i = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400986
Fabian Frederick0795bf82017-05-03 20:52:21 +0200987 array = kmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400988 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500989 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Trond Myklebustece0b422010-11-20 13:55:33 -0500991 ent = &array->array[i];
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500992 if (!dir_emit(desc->ctx, ent->name, ent->name_len,
Al Viro23db8622013-05-17 16:34:50 -0400993 nfs_compat_user_ino64(ent->ino), ent->d_type)) {
Trond Myklebuste1d26992022-01-18 22:10:52 -0500994 desc->eob = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500996 }
Trond Myklebust13884ff2021-03-16 07:57:40 -0400997 memcpy(desc->verf, verf, sizeof(desc->verf));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400998 if (i < (array->size-1))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500999 desc->dir_cookie = array->array[i+1].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -04001000 else
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001001 desc->dir_cookie = array->last_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001002 if (nfs_readdir_use_cookie(file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001003 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001004 else
1005 desc->ctx->pos++;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001006 if (desc->duped != 0)
1007 desc->duped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -05001009 if (array->page_is_eof)
Trond Myklebuste1d26992022-01-18 22:10:52 -05001010 desc->eof = !desc->eob;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -04001011
Fabian Frederick0795bf82017-05-03 20:52:21 +02001012 kunmap(desc->page);
Trond Myklebustdbeaf8c2020-11-01 18:20:03 -05001013 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1014 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*
1018 * If we cannot find a cookie in our cache, we suspect that this is
1019 * because it points to a deleted file, so we ask the server to return
1020 * whatever it thinks is the next entry. We then feed this to filldir.
1021 * If all goes well, we should then be able to find our way round the
1022 * cache on the next call to readdir_search_pagecache();
1023 *
1024 * NOTE: we cannot add the anonymous page to the pagecache because
1025 * the data it contains might not be page aligned. Besides,
1026 * we should already have a complete representation of the
1027 * directory in the page cache by the time we get here.
1028 */
Trond Myklebust6c981ef2020-11-03 07:42:04 -05001029static int uncached_readdir(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Trond Myklebust35df59d2020-11-06 20:38:47 -05001031 struct page **arrays;
1032 size_t i, sz = 512;
Trond Myklebustb593c092020-11-02 20:06:12 -05001033 __be32 verf[NFS_DIR_VERIFIER_SIZE];
Trond Myklebust35df59d2020-11-06 20:38:47 -05001034 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Trond Myklebust35df59d2020-11-06 20:38:47 -05001036 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001037 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Trond Myklebust35df59d2020-11-06 20:38:47 -05001039 arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
1040 if (!arrays)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto out;
Trond Myklebust35df59d2020-11-06 20:38:47 -05001042 arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
1043 if (!arrays[0])
1044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -05001046 desc->page_index = 0;
trondmy@kernel.orgce292d82022-01-18 19:52:16 -05001047 desc->cache_entry_index = 0;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001048 desc->last_cookie = desc->dir_cookie;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001049 desc->duped = 0;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -05001050
Trond Myklebust35df59d2020-11-06 20:38:47 -05001051 status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -04001052
Trond Myklebuste1d26992022-01-18 22:10:52 -05001053 for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
Trond Myklebust35df59d2020-11-06 20:38:47 -05001054 desc->page = arrays[i];
Trond Myklebust13884ff2021-03-16 07:57:40 -04001055 nfs_do_filldir(desc, verf);
Trond Myklebust35df59d2020-11-06 20:38:47 -05001056 }
1057 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Trond Myklebust35df59d2020-11-06 20:38:47 -05001059
1060 for (i = 0; i < sz && arrays[i]; i++)
1061 nfs_readdir_page_array_free(arrays[i]);
1062out:
1063 kfree(arrays);
1064 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Olivier Galibert00a92642005-06-22 17:16:29 +00001068/* The file offset position represents the dirent entry number. A
1069 last cookie cache takes care of the common case of reading the
1070 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Al Viro23db8622013-05-17 16:34:50 -04001072static int nfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Miklos Szeredibe62a1a2016-03-26 16:14:39 -04001074 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +00001075 struct inode *inode = d_inode(dentry);
Trond Myklebust13884ff2021-03-16 07:57:40 -04001076 struct nfs_inode *nfsi = NFS_I(inode);
Al Viro23db8622013-05-17 16:34:50 -04001077 struct nfs_open_dir_context *dir_ctx = file->private_data;
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001078 struct nfs_readdir_descriptor *desc;
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001079 pgoff_t page_index;
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001080 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Al Viro6de14722013-09-16 10:53:17 -04001082 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
1083 file, (long long)ctx->pos);
Chuck Lever91d5b472006-03-20 13:44:14 -05001084 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /*
Al Viro23db8622013-05-17 16:34:50 -04001087 * ctx->pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001088 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +00001089 * to either find the entry with the appropriate number or
1090 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001092 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) {
Scott Mayhew07b5ce82013-07-05 17:49:31 -04001093 res = nfs_revalidate_mapping(inode, file->f_mapping);
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001094 if (res < 0)
1095 goto out;
1096 }
1097
1098 res = -ENOMEM;
1099 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1100 if (!desc)
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001101 goto out;
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001102 desc->file = file;
1103 desc->ctx = ctx;
1104 desc->plus = nfs_use_readdirplus(inode, ctx);
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001105
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001106 spin_lock(&file->f_lock);
1107 desc->dir_cookie = dir_ctx->dir_cookie;
1108 desc->dup_cookie = dir_ctx->dup_cookie;
1109 desc->duped = dir_ctx->duped;
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001110 page_index = dir_ctx->page_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001111 desc->attr_gencount = dir_ctx->attr_gencount;
Trond Myklebuste1d26992022-01-18 22:10:52 -05001112 desc->eof = dir_ctx->eof;
Trond Myklebustb593c092020-11-02 20:06:12 -05001113 memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001114 spin_unlock(&file->f_lock);
Trond Myklebust565277f2007-10-15 18:17:53 -04001115
Trond Myklebuste1d26992022-01-18 22:10:52 -05001116 if (desc->eof) {
1117 res = 0;
1118 goto out_free;
1119 }
1120
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001121 if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) &&
1122 list_is_singular(&nfsi->open_files))
1123 invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1);
1124
Trond Myklebust47c716c2010-12-07 12:44:56 -05001125 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +00001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -05001129 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* This means either end of directory */
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001131 if (desc->dir_cookie && !desc->eof) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* Or that the server has 'lost' a cookie */
Al Viro23db8622013-05-17 16:34:50 -04001133 res = uncached_readdir(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -05001134 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 continue;
Trond Myklebust9fff59e2020-11-02 20:11:32 -05001136 if (res == -EBADCOOKIE || res == -ENOTSYNC)
1137 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 }
1141 if (res == -ETOOSMALL && desc->plus) {
Trond Myklebust13884ff2021-03-16 07:57:40 -04001142 clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -04001144 desc->page_index = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001145 desc->plus = false;
1146 desc->eof = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 continue;
1148 }
1149 if (res < 0)
1150 break;
1151
Trond Myklebust13884ff2021-03-16 07:57:40 -04001152 nfs_do_filldir(desc, nfsi->cookieverf);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -05001153 nfs_readdir_page_unlock_and_put_cached(desc);
Trond Myklebuste1d26992022-01-18 22:10:52 -05001154 } while (!desc->eob && !desc->eof);
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001155
1156 spin_lock(&file->f_lock);
1157 dir_ctx->dir_cookie = desc->dir_cookie;
1158 dir_ctx->dup_cookie = desc->dup_cookie;
1159 dir_ctx->duped = desc->duped;
1160 dir_ctx->attr_gencount = desc->attr_gencount;
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001161 dir_ctx->page_index = desc->page_index;
Trond Myklebuste1d26992022-01-18 22:10:52 -05001162 dir_ctx->eof = desc->eof;
Trond Myklebustb593c092020-11-02 20:06:12 -05001163 memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001164 spin_unlock(&file->f_lock);
Trond Myklebuste1d26992022-01-18 22:10:52 -05001165out_free:
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001166 kfree(desc);
1167
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001168out:
Al Viro6de14722013-09-16 10:53:17 -04001169 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001170 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Andrew Morton965c8e52012-12-17 15:59:39 -08001173static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001174{
Bryan Schumaker480c2002011-03-23 14:48:29 -04001175 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Chuck Leverb84e06c2008-06-11 17:55:34 -04001176
Al Viro6de14722013-09-16 10:53:17 -04001177 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1178 filp, offset, whence);
Chuck Leverb84e06c2008-06-11 17:55:34 -04001179
Andrew Morton965c8e52012-12-17 15:59:39 -08001180 switch (whence) {
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001181 default:
1182 return -EINVAL;
1183 case SEEK_SET:
1184 if (offset < 0)
1185 return -EINVAL;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001186 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001187 break;
1188 case SEEK_CUR:
1189 if (offset == 0)
1190 return filp->f_pos;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001191 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001192 offset += filp->f_pos;
1193 if (offset < 0) {
Trond Myklebust83f2c452020-10-30 17:57:29 -04001194 spin_unlock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001195 return -EINVAL;
1196 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001197 }
1198 if (offset != filp->f_pos) {
1199 filp->f_pos = offset;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001200 if (nfs_readdir_use_cookie(filp))
1201 dir_ctx->dir_cookie = offset;
1202 else
1203 dir_ctx->dir_cookie = 0;
Trond Myklebustb593c092020-11-02 20:06:12 -05001204 if (offset == 0)
1205 memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -04001206 dir_ctx->duped = 0;
Trond Myklebuste1d26992022-01-18 22:10:52 -05001207 dir_ctx->eof = false;
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001208 }
Trond Myklebust83f2c452020-10-30 17:57:29 -04001209 spin_unlock(&filp->f_lock);
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001210 return offset;
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
1214 * All directory operations under NFS are synchronous, so fsync()
1215 * is a dummy operation.
1216 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001217static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1218 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Al Viro6de14722013-09-16 10:53:17 -04001220 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001221
Trond Myklebust11decaf2020-10-30 17:57:30 -04001222 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
1224}
1225
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001226/**
1227 * nfs_force_lookup_revalidate - Mark the directory as having changed
Trond Myklebust302fad72019-02-18 13:32:38 -05001228 * @dir: pointer to directory inode
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001229 *
1230 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1231 * full lookup on all child dentries of 'dir' whenever a change occurs
1232 * on the server that might have invalidated our dcache.
1233 *
Trond Myklebustefeda802020-02-05 09:01:54 -05001234 * Note that we reserve bit '0' as a tag to let us know when a dentry
1235 * was revalidated while holding a delegation on its inode.
1236 *
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001237 * The caller should be holding dir->i_lock
1238 */
1239void nfs_force_lookup_revalidate(struct inode *dir)
1240{
Trond Myklebustefeda802020-02-05 09:01:54 -05001241 NFS_I(dir)->cache_change_attribute += 2;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001242}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001243EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001244
Trond Myklebustefeda802020-02-05 09:01:54 -05001245/**
1246 * nfs_verify_change_attribute - Detects NFS remote directory changes
1247 * @dir: pointer to parent directory inode
1248 * @verf: previously saved change attribute
1249 *
1250 * Return "false" if the verifiers doesn't match the change attribute.
1251 * This would usually indicate that the directory contents have changed on
1252 * the server, and that any dentries need revalidating.
1253 */
1254static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1255{
1256 return (verf & ~1UL) == nfs_save_change_attribute(dir);
1257}
1258
1259static void nfs_set_verifier_delegated(unsigned long *verf)
1260{
1261 *verf |= 1UL;
1262}
1263
1264#if IS_ENABLED(CONFIG_NFS_V4)
1265static void nfs_unset_verifier_delegated(unsigned long *verf)
1266{
1267 *verf &= ~1UL;
1268}
1269#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1270
1271static bool nfs_test_verifier_delegated(unsigned long verf)
1272{
1273 return verf & 1;
1274}
1275
1276static bool nfs_verifier_is_delegated(struct dentry *dentry)
1277{
1278 return nfs_test_verifier_delegated(dentry->d_time);
1279}
1280
1281static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1282{
1283 struct inode *inode = d_inode(dentry);
Trond Myklebustcec08f42021-09-29 08:12:53 -04001284 struct inode *dir = d_inode(dentry->d_parent);
Trond Myklebustefeda802020-02-05 09:01:54 -05001285
Trond Myklebustcec08f42021-09-29 08:12:53 -04001286 if (!nfs_verify_change_attribute(dir, verf))
1287 return;
Trond Myklebustefeda802020-02-05 09:01:54 -05001288 if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1289 nfs_set_verifier_delegated(&verf);
Trond Myklebustefeda802020-02-05 09:01:54 -05001290 dentry->d_time = verf;
1291}
1292
1293/**
1294 * nfs_set_verifier - save a parent directory verifier in the dentry
1295 * @dentry: pointer to dentry
1296 * @verf: verifier to save
1297 *
1298 * Saves the parent directory verifier in @dentry. If the inode has
1299 * a delegation, we also tag the dentry as having been revalidated
1300 * while holding a delegation so that we know we don't have to
1301 * look it up again after a directory change.
1302 */
1303void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1304{
1305
1306 spin_lock(&dentry->d_lock);
1307 nfs_set_verifier_locked(dentry, verf);
1308 spin_unlock(&dentry->d_lock);
1309}
1310EXPORT_SYMBOL_GPL(nfs_set_verifier);
1311
1312#if IS_ENABLED(CONFIG_NFS_V4)
1313/**
1314 * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1315 * @inode: pointer to inode
1316 *
1317 * Iterates through the dentries in the inode alias list and clears
1318 * the tag used to indicate that the dentry has been revalidated
1319 * while holding a delegation.
1320 * This function is intended for use when the delegation is being
1321 * returned or revoked.
1322 */
1323void nfs_clear_verifier_delegated(struct inode *inode)
1324{
1325 struct dentry *alias;
1326
1327 if (!inode)
1328 return;
1329 spin_lock(&inode->i_lock);
1330 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1331 spin_lock(&alias->d_lock);
1332 nfs_unset_verifier_delegated(&alias->d_time);
1333 spin_unlock(&alias->d_lock);
1334 }
1335 spin_unlock(&inode->i_lock);
1336}
1337EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1338#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1339
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001340static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
1341{
1342 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
1343 d_really_is_negative(dentry))
1344 return dentry->d_time == inode_peek_iversion_raw(dir);
1345 return nfs_verify_change_attribute(dir, dentry->d_time);
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/*
1349 * A check for whether or not the parent directory has changed.
1350 * In the case it has, we assume that the dentries are untrustworthy
1351 * and may need to be looked up again.
NeilBrown912a1082014-07-14 11:28:20 +10001352 * If rcu_walk prevents us from performing a full check, return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
NeilBrown912a1082014-07-14 11:28:20 +10001354static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1355 int rcu_walk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 if (IS_ROOT(dentry))
1358 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001359 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1360 return 0;
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001361 if (!nfs_dentry_verify_change(dir, dentry))
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001362 return 0;
1363 /* Revalidate nfsi->cache_change_attribute before we declare a match */
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001364 if (nfs_mapping_need_revalidate_inode(dir)) {
1365 if (rcu_walk)
1366 return 0;
1367 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1368 return 0;
1369 }
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001370 if (!nfs_dentry_verify_change(dir, dentry))
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001371 return 0;
1372 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/*
Trond Myklebusta12802c2007-10-02 19:13:04 -04001376 * Use intent information to check whether or not we're going to do
1377 * an O_EXCL create using this path component.
1378 */
Al Virofa3c56b2012-06-10 15:36:40 -04001379static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
Trond Myklebusta12802c2007-10-02 19:13:04 -04001380{
1381 if (NFS_PROTO(dir)->version == 2)
1382 return 0;
Al Virofa3c56b2012-06-10 15:36:40 -04001383 return flags & LOOKUP_EXCL;
Trond Myklebusta12802c2007-10-02 19:13:04 -04001384}
1385
1386/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001387 * Inode and filehandle revalidation for lookups.
1388 *
1389 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1390 * or if the intent information indicates that we're about to open this
1391 * particular file and the "nocto" mount flag is not set.
1392 *
1393 */
Trond Myklebust65a0c142012-12-14 17:51:40 -05001394static
Al Virofa3c56b2012-06-10 15:36:40 -04001395int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust65a0c142012-12-14 17:51:40 -05001398 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
David Howells36d43a42011-01-14 18:45:42 +00001400 if (IS_AUTOMOUNT(inode))
Trond Myklebust4e99a1f2008-03-06 12:34:59 -05001401 return 0;
Trond Myklebust47921922018-05-10 10:08:36 -04001402
1403 if (flags & LOOKUP_OPEN) {
1404 switch (inode->i_mode & S_IFMT) {
1405 case S_IFREG:
1406 /* A NFSv4 OPEN will revalidate later */
1407 if (server->caps & NFS_CAP_ATOMIC_OPEN)
1408 goto out;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001409 fallthrough;
Trond Myklebust47921922018-05-10 10:08:36 -04001410 case S_IFDIR:
1411 if (server->flags & NFS_MOUNT_NOCTO)
1412 break;
1413 /* NFS close-to-open cache consistency validation */
1414 goto out_force;
1415 }
1416 }
1417
Al Virofacc3532012-06-10 15:33:51 -04001418 /* VFS wants an on-the-wire revalidation */
Al Virofa3c56b2012-06-10 15:36:40 -04001419 if (flags & LOOKUP_REVAL)
Al Virofacc3532012-06-10 15:33:51 -04001420 goto out_force;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001421out:
Lance Sheltona61246c2018-07-16 13:05:36 -04001422 return (inode->i_nlink == 0) ? -ESTALE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423out_force:
NeilBrown1fa1e382014-07-14 11:28:20 +10001424 if (flags & LOOKUP_RCU)
1425 return -ECHILD;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001426 ret = __nfs_revalidate_inode(server, inode);
1427 if (ret != 0)
1428 return ret;
1429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001432static void nfs_mark_dir_for_revalidate(struct inode *inode)
1433{
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001434 spin_lock(&inode->i_lock);
Trond Myklebusta6a361c2021-09-28 11:24:57 -04001435 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001436 spin_unlock(&inode->i_lock);
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/*
1440 * We judge how long we want to trust negative
1441 * dentries by looking at the parent inode mtime.
1442 *
1443 * If parent mtime has changed, we revalidate, else we wait for a
1444 * period corresponding to the parent's attribute cache timeout value.
NeilBrown912a1082014-07-14 11:28:20 +10001445 *
1446 * If LOOKUP_RCU prevents us from performing a full check, return 1
1447 * suggesting a reval is needed.
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001448 *
1449 * Note that when creating a new file, or looking up a rename target,
1450 * then it shouldn't be necessary to revalidate a negative dentry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
1452static inline
1453int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
Al Virofa3c56b2012-06-10 15:36:40 -04001454 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001456 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001458 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1459 return 1;
Trond Myklebust98ca3ee2021-12-17 15:36:55 -05001460 /* Case insensitive server? Revalidate negative dentries */
1461 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1462 return 1;
NeilBrown912a1082014-07-14 11:28:20 +10001463 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001466static int
1467nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1468 struct inode *inode, int error)
1469{
1470 switch (error) {
1471 case 1:
1472 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
1473 __func__, dentry);
1474 return 1;
1475 case 0:
Trond Myklebust47397912021-03-08 14:42:52 -05001476 /*
1477 * We can't d_drop the root of a disconnected tree:
1478 * its d_hash is on the s_anon list and d_drop() would hide
1479 * it from shrink_dcache_for_unmount(), leading to busy
1480 * inodes on unmount and further oopses.
1481 */
1482 if (inode && IS_ROOT(dentry))
1483 return 1;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001484 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
1485 __func__, dentry);
1486 return 0;
1487 }
1488 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
1489 __func__, dentry, error);
1490 return error;
1491}
1492
1493static int
1494nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1495 unsigned int flags)
1496{
1497 int ret = 1;
1498 if (nfs_neg_need_reval(dir, dentry, flags)) {
1499 if (flags & LOOKUP_RCU)
1500 return -ECHILD;
1501 ret = 0;
1502 }
1503 return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1504}
1505
1506static int
1507nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1508 struct inode *inode)
1509{
1510 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1511 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1512}
1513
1514static int
1515nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
1516 struct inode *inode)
1517{
1518 struct nfs_fh *fhandle;
1519 struct nfs_fattr *fattr;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001520 unsigned long dir_verifier;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001521 int ret;
1522
1523 ret = -ENOMEM;
1524 fhandle = nfs_alloc_fhandle();
Anna Schumaker9558a002021-10-22 13:11:04 -04001525 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1526 if (fhandle == NULL || fattr == NULL)
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001527 goto out;
1528
Trond Myklebusta1147b82020-02-05 09:01:52 -05001529 dir_verifier = nfs_save_change_attribute(dir);
Anna Schumaker9558a002021-10-22 13:11:04 -04001530 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001531 if (ret < 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001532 switch (ret) {
1533 case -ESTALE:
1534 case -ENOENT:
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001535 ret = 0;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001536 break;
1537 case -ETIMEDOUT:
1538 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1539 ret = 1;
1540 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001541 goto out;
1542 }
1543 ret = 0;
1544 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1545 goto out;
1546 if (nfs_refresh_inode(inode, fattr) < 0)
1547 goto out;
1548
Anna Schumakerdd225cb2021-10-22 13:11:12 -04001549 nfs_setsecurity(inode, fattr);
Trond Myklebusta1147b82020-02-05 09:01:52 -05001550 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001551
1552 /* set a readdirplus hint that we had a cache miss */
1553 nfs_force_use_readdirplus(dir);
1554 ret = 1;
1555out:
1556 nfs_free_fattr(fattr);
1557 nfs_free_fhandle(fhandle);
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001558
1559 /*
1560 * If the lookup failed despite the dentry change attribute being
1561 * a match, then we should revalidate the directory cache.
1562 */
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001563 if (!ret && nfs_dentry_verify_change(dir, dentry))
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001564 nfs_mark_dir_for_revalidate(dir);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001565 return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/*
1569 * This is called every time the dcache has a lookup hit,
1570 * and we should check whether we can really trust that
1571 * lookup.
1572 *
1573 * NOTE! The hit can be a negative hit too, don't assume
1574 * we have an inode!
1575 *
1576 * If the parent directory is seen to have changed, we throw out the
1577 * cached dentry and do a new lookup.
1578 */
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001579static int
1580nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1581 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Chuck Lever91d5b472006-03-20 13:44:14 -05001586 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
David Howells2b0143b2015-03-17 22:25:59 +00001587 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001589 if (!inode)
1590 return nfs_lookup_revalidate_negative(dir, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001593 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1594 __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto out_bad;
1596 }
1597
Trond Myklebustefeda802020-02-05 09:01:54 -05001598 if (nfs_verifier_is_delegated(dentry))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001599 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /* Force a full look up iff the parent directory has changed */
Trond Myklebust73dd6842018-05-10 10:13:09 -04001602 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
NeilBrown912a1082014-07-14 11:28:20 +10001603 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
NeilBrowncc896842017-07-05 12:22:20 +10001604 error = nfs_lookup_verify_inode(inode, flags);
1605 if (error) {
NeilBrowncc896842017-07-05 12:22:20 +10001606 if (error == -ESTALE)
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001607 nfs_mark_dir_for_revalidate(dir);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001608 goto out_bad;
NeilBrown1fa1e382014-07-14 11:28:20 +10001609 }
Trond Myklebust63519fb2016-11-19 11:21:54 -05001610 nfs_advise_use_readdirplus(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto out_valid;
1612 }
1613
NeilBrown912a1082014-07-14 11:28:20 +10001614 if (flags & LOOKUP_RCU)
1615 return -ECHILD;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (NFS_STALE(inode))
1618 goto out_bad;
1619
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001620 trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001621 error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001622 trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001623 return error;
1624out_valid:
1625 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1626out_bad:
1627 if (flags & LOOKUP_RCU)
1628 return -ECHILD;
1629 return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001632static int
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001633__nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1634 int (*reval)(struct inode *, struct dentry *, unsigned int))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001635{
1636 struct dentry *parent;
1637 struct inode *dir;
1638 int ret;
David Quigleyaa9c2662013-05-22 12:50:44 -04001639
NeilBrownd51ac1a2014-07-14 11:28:20 +10001640 if (flags & LOOKUP_RCU) {
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001641 parent = READ_ONCE(dentry->d_parent);
1642 dir = d_inode_rcu(parent);
1643 if (!dir)
1644 return -ECHILD;
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001645 ret = reval(dir, dentry, flags);
Mark Rutland6aa7de02017-10-23 14:07:29 -07001646 if (parent != READ_ONCE(dentry->d_parent))
NeilBrownd51ac1a2014-07-14 11:28:20 +10001647 return -ECHILD;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001648 } else {
1649 parent = dget_parent(dentry);
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001650 ret = reval(d_inode(parent), dentry, flags);
NeilBrownd51ac1a2014-07-14 11:28:20 +10001651 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001656static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1657{
1658 return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1659}
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661/*
David Howells2b0143b2015-03-17 22:25:59 +00001662 * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001663 * when we don't really care about the dentry name. This is called when a
1664 * pathwalk ends on a dentry that was not found via a normal lookup in the
1665 * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1666 *
1667 * In this situation, we just want to verify that the inode itself is OK
1668 * since the dentry might have changed on the server.
1669 */
1670static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1671{
David Howells2b0143b2015-03-17 22:25:59 +00001672 struct inode *inode = d_inode(dentry);
Trond Myklebust9cdd1d32016-12-16 18:04:47 -05001673 int error = 0;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001674
1675 /*
1676 * I believe we can only get a negative dentry here in the case of a
1677 * procfs-style symlink. Just assume it's correct for now, but we may
1678 * eventually need to do something more here.
1679 */
1680 if (!inode) {
Al Viro6de14722013-09-16 10:53:17 -04001681 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1682 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001683 return 1;
1684 }
1685
1686 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001687 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1688 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001689 return 0;
1690 }
1691
NeilBrownb6887412017-08-25 17:34:41 +10001692 error = nfs_lookup_verify_inode(inode, flags);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001693 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1694 __func__, inode->i_ino, error ? "invalid" : "valid");
1695 return !error;
1696}
1697
1698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * This is called from dput() when d_count is going to 0.
1700 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001701static int nfs_dentry_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Al Viro6de14722013-09-16 10:53:17 -04001703 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1704 dentry, dentry->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Trond Myklebust77f11192008-01-28 19:43:19 -05001706 /* Unhash any dentry with a stale inode */
David Howells2b0143b2015-03-17 22:25:59 +00001707 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
Trond Myklebust77f11192008-01-28 19:43:19 -05001708 return 1;
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1711 /* Unhash it, so that ->d_iput() would be called */
1712 return 1;
1713 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001714 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* Unhash it, so that ancestors of killed async unlink
1716 * files will be cleaned up during umount */
1717 return 1;
1718 }
1719 return 0;
1720
1721}
1722
Trond Myklebust1f018452012-12-14 16:38:46 -05001723/* Ensure that we revalidate inode->i_nlink */
Trond Myklebust1b83d702008-06-11 15:44:04 -04001724static void nfs_drop_nlink(struct inode *inode)
1725{
1726 spin_lock(&inode->i_lock);
Trond Myklebust1f018452012-12-14 16:38:46 -05001727 /* drop the inode if we're reasonably sure this is the last link */
Trond Myklebust59a707b2018-04-08 18:11:18 -04001728 if (inode->i_nlink > 0)
1729 drop_nlink(inode);
1730 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustac46b3d2021-03-08 14:42:54 -05001731 nfs_set_cache_invalid(
1732 inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
Trond Myklebust1301e422021-04-01 14:57:56 -04001733 NFS_INO_INVALID_NLINK);
Trond Myklebust1b83d702008-06-11 15:44:04 -04001734 spin_unlock(&inode->i_lock);
1735}
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737/*
1738 * Called when the dentry loses inode.
1739 * We use it to clean up silly-renamed files.
1740 */
1741static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1742{
1743 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001744 nfs_complete_unlink(dentry, inode);
Trond Myklebust1f018452012-12-14 16:38:46 -05001745 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 iput(inode);
1748}
1749
Al Virob1942c52011-03-16 05:44:14 -04001750static void nfs_d_release(struct dentry *dentry)
1751{
1752 /* free cached devname value, if it survived that far */
1753 if (unlikely(dentry->d_fsdata)) {
1754 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1755 WARN_ON(1);
1756 else
1757 kfree(dentry->d_fsdata);
1758 }
1759}
1760
Al Virof786aa92009-02-20 05:51:22 +00001761const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 .d_revalidate = nfs_lookup_revalidate,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001763 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 .d_delete = nfs_dentry_delete,
1765 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001766 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001767 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768};
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001769EXPORT_SYMBOL_GPL(nfs_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Bryan Schumaker597d9282012-07-16 16:39:10 -04001771struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 struct dentry *res;
1774 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001775 struct nfs_fh *fhandle = NULL;
1776 struct nfs_fattr *fattr = NULL;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001777 unsigned long dir_verifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Al Viro6de14722013-09-16 10:53:17 -04001780 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
Chuck Lever91d5b472006-03-20 13:44:14 -05001781 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Al Viro130f9ab2016-03-07 22:40:43 -05001783 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1784 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Trond Myklebustfd684072006-09-05 12:27:44 -04001786 /*
1787 * If we're doing an exclusive create, optimize away the lookup
1788 * but don't hash the dentry.
1789 */
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001790 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
Al Viro130f9ab2016-03-07 22:40:43 -05001791 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001793 res = ERR_PTR(-ENOMEM);
1794 fhandle = nfs_alloc_fhandle();
Anna Schumaker9558a002021-10-22 13:11:04 -04001795 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001796 if (fhandle == NULL || fattr == NULL)
1797 goto out;
1798
Trond Myklebusta1147b82020-02-05 09:01:52 -05001799 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001800 trace_nfs_lookup_enter(dir, dentry, flags);
Anna Schumaker9558a002021-10-22 13:11:04 -04001801 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001802 if (error == -ENOENT) {
1803 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1804 dir_verifier = inode_peek_iversion_raw(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto no_entry;
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (error < 0) {
1808 res = ERR_PTR(error);
Anna Schumaker9558a002021-10-22 13:11:04 -04001809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Anna Schumakercf7ab002021-10-22 13:11:11 -04001811 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Namhyung Kimbf0c84f2010-12-28 17:02:46 +00001812 res = ERR_CAST(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001813 if (IS_ERR(res))
Anna Schumaker9558a002021-10-22 13:11:04 -04001814 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001815
Trond Myklebust63519fb2016-11-19 11:21:54 -05001816 /* Notify readdir to use READDIRPLUS */
1817 nfs_force_use_readdirplus(dir);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819no_entry:
Al Viro41d28bc2014-10-12 22:24:21 -04001820 res = d_splice_alias(inode, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001821 if (res != NULL) {
1822 if (IS_ERR(res))
Anna Schumaker9558a002021-10-22 13:11:04 -04001823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001825 }
Trond Myklebusta1147b82020-02-05 09:01:52 -05001826 nfs_set_verifier(dentry, dir_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827out:
Anna Schumaker9558a002021-10-22 13:11:04 -04001828 trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001829 nfs_free_fattr(fattr);
1830 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return res;
1832}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001833EXPORT_SYMBOL_GPL(nfs_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Trond Myklebust00bdadc2021-12-17 15:36:57 -05001835void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
1836{
1837 /* Case insensitive server? Revalidate dentries */
1838 if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
1839 d_prune_aliases(inode);
1840}
1841EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
1842
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001843#if IS_ENABLED(CONFIG_NFS_V4)
Al Viro0b728e12012-06-10 16:03:43 -04001844static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Al Virof786aa92009-02-20 05:51:22 +00001846const struct dentry_operations nfs4_dentry_operations = {
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001847 .d_revalidate = nfs4_lookup_revalidate,
NeilBrownb6887412017-08-25 17:34:41 +10001848 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 .d_delete = nfs_dentry_delete,
1850 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001851 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001852 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853};
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001854EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Al Viro8a5e9292011-06-25 19:15:54 -04001856static fmode_t flags_to_mode(int flags)
1857{
1858 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1859 if ((flags & O_ACCMODE) != O_WRONLY)
1860 res |= FMODE_READ;
1861 if ((flags & O_ACCMODE) != O_RDONLY)
1862 res |= FMODE_WRITE;
1863 return res;
1864}
1865
NeilBrown532d4de2016-10-13 15:26:47 +11001866static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001867{
NeilBrown532d4de2016-10-13 15:26:47 +11001868 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001869}
1870
1871static int do_open(struct inode *inode, struct file *filp)
1872{
David Howellsf1fe29b2013-09-27 11:20:03 +01001873 nfs_fscache_open_file(inode, filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001874 return 0;
1875}
1876
Al Virod9585272012-06-22 12:39:14 +04001877static int nfs_finish_open(struct nfs_open_context *ctx,
1878 struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -04001879 struct file *file, unsigned open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001880{
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001881 int err;
1882
Al Virobe12af32018-06-08 11:44:56 -04001883 err = finish_open(file, dentry, do_open);
Al Viro30d90492012-06-22 12:40:19 +04001884 if (err)
Al Virod9585272012-06-22 12:39:14 +04001885 goto out;
NeilBrowneaa2b822017-07-03 15:27:26 +10001886 if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1887 nfs_file_set_open_context(file, ctx);
1888 else
Trond Myklebust98214212019-08-09 12:15:07 -04001889 err = -EOPENSTALE;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001890out:
Al Virod9585272012-06-22 12:39:14 +04001891 return err;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001892}
1893
Bryan Schumaker73a79702012-07-16 16:39:12 -04001894int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1895 struct file *file, unsigned open_flags,
Al Viro44907d72018-06-08 13:32:02 -04001896 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Al Viroc94c0952016-07-05 09:49:21 -04001898 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001899 struct nfs_open_context *ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001900 struct dentry *res;
1901 struct iattr attr = { .ia_valid = ATTR_OPEN };
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001902 struct inode *inode;
Trond Myklebust1472b832013-08-20 11:59:41 -04001903 unsigned int lookup_flags = 0;
Trond Myklebust68eaba42021-12-17 15:36:58 -05001904 unsigned long dir_verifier;
Al Viroc94c0952016-07-05 09:49:21 -04001905 bool switched = false;
Al Viro73a09dd2018-06-08 13:22:02 -04001906 int created = 0;
Trond Myklebust898f6352010-10-23 11:24:25 -04001907 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001909 /* Expect a negative dentry */
David Howells2b0143b2015-03-17 22:25:59 +00001910 BUG_ON(d_inode(dentry));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001911
Niels de Vos1e8968c2013-12-17 18:20:16 +01001912 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001913 dir->i_sb->s_id, dir->i_ino, dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001914
Jeff Layton9597c132013-08-02 11:39:32 -04001915 err = nfs_check_flags(open_flags);
1916 if (err)
1917 return err;
1918
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001919 /* NFS only supports OPEN on regular files */
1920 if ((open_flags & O_DIRECTORY)) {
Al Viro00699ad2016-07-05 09:44:53 -04001921 if (!d_in_lookup(dentry)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001922 /*
1923 * Hashed negative dentry with O_DIRECTORY: dentry was
1924 * revalidated and is fine, no need to perform lookup
1925 * again
1926 */
Al Virod9585272012-06-22 12:39:14 +04001927 return -ENOENT;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001928 }
Trond Myklebust1472b832013-08-20 11:59:41 -04001929 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001933 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
Al Virod9585272012-06-22 12:39:14 +04001934 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001936 if (open_flags & O_CREAT) {
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001937 struct nfs_server *server = NFS_SERVER(dir);
1938
1939 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1940 mode &= ~current_umask();
1941
Trond Myklebust536e43d2012-01-17 22:04:26 -05001942 attr.ia_valid |= ATTR_MODE;
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001943 attr.ia_mode = mode;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001944 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001945 if (open_flags & O_TRUNC) {
1946 attr.ia_valid |= ATTR_SIZE;
1947 attr.ia_size = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001948 }
1949
Al Viroc94c0952016-07-05 09:49:21 -04001950 if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1951 d_drop(dentry);
1952 switched = true;
1953 dentry = d_alloc_parallel(dentry->d_parent,
1954 &dentry->d_name, &wq);
1955 if (IS_ERR(dentry))
1956 return PTR_ERR(dentry);
1957 if (unlikely(!d_in_lookup(dentry)))
1958 return finish_no_open(file, dentry);
1959 }
1960
NeilBrown532d4de2016-10-13 15:26:47 +11001961 ctx = create_nfs_open_context(dentry, open_flags, file);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001962 err = PTR_ERR(ctx);
1963 if (IS_ERR(ctx))
Al Virod9585272012-06-22 12:39:14 +04001964 goto out;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001965
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001966 trace_nfs_atomic_open_enter(dir, ctx, open_flags);
Al Viro73a09dd2018-06-08 13:22:02 -04001967 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
1968 if (created)
1969 file->f_mode |= FMODE_CREATED;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001970 if (IS_ERR(inode)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001971 err = PTR_ERR(inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001972 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001973 put_nfs_open_context(ctx);
Al Virod20cb712016-06-20 13:14:36 -04001974 d_drop(dentry);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001975 switch (err) {
1976 case -ENOENT:
Peng Tao774d9512017-06-29 06:34:50 -07001977 d_splice_alias(NULL, dentry);
Trond Myklebust68eaba42021-12-17 15:36:58 -05001978 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1979 dir_verifier = inode_peek_iversion_raw(dir);
1980 else
1981 dir_verifier = nfs_save_change_attribute(dir);
1982 nfs_set_verifier(dentry, dir_verifier);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001983 break;
1984 case -EISDIR:
1985 case -ENOTDIR:
1986 goto no_open;
1987 case -ELOOP:
1988 if (!(open_flags & O_NOFOLLOW))
Trond Myklebust6f926b52005-10-18 14:20:18 -07001989 goto no_open;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* case -EINVAL: */
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001992 default:
1993 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Al Virod9585272012-06-22 12:39:14 +04001995 goto out;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001996 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001997
Al Virob452a452018-06-08 13:06:28 -04001998 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001999 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04002000 put_nfs_open_context(ctx);
Al Virod9585272012-06-22 12:39:14 +04002001out:
Al Viroc94c0952016-07-05 09:49:21 -04002002 if (unlikely(switched)) {
2003 d_lookup_done(dentry);
2004 dput(dentry);
2005 }
Al Virod9585272012-06-22 12:39:14 +04002006 return err;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008no_open:
Trond Myklebust1472b832013-08-20 11:59:41 -04002009 res = nfs_lookup(dir, dentry, lookup_flags);
Trond Myklebustac795162022-01-06 18:24:02 -05002010 if (!res) {
2011 inode = d_inode(dentry);
2012 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2013 !S_ISDIR(inode->i_mode))
2014 res = ERR_PTR(-ENOTDIR);
Trond Myklebust1751fc12022-01-06 18:24:03 -05002015 else if (inode && S_ISREG(inode->i_mode))
2016 res = ERR_PTR(-EOPENSTALE);
Trond Myklebustac795162022-01-06 18:24:02 -05002017 } else if (!IS_ERR(res)) {
2018 inode = d_inode(res);
2019 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2020 !S_ISDIR(inode->i_mode)) {
2021 dput(res);
2022 res = ERR_PTR(-ENOTDIR);
Trond Myklebust1751fc12022-01-06 18:24:03 -05002023 } else if (inode && S_ISREG(inode->i_mode)) {
2024 dput(res);
2025 res = ERR_PTR(-EOPENSTALE);
Trond Myklebustac795162022-01-06 18:24:02 -05002026 }
2027 }
Al Viroc94c0952016-07-05 09:49:21 -04002028 if (switched) {
2029 d_lookup_done(dentry);
2030 if (!res)
2031 res = dentry;
2032 else
2033 dput(dentry);
2034 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02002035 if (IS_ERR(res))
Al Viroc94c0952016-07-05 09:49:21 -04002036 return PTR_ERR(res);
Al Viroe45198a2012-06-10 06:48:09 -04002037 return finish_no_open(file, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002039EXPORT_SYMBOL_GPL(nfs_atomic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002041static int
2042nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2043 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Nick Piggin657e94b2011-01-14 02:48:39 +00002045 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Al Virofa3c56b2012-06-10 15:36:40 -04002047 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002048 goto full_reval;
Miklos Szeredieda72af2012-06-05 15:10:21 +02002049 if (d_mountpoint(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002050 goto full_reval;
Trond Myklebust2b484292010-09-17 10:56:51 -04002051
David Howells2b0143b2015-03-17 22:25:59 +00002052 inode = d_inode(dentry);
Trond Myklebust2b484292010-09-17 10:56:51 -04002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* We can't create new files in nfs_open_revalidate(), so we
2055 * optimize away revalidation of negative dentries.
2056 */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002057 if (inode == NULL)
2058 goto full_reval;
NeilBrown49317a72014-07-14 11:28:20 +10002059
Trond Myklebustefeda802020-02-05 09:01:54 -05002060 if (nfs_verifier_is_delegated(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002061 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust216d5d062007-10-01 20:10:12 -04002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /* NFS only supports OPEN on regular files */
2064 if (!S_ISREG(inode->i_mode))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002065 goto full_reval;
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /* We cannot do exclusive creation on a positive dentry */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002068 if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2069 goto reval_dentry;
2070
2071 /* Check if the directory changed */
2072 if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2073 goto reval_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02002075 /* Let f_op->open() actually open (and revalidate) the file */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002076 return 1;
2077reval_dentry:
2078 if (flags & LOOKUP_RCU)
2079 return -ECHILD;
zhangliguang42f72cf2019-02-12 09:38:33 +08002080 return nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust536e43d2012-01-17 22:04:26 -05002081
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002082full_reval:
2083 return nfs_do_lookup_revalidate(dir, dentry, flags);
2084}
Trond Myklebust535918f2010-09-17 10:56:51 -04002085
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002086static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2087{
2088 return __nfs_lookup_revalidate(dentry, flags,
2089 nfs4_do_lookup_revalidate);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092#endif /* CONFIG_NFSV4 */
2093
Benjamin Coddington406cd912019-09-13 08:29:02 -04002094struct dentry *
2095nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumakercc6f3292021-10-22 13:11:10 -04002096 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Trond Myklebustfab728e2007-09-29 17:41:33 -04002098 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002099 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct inode *inode;
Al Virob0c6108e2018-05-16 10:55:01 -04002101 struct dentry *d;
Benjamin Coddington406cd912019-09-13 08:29:02 -04002102 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Trond Myklebustfab728e2007-09-29 17:41:33 -04002104 d_drop(dentry);
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (fhandle->size == 0) {
Anna Schumaker9558a002021-10-22 13:11:04 -04002107 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04002109 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04002111 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (!(fattr->valid & NFS_ATTR_FATTR)) {
2113 struct nfs_server *server = NFS_SB(dentry->d_sb);
Trond Myklebusta841b542018-04-07 13:50:59 -04002114 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -04002115 fattr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04002117 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Anna Schumakercf7ab002021-10-22 13:11:11 -04002119 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Al Virob0c6108e2018-05-16 10:55:01 -04002120 d = d_splice_alias(inode, dentry);
Trond Myklebustfab728e2007-09-29 17:41:33 -04002121out:
2122 dput(parent);
Benjamin Coddington406cd912019-09-13 08:29:02 -04002123 return d;
Trond Myklebustfab728e2007-09-29 17:41:33 -04002124out_error:
Benjamin Coddington406cd912019-09-13 08:29:02 -04002125 d = ERR_PTR(error);
2126 goto out;
2127}
2128EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2129
2130/*
2131 * Code common to create, mkdir, and mknod.
2132 */
2133int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumakerd91bfc42021-10-22 13:11:09 -04002134 struct nfs_fattr *fattr)
Benjamin Coddington406cd912019-09-13 08:29:02 -04002135{
2136 struct dentry *d;
2137
Anna Schumakercc6f3292021-10-22 13:11:10 -04002138 d = nfs_add_or_obtain(dentry, fhandle, fattr);
Benjamin Coddington406cd912019-09-13 08:29:02 -04002139 if (IS_ERR(d))
2140 return PTR_ERR(d);
2141
2142 /* Callers don't care */
2143 dput(d);
2144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002146EXPORT_SYMBOL_GPL(nfs_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148/*
2149 * Following a failed create operation, we drop the dentry rather
2150 * than retain a negative dentry. This avoids a problem in the event
2151 * that the operation succeeded on the server, but an error in the
2152 * reply path made it appear to have failed.
2153 */
Christian Brauner549c7292021-01-21 14:19:43 +01002154int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2155 struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 struct iattr attr;
Al Viroebfc3b42012-06-10 18:05:36 -04002158 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Niels de Vos1e8968c2013-12-17 18:20:16 +01002161 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002162 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 attr.ia_mode = mode;
2165 attr.ia_valid = ATTR_MODE;
2166
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002167 trace_nfs_create_enter(dir, dentry, open_flags);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002168 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002169 trace_nfs_create_exit(dir, dentry, open_flags, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (error != 0)
2171 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return 0;
2173out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 d_drop(dentry);
2175 return error;
2176}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002177EXPORT_SYMBOL_GPL(nfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179/*
2180 * See comments for nfs_proc_create regarding failed operations.
2181 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002182int
Christian Brauner549c7292021-01-21 14:19:43 +01002183nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2184 struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct iattr attr;
2187 int status;
2188
Niels de Vos1e8968c2013-12-17 18:20:16 +01002189 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002190 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 attr.ia_mode = mode;
2193 attr.ia_valid = ATTR_MODE;
2194
Trond Myklebust1ca42382013-08-21 12:36:04 -04002195 trace_nfs_mknod_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002197 trace_nfs_mknod_exit(dir, dentry, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (status != 0)
2199 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
2201out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 d_drop(dentry);
2203 return status;
2204}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002205EXPORT_SYMBOL_GPL(nfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207/*
2208 * See comments for nfs_proc_create regarding failed operations.
2209 */
Christian Brauner549c7292021-01-21 14:19:43 +01002210int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2211 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 struct iattr attr;
2214 int error;
2215
Niels de Vos1e8968c2013-12-17 18:20:16 +01002216 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002217 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 attr.ia_valid = ATTR_MODE;
2220 attr.ia_mode = mode | S_IFDIR;
2221
Trond Myklebust1ca42382013-08-21 12:36:04 -04002222 trace_nfs_mkdir_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002224 trace_nfs_mkdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (error != 0)
2226 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
2228out_err:
2229 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return error;
2231}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002232EXPORT_SYMBOL_GPL(nfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002234static void nfs_dentry_handle_enoent(struct dentry *dentry)
2235{
Al Virodc3f4192015-05-18 10:10:34 -04002236 if (simple_positive(dentry))
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002237 d_delete(dentry);
2238}
2239
Trond Myklebust9019fb32021-07-07 21:43:09 -04002240static void nfs_dentry_remove_handle_error(struct inode *dir,
2241 struct dentry *dentry, int error)
2242{
2243 switch (error) {
2244 case -ENOENT:
2245 d_delete(dentry);
Trond Myklebust00bdadc2021-12-17 15:36:57 -05002246 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2247 break;
Trond Myklebust9019fb32021-07-07 21:43:09 -04002248 case 0:
Trond Myklebust00bdadc2021-12-17 15:36:57 -05002249 nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
Trond Myklebust9019fb32021-07-07 21:43:09 -04002250 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2251 }
2252}
2253
Bryan Schumaker597d9282012-07-16 16:39:10 -04002254int nfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 int error;
2257
Niels de Vos1e8968c2013-12-17 18:20:16 +01002258 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002259 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Trond Myklebust1ca42382013-08-21 12:36:04 -04002261 trace_nfs_rmdir_enter(dir, dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002262 if (d_really_is_positive(dentry)) {
Al Viro884be172016-04-28 23:56:31 -04002263 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002264 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2265 /* Ensure the VFS deletes this inode */
2266 switch (error) {
2267 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00002268 clear_nlink(d_inode(dentry));
Trond Myklebustba6c0592013-08-30 12:24:25 -04002269 break;
2270 case -ENOENT:
2271 nfs_dentry_handle_enoent(dentry);
2272 }
Al Viro884be172016-04-28 23:56:31 -04002273 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002274 } else
2275 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
Trond Myklebust9019fb32021-07-07 21:43:09 -04002276 nfs_dentry_remove_handle_error(dir, dentry, error);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002277 trace_nfs_rmdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 return error;
2280}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002281EXPORT_SYMBOL_GPL(nfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283/*
2284 * Remove a file after making sure there are no pending writes,
2285 * and after checking that the file has only one user.
2286 *
2287 * We invalidate the attribute cache and free the inode prior to the operation
2288 * to avoid possible races if the server reuses the inode.
2289 */
2290static int nfs_safe_remove(struct dentry *dentry)
2291{
David Howells2b0143b2015-03-17 22:25:59 +00002292 struct inode *dir = d_inode(dentry->d_parent);
2293 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 int error = -EBUSY;
2295
Al Viro6de14722013-09-16 10:53:17 -04002296 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* If the dentry was sillyrenamed, we simply call d_delete() */
2299 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2300 error = 0;
2301 goto out;
2302 }
2303
Trond Myklebust1ca42382013-08-21 12:36:04 -04002304 trace_nfs_remove_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (inode != NULL) {
Trond Myklebust912678d2018-03-20 16:43:15 -04002306 error = NFS_PROTO(dir)->remove(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04002308 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 } else
Trond Myklebust912678d2018-03-20 16:43:15 -04002310 error = NFS_PROTO(dir)->remove(dir, dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002311 if (error == -ENOENT)
2312 nfs_dentry_handle_enoent(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002313 trace_nfs_remove_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314out:
2315 return error;
2316}
2317
2318/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
2319 * belongs to an active ".nfs..." file and we return -EBUSY.
2320 *
2321 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
2322 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002323int nfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 int error;
2326 int need_rehash = 0;
2327
Niels de Vos1e8968c2013-12-17 18:20:16 +01002328 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002329 dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Trond Myklebust1ca42382013-08-21 12:36:04 -04002331 trace_nfs_unlink_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 spin_lock(&dentry->d_lock);
Al Viro84d08fa2013-07-05 18:59:33 +04002333 if (d_count(dentry) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 spin_unlock(&dentry->d_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05002335 /* Start asynchronous writeout of the inode */
David Howells2b0143b2015-03-17 22:25:59 +00002336 write_inode_now(d_inode(dentry), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 error = nfs_sillyrename(dir, dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 if (!d_unhashed(dentry)) {
2341 __d_drop(dentry);
2342 need_rehash = 1;
2343 }
2344 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 error = nfs_safe_remove(dentry);
Trond Myklebust9019fb32021-07-07 21:43:09 -04002346 nfs_dentry_remove_handle_error(dir, dentry, error);
2347 if (need_rehash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 d_rehash(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002349out:
2350 trace_nfs_unlink_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return error;
2352}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002353EXPORT_SYMBOL_GPL(nfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Chuck Lever873101b2006-08-22 20:06:23 -04002355/*
2356 * To create a symbolic link, most file systems instantiate a new inode,
2357 * add a page to it containing the path, then write it out to the disk
2358 * using prepare_write/commit_write.
2359 *
2360 * Unfortunately the NFS client can't create the in-core inode first
2361 * because it needs a file handle to create an in-core inode (see
2362 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
2363 * symlink request has completed on the server.
2364 *
2365 * So instead we allocate a raw page, copy the symname into it, then do
2366 * the SYMLINK request with the page as the buffer. If it succeeds, we
2367 * now have a new file handle and can instantiate an in-core NFS inode
2368 * and move the raw page into its mapping.
2369 */
Christian Brauner549c7292021-01-21 14:19:43 +01002370int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2371 struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Chuck Lever873101b2006-08-22 20:06:23 -04002373 struct page *page;
2374 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04002376 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int error;
2378
Niels de Vos1e8968c2013-12-17 18:20:16 +01002379 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002380 dir->i_ino, dentry, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Chuck Lever873101b2006-08-22 20:06:23 -04002382 if (pathlen > PAGE_SIZE)
2383 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Chuck Lever873101b2006-08-22 20:06:23 -04002385 attr.ia_mode = S_IFLNK | S_IRWXUGO;
2386 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Al Viroe8ecde22016-01-14 17:52:59 -05002388 page = alloc_page(GFP_USER);
Trond Myklebust76566992008-06-11 15:44:22 -04002389 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04002390 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04002391
Al Viroe8ecde22016-01-14 17:52:59 -05002392 kaddr = page_address(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002393 memcpy(kaddr, symname, pathlen);
2394 if (pathlen < PAGE_SIZE)
2395 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
Chuck Lever873101b2006-08-22 20:06:23 -04002396
Trond Myklebust1ca42382013-08-21 12:36:04 -04002397 trace_nfs_symlink_enter(dir, dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -04002398 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002399 trace_nfs_symlink_exit(dir, dentry, error);
Chuck Lever873101b2006-08-22 20:06:23 -04002400 if (error != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01002401 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
Chuck Lever873101b2006-08-22 20:06:23 -04002402 dir->i_sb->s_id, dir->i_ino,
Al Viro6de14722013-09-16 10:53:17 -04002403 dentry, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04002405 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002406 return error;
2407 }
2408
Trond Myklebust342a67f2021-07-07 22:08:32 -04002409 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2410
Chuck Lever873101b2006-08-22 20:06:23 -04002411 /*
2412 * No big deal if we can't add this page to the page cache here.
2413 * READLINK will get the missing page from the server if needed.
2414 */
David Howells2b0143b2015-03-17 22:25:59 +00002415 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
Chuck Lever873101b2006-08-22 20:06:23 -04002416 GFP_KERNEL)) {
Chuck Lever873101b2006-08-22 20:06:23 -04002417 SetPageUptodate(page);
2418 unlock_page(page);
Rafael Aquinia0b54add2014-02-10 14:25:48 -08002419 /*
2420 * add_to_page_cache_lru() grabs an extra page refcount.
2421 * Drop it here to avoid leaking this page later.
2422 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002423 put_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002424 } else
2425 __free_page(page);
2426
Chuck Lever873101b2006-08-22 20:06:23 -04002427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002429EXPORT_SYMBOL_GPL(nfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Bryan Schumaker597d9282012-07-16 16:39:10 -04002431int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2433{
David Howells2b0143b2015-03-17 22:25:59 +00002434 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 int error;
2436
Al Viro6de14722013-09-16 10:53:17 -04002437 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2438 old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Trond Myklebust1fd10852013-08-21 13:54:44 -04002440 trace_nfs_link_enter(inode, dir, dentry);
Trond Myklebust9697d232007-10-02 21:58:05 -04002441 d_drop(dentry);
Trond Myklebust20497502021-12-15 16:38:15 -05002442 if (S_ISREG(inode->i_mode))
2443 nfs_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04002445 if (error == 0) {
Trond Myklebust342a67f2021-07-07 22:08:32 -04002446 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Al Viro7de9c6ee2010-10-23 11:11:40 -04002447 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04002448 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04002449 }
Trond Myklebust1fd10852013-08-21 13:54:44 -04002450 trace_nfs_link_exit(inode, dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return error;
2452}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002453EXPORT_SYMBOL_GPL(nfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455/*
2456 * RENAME
2457 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2458 * different file handle for the same inode after a rename (e.g. when
2459 * moving to a different directory). A fail-safe method to do so would
2460 * be to look up old_dir/old_name, create a link to new_dir/new_name and
2461 * rename the old file using the sillyrename stuff. This way, the original
2462 * file in old_dir will go away when the last process iput()s the inode.
2463 *
2464 * FIXED.
2465 *
2466 * It actually works quite well. One needs to have the possibility for
2467 * at least one ".nfs..." file in each directory the file ever gets
2468 * moved or linked to which happens automagically with the new
2469 * implementation that only depends on the dcache stuff instead of
2470 * using the inode layer
2471 *
2472 * Unfortunately, things are a little more complicated than indicated
2473 * above. For a cross-directory move, we want to make sure we can get
2474 * rid of the old inode after the operation. This means there must be
2475 * no pending writes (if it's a file), and the use count must be 1.
2476 * If these conditions are met, we can drop the dentries before doing
2477 * the rename.
2478 */
Christian Brauner549c7292021-01-21 14:19:43 +01002479int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2480 struct dentry *old_dentry, struct inode *new_dir,
2481 struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
David Howells2b0143b2015-03-17 22:25:59 +00002483 struct inode *old_inode = d_inode(old_dentry);
2484 struct inode *new_inode = d_inode(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002485 struct dentry *dentry = NULL, *rehash = NULL;
Jeff Layton80a491f2014-03-17 07:06:56 -04002486 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 int error = -EBUSY;
2488
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002489 if (flags)
2490 return -EINVAL;
2491
Al Viro6de14722013-09-16 10:53:17 -04002492 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2493 old_dentry, new_dentry,
Al Viro84d08fa2013-07-05 18:59:33 +04002494 d_count(new_dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Trond Myklebust70ded202013-08-21 12:08:45 -04002496 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05002498 * For non-directories, check whether the target is busy and if so,
2499 * make a copy of the dentry and then do a silly-rename. If the
2500 * silly-rename succeeds, the copied dentry is hashed and becomes
2501 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 */
Miklos Szeredi27226102009-12-03 15:58:56 -05002503 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2504 /*
2505 * To prevent any new references to the target during the
2506 * rename, we unhash the dentry in advance.
2507 */
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002508 if (!d_unhashed(new_dentry)) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002509 d_drop(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002510 rehash = new_dentry;
2511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Al Viro84d08fa2013-07-05 18:59:33 +04002513 if (d_count(new_dentry) > 2) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002514 int err;
2515
2516 /* copy the target dentry's name */
2517 dentry = d_alloc(new_dentry->d_parent,
2518 &new_dentry->d_name);
2519 if (!dentry)
2520 goto out;
2521
2522 /* silly-rename the existing target ... */
2523 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05002524 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05002525 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002526
2527 new_dentry = dentry;
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002528 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002529 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05002530 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Trond Myklebust6ff9d992021-12-15 16:38:16 -05002533 if (S_ISREG(old_inode->i_mode))
2534 nfs_sync_inode(old_inode);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002535 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
Jeff Layton80a491f2014-03-17 07:06:56 -04002536 if (IS_ERR(task)) {
2537 error = PTR_ERR(task);
2538 goto out;
2539 }
2540
2541 error = rpc_wait_for_completion_task(task);
Benjamin Coddington818a8db2017-06-16 11:13:00 -04002542 if (error != 0) {
2543 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2544 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2545 smp_wmb();
2546 } else
Jeff Layton80a491f2014-03-17 07:06:56 -04002547 error = task->tk_status;
2548 rpc_put_task(task);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002549 /* Ensure the inode attributes are revalidated */
2550 if (error == 0) {
2551 spin_lock(&old_inode->i_lock);
2552 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustac46b3d2021-03-08 14:42:54 -05002553 nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2554 NFS_INO_INVALID_CTIME |
2555 NFS_INO_REVAL_FORCED);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002556 spin_unlock(&old_inode->i_lock);
2557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558out:
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002559 if (rehash)
2560 d_rehash(rehash);
Trond Myklebust70ded202013-08-21 12:08:45 -04002561 trace_nfs_rename_exit(old_dir, old_dentry,
2562 new_dir, new_dentry, error);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002563 if (!error) {
2564 if (new_inode != NULL)
2565 nfs_drop_nlink(new_inode);
2566 /*
2567 * The d_move() should be here instead of in an async RPC completion
2568 * handler because we need the proper locks to move the dentry. If
2569 * we're interrupted by a signal, the async RPC completion handler
2570 * should mark the directories for revalidation.
2571 */
2572 d_move(old_dentry, new_dentry);
Trond Myklebustd8032242017-11-06 15:28:04 -05002573 nfs_set_verifier(old_dentry,
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002574 nfs_save_change_attribute(new_dir));
2575 } else if (error == -ENOENT)
2576 nfs_dentry_handle_enoent(old_dentry);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /* new dentry created? */
2579 if (dentry)
2580 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return error;
2582}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002583EXPORT_SYMBOL_GPL(nfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002585static DEFINE_SPINLOCK(nfs_access_lru_lock);
2586static LIST_HEAD(nfs_access_lru_list);
2587static atomic_long_t nfs_access_nr_entries;
2588
Trond Myklebusta8b373e2020-02-08 09:14:11 -05002589static unsigned long nfs_access_max_cachesize = 4*1024*1024;
Trond Myklebust3a505842014-07-21 13:53:48 -04002590module_param(nfs_access_max_cachesize, ulong, 0644);
2591MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2592
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002593static void nfs_access_free_entry(struct nfs_access_entry *entry)
2594{
NeilBrown6238aec2021-09-28 09:47:57 +10002595 put_group_info(entry->group_info);
NeilBrownf682a392014-07-14 11:28:20 +10002596 kfree_rcu(entry, rcu_head);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002597 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002598 atomic_long_dec(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002599 smp_mb__after_atomic();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002600}
2601
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002602static void nfs_access_free_list(struct list_head *head)
2603{
2604 struct nfs_access_entry *cache;
2605
2606 while (!list_empty(head)) {
2607 cache = list_entry(head->next, struct nfs_access_entry, lru);
2608 list_del(&cache->lru);
2609 nfs_access_free_entry(cache);
2610 }
2611}
2612
Trond Myklebust3a505842014-07-21 13:53:48 -04002613static unsigned long
2614nfs_do_access_cache_scan(unsigned int nr_to_scan)
Trond Myklebust979df722006-07-25 11:28:19 -04002615{
2616 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002617 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04002618 struct nfs_access_entry *cache;
Dave Chinner1ab6c492013-08-28 10:18:09 +10002619 long freed = 0;
Trond Myklebust979df722006-07-25 11:28:19 -04002620
Trond Myklebusta50f7952007-06-05 19:23:43 -04002621 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002622 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04002623 struct inode *inode;
2624
2625 if (nr_to_scan-- == 0)
2626 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002627 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04002628 spin_lock(&inode->i_lock);
2629 if (list_empty(&nfsi->access_cache_entry_lru))
2630 goto remove_lru_entry;
2631 cache = list_entry(nfsi->access_cache_entry_lru.next,
2632 struct nfs_access_entry, lru);
2633 list_move(&cache->lru, &head);
2634 rb_erase(&cache->rb_node, &nfsi->access_cache);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002635 freed++;
Trond Myklebust979df722006-07-25 11:28:19 -04002636 if (!list_empty(&nfsi->access_cache_entry_lru))
2637 list_move_tail(&nfsi->access_cache_inode_lru,
2638 &nfs_access_lru_list);
2639 else {
2640remove_lru_entry:
2641 list_del_init(&nfsi->access_cache_inode_lru);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002642 smp_mb__before_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002643 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002644 smp_mb__after_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002645 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002646 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002647 }
2648 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002649 nfs_access_free_list(&head);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002650 return freed;
2651}
2652
2653unsigned long
Trond Myklebust3a505842014-07-21 13:53:48 -04002654nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2655{
2656 int nr_to_scan = sc->nr_to_scan;
2657 gfp_t gfp_mask = sc->gfp_mask;
2658
2659 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2660 return SHRINK_STOP;
2661 return nfs_do_access_cache_scan(nr_to_scan);
2662}
2663
2664
2665unsigned long
Dave Chinner1ab6c492013-08-28 10:18:09 +10002666nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2667{
Glauber Costa55f841c2013-08-28 10:17:53 +10002668 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
Trond Myklebust979df722006-07-25 11:28:19 -04002669}
2670
Trond Myklebust3a505842014-07-21 13:53:48 -04002671static void
2672nfs_access_cache_enforce_limit(void)
2673{
2674 long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2675 unsigned long diff;
2676 unsigned int nr_to_scan;
2677
2678 if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2679 return;
2680 nr_to_scan = 100;
2681 diff = nr_entries - nfs_access_max_cachesize;
2682 if (diff < nr_to_scan)
2683 nr_to_scan = diff;
2684 nfs_do_access_cache_scan(nr_to_scan);
2685}
2686
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002687static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002688{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002689 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002690 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002691 struct nfs_access_entry *entry;
2692
2693 /* Unhook entries from the cache */
2694 while ((n = rb_first(root_node)) != NULL) {
2695 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2696 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002697 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002698 }
2699 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002700}
2701
2702void nfs_access_zap_cache(struct inode *inode)
2703{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002704 LIST_HEAD(head);
2705
2706 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2707 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002708 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002709 spin_lock(&nfs_access_lru_lock);
2710 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002711 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002712
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002713 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002714 __nfs_access_zap_cache(NFS_I(inode), &head);
2715 spin_unlock(&inode->i_lock);
2716 spin_unlock(&nfs_access_lru_lock);
2717 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002718}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04002719EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002720
NeilBrown6238aec2021-09-28 09:47:57 +10002721static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
2722{
2723 struct group_info *ga, *gb;
2724 int g;
2725
2726 if (uid_lt(a->fsuid, b->fsuid))
2727 return -1;
2728 if (uid_gt(a->fsuid, b->fsuid))
2729 return 1;
2730
2731 if (gid_lt(a->fsgid, b->fsgid))
2732 return -1;
2733 if (gid_gt(a->fsgid, b->fsgid))
2734 return 1;
2735
2736 ga = a->group_info;
2737 gb = b->group_info;
2738 if (ga == gb)
2739 return 0;
2740 if (ga == NULL)
2741 return -1;
2742 if (gb == NULL)
2743 return 1;
2744 if (ga->ngroups < gb->ngroups)
2745 return -1;
2746 if (ga->ngroups > gb->ngroups)
2747 return 1;
2748
2749 for (g = 0; g < ga->ngroups; g++) {
2750 if (gid_lt(ga->gid[g], gb->gid[g]))
2751 return -1;
2752 if (gid_gt(ga->gid[g], gb->gid[g]))
2753 return 1;
2754 }
2755 return 0;
2756}
2757
NeilBrownb68572e2018-12-03 11:30:30 +11002758static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002759{
2760 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002761
2762 while (n != NULL) {
NeilBrownb68572e2018-12-03 11:30:30 +11002763 struct nfs_access_entry *entry =
2764 rb_entry(n, struct nfs_access_entry, rb_node);
NeilBrown6238aec2021-09-28 09:47:57 +10002765 int cmp = access_cmp(cred, entry);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002766
NeilBrownb68572e2018-12-03 11:30:30 +11002767 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002768 n = n->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002769 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002770 n = n->rb_right;
2771 else
2772 return entry;
2773 }
2774 return NULL;
2775}
2776
NeilBrownb5e7b592021-09-28 09:47:57 +10002777static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Chuck Lever55296802005-08-18 11:24:09 -07002779 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002780 struct nfs_access_entry *cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002781 bool retry = true;
2782 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002784 spin_lock(&inode->i_lock);
Trond Myklebust57b69182016-06-03 17:07:19 -04002785 for(;;) {
2786 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2787 goto out_zap;
2788 cache = nfs_access_search_rbtree(inode, cred);
2789 err = -ENOENT;
2790 if (cache == NULL)
2791 goto out;
2792 /* Found an entry, is our attribute cache valid? */
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002793 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
Trond Myklebust57b69182016-06-03 17:07:19 -04002794 break;
Trond Myklebust5c965db2020-01-06 15:39:36 -05002795 if (!retry)
2796 break;
Trond Myklebust57b69182016-06-03 17:07:19 -04002797 err = -ECHILD;
2798 if (!may_block)
2799 goto out;
Trond Myklebust57b69182016-06-03 17:07:19 -04002800 spin_unlock(&inode->i_lock);
2801 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2802 if (err)
2803 return err;
2804 spin_lock(&inode->i_lock);
2805 retry = false;
2806 }
NeilBrownb5e7b592021-09-28 09:47:57 +10002807 *mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002808 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002809 err = 0;
2810out:
2811 spin_unlock(&inode->i_lock);
2812 return err;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002813out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002814 spin_unlock(&inode->i_lock);
2815 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002816 return -ENOENT;
2817}
2818
NeilBrownb5e7b592021-09-28 09:47:57 +10002819static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
NeilBrownf682a392014-07-14 11:28:20 +10002820{
2821 /* Only check the most recently returned cache entry,
2822 * but do it without locking.
2823 */
2824 struct nfs_inode *nfsi = NFS_I(inode);
2825 struct nfs_access_entry *cache;
2826 int err = -ECHILD;
2827 struct list_head *lh;
2828
2829 rcu_read_lock();
2830 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2831 goto out;
Madhuparna Bhowmik9f01eb52019-12-10 11:16:39 +05302832 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
NeilBrownf682a392014-07-14 11:28:20 +10002833 cache = list_entry(lh, struct nfs_access_entry, lru);
2834 if (lh == &nfsi->access_cache_entry_lru ||
NeilBrown6238aec2021-09-28 09:47:57 +10002835 access_cmp(cred, cache) != 0)
NeilBrownf682a392014-07-14 11:28:20 +10002836 cache = NULL;
2837 if (cache == NULL)
2838 goto out;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002839 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
NeilBrownf682a392014-07-14 11:28:20 +10002840 goto out;
NeilBrownb5e7b592021-09-28 09:47:57 +10002841 *mask = cache->mask;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002842 err = 0;
NeilBrownf682a392014-07-14 11:28:20 +10002843out:
2844 rcu_read_unlock();
2845 return err;
2846}
2847
NeilBrownb5e7b592021-09-28 09:47:57 +10002848int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2849 u32 *mask, bool may_block)
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002850{
2851 int status;
2852
NeilBrownb5e7b592021-09-28 09:47:57 +10002853 status = nfs_access_get_cached_rcu(inode, cred, mask);
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002854 if (status != 0)
NeilBrownb5e7b592021-09-28 09:47:57 +10002855 status = nfs_access_get_cached_locked(inode, cred, mask,
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002856 may_block);
2857
2858 return status;
2859}
2860EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2861
NeilBrown73fbb3f2021-09-28 09:47:57 +10002862static void nfs_access_add_rbtree(struct inode *inode,
2863 struct nfs_access_entry *set,
2864 const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002865{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002866 struct nfs_inode *nfsi = NFS_I(inode);
2867 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002868 struct rb_node **p = &root_node->rb_node;
2869 struct rb_node *parent = NULL;
2870 struct nfs_access_entry *entry;
NeilBrownb68572e2018-12-03 11:30:30 +11002871 int cmp;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002872
2873 spin_lock(&inode->i_lock);
2874 while (*p != NULL) {
2875 parent = *p;
2876 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
NeilBrown6238aec2021-09-28 09:47:57 +10002877 cmp = access_cmp(cred, entry);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002878
NeilBrownb68572e2018-12-03 11:30:30 +11002879 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002880 p = &parent->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002881 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002882 p = &parent->rb_right;
2883 else
2884 goto found;
2885 }
2886 rb_link_node(&set->rb_node, parent, p);
2887 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002888 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002889 spin_unlock(&inode->i_lock);
2890 return;
2891found:
2892 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002893 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2894 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002895 spin_unlock(&inode->i_lock);
2896 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
NeilBrown73fbb3f2021-09-28 09:47:57 +10002899void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
2900 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002902 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2903 if (cache == NULL)
2904 return;
2905 RB_CLEAR_NODE(&cache->rb_node);
NeilBrown6238aec2021-09-28 09:47:57 +10002906 cache->fsuid = cred->fsuid;
2907 cache->fsgid = cred->fsgid;
2908 cache->group_info = get_group_info(cred->group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002910
NeilBrownf682a392014-07-14 11:28:20 +10002911 /* The above field assignments must be visible
2912 * before this item appears on the lru. We cannot easily
2913 * use rcu_assign_pointer, so just force the memory barrier.
2914 */
2915 smp_wmb();
NeilBrown73fbb3f2021-09-28 09:47:57 +10002916 nfs_access_add_rbtree(inode, cache, cred);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002917
2918 /* Update accounting */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002919 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002920 atomic_long_inc(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002921 smp_mb__after_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002922
2923 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002924 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002925 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002926 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2927 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2928 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002929 spin_unlock(&nfs_access_lru_lock);
2930 }
Trond Myklebust3a505842014-07-21 13:53:48 -04002931 nfs_access_cache_enforce_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002933EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2934
Anna Schumaker3c181822017-07-26 10:14:55 -04002935#define NFS_MAY_READ (NFS_ACCESS_READ)
2936#define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
2937 NFS_ACCESS_EXTEND | \
2938 NFS_ACCESS_DELETE)
2939#define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
2940 NFS_ACCESS_EXTEND)
Trond Myklebustecbb9032017-07-11 17:54:35 -04002941#define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
Anna Schumaker3c181822017-07-26 10:14:55 -04002942#define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
2943#define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002944static int
Trond Myklebustecbb9032017-07-11 17:54:35 -04002945nfs_access_calc_mask(u32 access_result, umode_t umode)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002946{
2947 int mask = 0;
2948
2949 if (access_result & NFS_MAY_READ)
2950 mask |= MAY_READ;
Trond Myklebustecbb9032017-07-11 17:54:35 -04002951 if (S_ISDIR(umode)) {
2952 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
2953 mask |= MAY_WRITE;
2954 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
2955 mask |= MAY_EXEC;
2956 } else if (S_ISREG(umode)) {
2957 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2958 mask |= MAY_WRITE;
2959 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
2960 mask |= MAY_EXEC;
2961 } else if (access_result & NFS_MAY_WRITE)
2962 mask |= MAY_WRITE;
Trond Myklebust15d4b732017-07-11 17:54:32 -04002963 return mask;
2964}
2965
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002966void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2967{
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002968 entry->mask = access_result;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002969}
2970EXPORT_SYMBOL_GPL(nfs_access_set_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
NeilBrownb68572e2018-12-03 11:30:30 +11002972static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 struct nfs_access_entry cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002975 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
Trond Myklebuste8194b72020-01-06 15:25:12 -05002976 int cache_mask = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 int status;
2978
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002979 trace_nfs_access_enter(inode);
2980
NeilBrownb5e7b592021-09-28 09:47:57 +10002981 status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (status == 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002983 goto out_cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
NeilBrownf3324a22014-07-14 11:28:20 +10002985 status = -ECHILD;
Trond Myklebust57b69182016-06-03 17:07:19 -04002986 if (!may_block)
NeilBrownf3324a22014-07-14 11:28:20 +10002987 goto out;
2988
Anna Schumaker1750d922017-07-26 12:00:21 -04002989 /*
2990 * Determine which access bits we want to ask for...
2991 */
2992 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
Frank van der Linden72832a22020-06-23 22:38:58 +00002993 if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
2994 cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
2995 NFS_ACCESS_XALIST;
2996 }
Anna Schumaker1750d922017-07-26 12:00:21 -04002997 if (S_ISDIR(inode->i_mode))
2998 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
2999 else
3000 cache.mask |= NFS_ACCESS_EXECUTE;
NeilBrown73fbb3f2021-09-28 09:47:57 +10003001 status = NFS_PROTO(inode)->access(inode, &cache, cred);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04003002 if (status != 0) {
3003 if (status == -ESTALE) {
Suresh Jayaramana71ee332009-03-10 20:33:21 -04003004 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04003005 nfs_set_inode_stale(inode);
3006 else
3007 nfs_zap_caches(inode);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04003008 }
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003009 goto out;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04003010 }
NeilBrown73fbb3f2021-09-28 09:47:57 +10003011 nfs_access_add_cache(inode, &cache, cred);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003012out_cached:
Trond Myklebustecbb9032017-07-11 17:54:35 -04003013 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
Trond Myklebustbd8b2442017-07-11 17:54:34 -04003014 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003015 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016out:
Trond Myklebuste8194b72020-01-06 15:25:12 -05003017 trace_nfs_access_exit(inode, mask, cache_mask, status);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003018 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
Trond Myklebustaf22f942007-08-10 17:45:10 -04003021static int nfs_open_permission_mask(int openflags)
3022{
3023 int mask = 0;
3024
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05003025 if (openflags & __FMODE_EXEC) {
3026 /* ONLY check exec rights */
3027 mask = MAY_EXEC;
3028 } else {
3029 if ((openflags & O_ACCMODE) != O_WRONLY)
3030 mask |= MAY_READ;
3031 if ((openflags & O_ACCMODE) != O_RDONLY)
3032 mask |= MAY_WRITE;
3033 }
3034
Trond Myklebustaf22f942007-08-10 17:45:10 -04003035 return mask;
3036}
3037
NeilBrownb68572e2018-12-03 11:30:30 +11003038int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
Trond Myklebustaf22f942007-08-10 17:45:10 -04003039{
3040 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3041}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04003042EXPORT_SYMBOL_GPL(nfs_may_open);
Trond Myklebustaf22f942007-08-10 17:45:10 -04003043
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003044static int nfs_execute_ok(struct inode *inode, int mask)
3045{
3046 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust21c3ba72016-12-16 18:40:03 -05003047 int ret = 0;
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003048
Trond Myklebust38258272018-07-24 14:27:11 -04003049 if (S_ISDIR(inode->i_mode))
3050 return 0;
Trond Myklebust720869e2021-04-13 09:41:16 -04003051 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
Trond Myklebust21c3ba72016-12-16 18:40:03 -05003052 if (mask & MAY_NOT_BLOCK)
3053 return -ECHILD;
3054 ret = __nfs_revalidate_inode(server, inode);
3055 }
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003056 if (ret == 0 && !execute_ok(inode))
3057 ret = -EACCES;
3058 return ret;
3059}
3060
Christian Brauner549c7292021-01-21 14:19:43 +01003061int nfs_permission(struct user_namespace *mnt_userns,
3062 struct inode *inode,
3063 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
NeilBrownb68572e2018-12-03 11:30:30 +11003065 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 int res = 0;
3067
Chuck Lever91d5b472006-03-20 13:44:14 -05003068 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
3069
Al Viroe6305c42008-07-15 21:03:57 -04003070 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 goto out;
3072 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04003073 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 goto force_lookup;
3075
3076 switch (inode->i_mode & S_IFMT) {
3077 case S_IFLNK:
3078 goto out;
3079 case S_IFREG:
Trond Myklebust762674f2015-12-26 21:54:58 -05003080 if ((mask & MAY_OPEN) &&
3081 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 break;
3084 case S_IFDIR:
3085 /*
3086 * Optimize away all write operations, since the server
3087 * will check permissions when we perform the op.
3088 */
3089 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
3090 goto out;
3091 }
3092
3093force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (!NFS_PROTO(inode)->access)
3095 goto out_notsup;
3096
Zhouyi Zhoueb095c12020-03-06 03:45:26 +00003097 res = nfs_do_access(inode, cred, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098out:
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003099 if (!res && (mask & MAY_EXEC))
3100 res = nfs_execute_ok(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +02003101
Niels de Vos1e8968c2013-12-17 18:20:16 +01003102 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05003103 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return res;
3105out_notsup:
NeilBrownd51ac1a2014-07-14 11:28:20 +10003106 if (mask & MAY_NOT_BLOCK)
3107 return -ECHILD;
3108
Trond Myklebust720869e2021-04-13 09:41:16 -04003109 res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3110 NFS_INO_INVALID_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (res == 0)
Christian Brauner47291ba2021-01-21 14:19:24 +01003112 res = generic_permission(&init_user_ns, inode, mask);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05003113 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04003115EXPORT_SYMBOL_GPL(nfs_permission);