blob: a3de586d21e2bf17808ac646d1d4044e4cc785a7 [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 Myklebust311324a2014-02-07 17:02:08 -050083 spin_lock(&dir->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -040084 if (list_empty(&nfsi->open_files) &&
85 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
Trond Myklebustac46b3d2021-03-08 14:42:54 -050086 nfs_set_cache_invalid(dir,
87 NFS_INO_INVALID_DATA |
88 NFS_INO_REVAL_FORCED);
Trond Myklebust311324a2014-02-07 17:02:08 -050089 list_add(&ctx->list, &nfsi->open_files);
Trond Myklebustff81dfb2021-09-28 14:33:44 -040090 clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
Trond Myklebust311324a2014-02-07 17:02:08 -050091 spin_unlock(&dir->i_lock);
Trond Myklebust0c030802011-07-30 12:45:35 -040092 return ctx;
93 }
94 return ERR_PTR(-ENOMEM);
Bryan Schumaker480c2002011-03-23 14:48:29 -040095}
96
Trond Myklebust311324a2014-02-07 17:02:08 -050097static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
Bryan Schumaker480c2002011-03-23 14:48:29 -040098{
Trond Myklebust311324a2014-02-07 17:02:08 -050099 spin_lock(&dir->i_lock);
100 list_del(&ctx->list);
101 spin_unlock(&dir->i_lock);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400102 kfree(ctx);
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Open file
107 */
108static int
109nfs_opendir(struct inode *inode, struct file *filp)
110{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400111 int res = 0;
112 struct nfs_open_dir_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Al Viro6de14722013-09-16 10:53:17 -0400114 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400115
116 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500117
Trond Myklebust93b89592020-11-01 15:24:41 -0500118 ctx = alloc_nfs_open_dir_context(inode);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400119 if (IS_ERR(ctx)) {
120 res = PTR_ERR(ctx);
121 goto out;
122 }
123 filp->private_data = ctx;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400124out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return res;
126}
127
Bryan Schumaker480c2002011-03-23 14:48:29 -0400128static int
129nfs_closedir(struct inode *inode, struct file *filp)
130{
Al Viroa4555892014-10-21 20:11:25 -0400131 put_nfs_open_dir_context(file_inode(filp), filp->private_data);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400132 return 0;
133}
134
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400135struct nfs_cache_array_entry {
136 u64 cookie;
137 u64 ino;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500138 const char *name;
139 unsigned int name_len;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500140 unsigned char d_type;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400141};
142
143struct nfs_cache_array {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400144 u64 last_cookie;
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500145 unsigned int size;
146 unsigned char page_full : 1,
Trond Myklebust762567b2020-11-04 08:32:19 -0500147 page_is_eof : 1,
148 cookies_are_ordered : 1;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -0500149 struct nfs_cache_array_entry array[];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400150};
151
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500152struct nfs_readdir_descriptor {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct file *file;
154 struct page *page;
Al Viro23db8622013-05-17 16:34:50 -0400155 struct dir_context *ctx;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500156 pgoff_t page_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500157 u64 dir_cookie;
Trond Myklebust0aded702010-11-30 21:56:32 -0500158 u64 last_cookie;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500159 u64 dup_cookie;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000160 loff_t current_index;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500161 loff_t prev_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400162
Trond Myklebustb593c092020-11-02 20:06:12 -0500163 __be32 verf[NFS_DIR_VERIFIER_SIZE];
Trond Myklebusta1147b82020-02-05 09:01:52 -0500164 unsigned long dir_verifier;
Neil Brown1f4eab72007-04-16 09:35:27 +1000165 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400166 unsigned long gencount;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500167 unsigned long attr_gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400168 unsigned int cache_entry_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500169 signed char duped;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400170 bool plus;
171 bool eof;
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500174static void nfs_readdir_array_init(struct nfs_cache_array *array)
175{
176 memset(array, 0, sizeof(struct nfs_cache_array));
177}
178
179static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie)
Trond Myklebust4b310312020-02-02 17:53:53 -0500180{
181 struct nfs_cache_array *array;
182
183 array = kmap_atomic(page);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500184 nfs_readdir_array_init(array);
185 array->last_cookie = last_cookie;
Trond Myklebust762567b2020-11-04 08:32:19 -0500186 array->cookies_are_ordered = 1;
Trond Myklebust4b310312020-02-02 17:53:53 -0500187 kunmap_atomic(array);
188}
189
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400190/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400191 * we are freeing strings created by nfs_add_to_readdir_array()
192 */
193static
Trond Myklebust11de3b12010-12-01 14:17:06 -0500194void nfs_readdir_clear_array(struct page *page)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400195{
Trond Myklebust11de3b12010-12-01 14:17:06 -0500196 struct nfs_cache_array *array;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400197 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500198
Cong Wang2b86ce22011-11-25 23:14:33 +0800199 array = kmap_atomic(page);
Benjamin Coddingtonb044f642017-03-10 17:07:46 -0500200 for (i = 0; i < array->size; i++)
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500201 kfree(array->array[i].name);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500202 nfs_readdir_array_init(array);
Cong Wang2b86ce22011-11-25 23:14:33 +0800203 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400204}
205
Trond Myklebust35df59d2020-11-06 20:38:47 -0500206static struct page *
207nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
208{
209 struct page *page = alloc_page(gfp_flags);
210 if (page)
211 nfs_readdir_page_init_array(page, last_cookie);
212 return page;
213}
214
215static void nfs_readdir_page_array_free(struct page *page)
216{
217 if (page) {
218 nfs_readdir_clear_array(page);
219 put_page(page);
220 }
221}
222
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500223static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
224{
225 array->page_is_eof = 1;
226 array->page_full = 1;
227}
228
229static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
230{
231 return array->page_full;
232}
233
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400234/*
235 * the caller is responsible for freeing qstr.name
236 * when called by nfs_readdir_add_to_array, the strings will be freed in
237 * nfs_clear_readdir_array()
238 */
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500239static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400240{
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500241 const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
242
Catalin Marinas04e4bd12010-11-11 12:53:47 +0000243 /*
244 * Avoid a kmemleak false positive. The pointer to the name is stored
245 * in a page cache page which kmemleak does not scan.
246 */
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500247 if (ret != NULL)
248 kmemleak_not_leak(ret);
249 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400250}
251
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500252/*
253 * Check that the next array entry lies entirely within the page bounds
254 */
255static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
256{
257 struct nfs_cache_array_entry *cache_entry;
258
259 if (array->page_full)
260 return -ENOSPC;
261 cache_entry = &array->array[array->size + 1];
262 if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
263 array->page_full = 1;
264 return -ENOSPC;
265 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400266 return 0;
267}
268
269static
270int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
271{
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500272 struct nfs_cache_array *array;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400273 struct nfs_cache_array_entry *cache_entry;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500274 const char *name;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400275 int ret;
276
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500277 name = nfs_readdir_copy_name(entry->name, entry->len);
278 if (!name)
279 return -ENOMEM;
Trond Myklebust30200932010-11-20 15:18:22 -0500280
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500281 array = kmap_atomic(page);
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500282 ret = nfs_readdir_array_can_expand(array);
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500283 if (ret) {
284 kfree(name);
Trond Myklebust30200932010-11-20 15:18:22 -0500285 goto out;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500286 }
Trond Myklebust30200932010-11-20 15:18:22 -0500287
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500288 cache_entry = &array->array[array->size];
Trond Myklebust4a201d62010-10-23 14:53:23 -0400289 cache_entry->cookie = entry->prev_cookie;
290 cache_entry->ino = entry->ino;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500291 cache_entry->d_type = entry->d_type;
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500292 cache_entry->name_len = entry->len;
293 cache_entry->name = name;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400294 array->last_cookie = entry->cookie;
Trond Myklebust762567b2020-11-04 08:32:19 -0500295 if (array->last_cookie <= cache_entry->cookie)
296 array->cookies_are_ordered = 0;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500297 array->size++;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500298 if (entry->eof != 0)
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500299 nfs_readdir_array_set_eof(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400300out:
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500301 kunmap_atomic(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400302 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400303}
304
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500305static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
306 pgoff_t index, u64 last_cookie)
307{
308 struct page *page;
309
310 page = grab_cache_page(mapping, index);
311 if (page && !PageUptodate(page)) {
312 nfs_readdir_page_init_array(page, last_cookie);
313 if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
314 nfs_zap_mapping(mapping->host, mapping);
315 SetPageUptodate(page);
316 }
317
318 return page;
319}
320
321static u64 nfs_readdir_page_last_cookie(struct page *page)
322{
323 struct nfs_cache_array *array;
324 u64 ret;
325
326 array = kmap_atomic(page);
327 ret = array->last_cookie;
328 kunmap_atomic(array);
329 return ret;
330}
331
332static bool nfs_readdir_page_needs_filling(struct page *page)
333{
334 struct nfs_cache_array *array;
335 bool ret;
336
337 array = kmap_atomic(page);
338 ret = !nfs_readdir_array_is_full(array);
339 kunmap_atomic(array);
340 return ret;
341}
342
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500343static void nfs_readdir_page_set_eof(struct page *page)
344{
345 struct nfs_cache_array *array;
346
347 array = kmap_atomic(page);
348 nfs_readdir_array_set_eof(array);
349 kunmap_atomic(array);
350}
351
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500352static void nfs_readdir_page_unlock_and_put(struct page *page)
353{
354 unlock_page(page);
355 put_page(page);
356}
357
358static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
359 pgoff_t index, u64 cookie)
360{
361 struct page *page;
362
363 page = nfs_readdir_page_get_locked(mapping, index, cookie);
364 if (page) {
365 if (nfs_readdir_page_last_cookie(page) == cookie)
366 return page;
367 nfs_readdir_page_unlock_and_put(page);
368 }
369 return NULL;
370}
371
Trond Myklebust59e356a2020-02-03 14:49:33 -0500372static inline
373int is_32bit_api(void)
374{
375#ifdef CONFIG_COMPAT
376 return in_compat_syscall();
377#else
378 return (BITS_PER_LONG == 32);
379#endif
380}
381
382static
383bool nfs_readdir_use_cookie(const struct file *filp)
384{
385 if ((filp->f_mode & FMODE_32BITHASH) ||
386 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
387 return false;
388 return true;
389}
390
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500391static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
392 struct nfs_readdir_descriptor *desc)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400393{
Al Viro23db8622013-05-17 16:34:50 -0400394 loff_t diff = desc->ctx->pos - desc->current_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400395 unsigned int index;
396
397 if (diff < 0)
398 goto out_eof;
399 if (diff >= array->size) {
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500400 if (array->page_is_eof)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400401 goto out_eof;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400402 return -EAGAIN;
403 }
404
405 index = (unsigned int)diff;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500406 desc->dir_cookie = array->array[index].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400407 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400408 return 0;
409out_eof:
Thomas Meyer6089dd02017-10-07 16:02:21 +0200410 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400411 return -EBADCOOKIE;
412}
413
Jeff Layton4db72b42014-01-28 13:47:46 -0500414static bool
415nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
416{
Trond Myklebust2929bc32021-09-28 12:37:05 -0400417 if (nfsi->cache_validity & (NFS_INO_INVALID_CHANGE |
418 NFS_INO_INVALID_DATA))
Jeff Layton4db72b42014-01-28 13:47:46 -0500419 return false;
420 smp_rmb();
421 return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
422}
423
Trond Myklebust762567b2020-11-04 08:32:19 -0500424static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
425 u64 cookie)
426{
427 if (!array->cookies_are_ordered)
428 return true;
429 /* Optimisation for monotonically increasing cookies */
430 if (cookie >= array->last_cookie)
431 return false;
432 if (array->size && cookie < array->array[0].cookie)
433 return false;
434 return true;
435}
436
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500437static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
438 struct nfs_readdir_descriptor *desc)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400439{
440 int i;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400441 loff_t new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400442 int status = -EAGAIN;
443
Trond Myklebust762567b2020-11-04 08:32:19 -0500444 if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
445 goto check_eof;
446
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400447 for (i = 0; i < array->size; i++) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500448 if (array->array[i].cookie == desc->dir_cookie) {
Al Viro496ad9a2013-01-23 17:07:38 -0500449 struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
Trond Myklebust0c030802011-07-30 12:45:35 -0400450
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400451 new_pos = desc->current_index + i;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500452 if (desc->attr_gencount != nfsi->attr_gencount ||
Jeff Layton4db72b42014-01-28 13:47:46 -0500453 !nfs_readdir_inode_mapping_valid(nfsi)) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500454 desc->duped = 0;
455 desc->attr_gencount = nfsi->attr_gencount;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500456 } else if (new_pos < desc->prev_index) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500457 if (desc->duped > 0
458 && desc->dup_cookie == desc->dir_cookie) {
Trond Myklebust0c030802011-07-30 12:45:35 -0400459 if (printk_ratelimit()) {
Al Viro6de14722013-09-16 10:53:17 -0400460 pr_notice("NFS: directory %pD2 contains a readdir loop."
Trond Myklebust0c030802011-07-30 12:45:35 -0400461 "Please contact your server vendor. "
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500462 "The file: %s has duplicate cookie %llu\n",
463 desc->file, array->array[i].name, desc->dir_cookie);
Trond Myklebust0c030802011-07-30 12:45:35 -0400464 }
465 status = -ELOOP;
466 goto out;
467 }
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500468 desc->dup_cookie = desc->dir_cookie;
469 desc->duped = -1;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400470 }
Trond Myklebust59e356a2020-02-03 14:49:33 -0500471 if (nfs_readdir_use_cookie(desc->file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500472 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500473 else
474 desc->ctx->pos = new_pos;
475 desc->prev_index = new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400476 desc->cache_entry_index = i;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500477 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400478 }
479 }
Trond Myklebust762567b2020-11-04 08:32:19 -0500480check_eof:
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500481 if (array->page_is_eof) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500482 status = -EBADCOOKIE;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500483 if (desc->dir_cookie == array->last_cookie)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200484 desc->eof = true;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500485 }
Trond Myklebust0c030802011-07-30 12:45:35 -0400486out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400487 return status;
488}
489
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500490static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400491{
492 struct nfs_cache_array *array;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500493 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400494
Trond Myklebusted092222020-11-01 13:34:32 -0500495 array = kmap_atomic(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400496
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500497 if (desc->dir_cookie == 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400498 status = nfs_readdir_search_for_pos(array, desc);
499 else
500 status = nfs_readdir_search_for_cookie(array, desc);
501
Trond Myklebust47c716c2010-12-07 12:44:56 -0500502 if (status == -EAGAIN) {
Trond Myklebust0aded702010-11-30 21:56:32 -0500503 desc->last_cookie = array->last_cookie;
Trond Myklebuste47c0852011-03-23 08:43:09 -0400504 desc->current_index += array->size;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500505 desc->page_index++;
506 }
Trond Myklebusted092222020-11-01 13:34:32 -0500507 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400508 return status;
509}
510
511/* Fill a page with xdr information before transferring to the cache page */
Trond Myklebust93b89592020-11-01 15:24:41 -0500512static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
Trond Myklebustb593c092020-11-02 20:06:12 -0500513 __be32 *verf, u64 cookie,
514 struct page **pages, size_t bufsize,
515 __be32 *verf_res)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400516{
Trond Myklebust82e22a52020-11-02 17:34:23 -0500517 struct inode *inode = file_inode(desc->file);
Trond Myklebust82e22a52020-11-02 17:34:23 -0500518 struct nfs_readdir_arg arg = {
519 .dentry = file_dentry(desc->file),
520 .cred = desc->file->f_cred,
Trond Myklebustb593c092020-11-02 20:06:12 -0500521 .verf = verf,
Trond Myklebust82e22a52020-11-02 17:34:23 -0500522 .cookie = cookie,
523 .pages = pages,
524 .page_len = bufsize,
525 .plus = desc->plus,
526 };
527 struct nfs_readdir_res res = {
528 .verf = verf_res,
529 };
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400530 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int error;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 again:
534 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400535 gencount = nfs_inc_attr_generation_counter();
Trond Myklebusta1147b82020-02-05 09:01:52 -0500536 desc->dir_verifier = nfs_save_change_attribute(inode);
Trond Myklebust82e22a52020-11-02 17:34:23 -0500537 error = NFS_PROTO(inode)->readdir(&arg, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (error < 0) {
539 /* We requested READDIRPLUS, but the server doesn't grok it */
540 if (error == -ENOTSUPP && desc->plus) {
541 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200542 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Trond Myklebust82e22a52020-11-02 17:34:23 -0500543 desc->plus = arg.plus = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto again;
545 }
546 goto error;
547 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000548 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400549 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400550error:
551 return error;
552}
553
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500554static int xdr_decode(struct nfs_readdir_descriptor *desc,
Chuck Lever573c4e12010-12-14 14:58:11 +0000555 struct nfs_entry *entry, struct xdr_stream *xdr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400556{
Trond Myklebust59e356a2020-02-03 14:49:33 -0500557 struct inode *inode = file_inode(desc->file);
Chuck Lever573c4e12010-12-14 14:58:11 +0000558 int error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400559
Trond Myklebust59e356a2020-02-03 14:49:33 -0500560 error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
Chuck Lever573c4e12010-12-14 14:58:11 +0000561 if (error)
562 return error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400563 entry->fattr->time_start = desc->timestamp;
564 entry->fattr->gencount = desc->gencount;
565 return 0;
566}
567
Trond Myklebustfa923362015-02-23 18:51:32 -0500568/* Match file and dirent using either filehandle or fileid
569 * Note: caller is responsible for checking the fsid
570 */
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400571static
572int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
573{
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400574 struct inode *inode;
Trond Myklebustfa923362015-02-23 18:51:32 -0500575 struct nfs_inode *nfsi;
576
David Howells2b0143b2015-03-17 22:25:59 +0000577 if (d_really_is_negative(dentry))
578 return 0;
Trond Myklebustfa923362015-02-23 18:51:32 -0500579
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400580 inode = d_inode(dentry);
581 if (is_bad_inode(inode) || NFS_STALE(inode))
582 return 0;
583
584 nfsi = NFS_I(inode);
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400585 if (entry->fattr->fileid != nfsi->fileid)
586 return 0;
587 if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
588 return 0;
589 return 1;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400590}
591
592static
Al Viro23db8622013-05-17 16:34:50 -0400593bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400594{
595 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
596 return false;
597 if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
598 return true;
Al Viro23db8622013-05-17 16:34:50 -0400599 if (ctx->pos == 0)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400600 return true;
601 return false;
602}
603
604/*
Trond Myklebust63519fb2016-11-19 11:21:54 -0500605 * This function is called by the lookup and getattr code to request the
606 * use of readdirplus to accelerate any future lookups in the same
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400607 * directory.
608 */
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400609void nfs_advise_use_readdirplus(struct inode *dir)
610{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500611 struct nfs_inode *nfsi = NFS_I(dir);
612
613 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
614 !list_empty(&nfsi->open_files))
615 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400616}
617
Trond Myklebust311324a2014-02-07 17:02:08 -0500618/*
619 * This function is mainly for use by nfs_getattr().
620 *
621 * If this is an 'ls -l', we want to force use of readdirplus.
622 * Do this by checking if there is an active file descriptor
623 * and calling nfs_advise_use_readdirplus, then forcing a
624 * cache flush.
625 */
626void nfs_force_use_readdirplus(struct inode *dir)
627{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500628 struct nfs_inode *nfsi = NFS_I(dir);
629
630 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
631 !list_empty(&nfsi->open_files)) {
632 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Trond Myklebustff81dfb2021-09-28 14:33:44 -0400633 set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
Trond Myklebust311324a2014-02-07 17:02:08 -0500634 }
635}
636
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400637static
Trond Myklebusta1147b82020-02-05 09:01:52 -0500638void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
639 unsigned long dir_verifier)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400640{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700641 struct qstr filename = QSTR_INIT(entry->name, entry->len);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400642 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400643 struct dentry *dentry;
644 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400645 struct inode *inode;
David Quigleyaa9c2662013-05-22 12:50:44 -0400646 int status;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400647
Trond Myklebustfa923362015-02-23 18:51:32 -0500648 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
649 return;
Trond Myklebust6c441c22015-02-22 16:35:36 -0500650 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
651 return;
Trond Myklebust78d04af2016-09-20 14:34:24 -0400652 if (filename.len == 0)
653 return;
654 /* Validate that the name doesn't contain any illegal '\0' */
655 if (strnlen(filename.name, filename.len) != filename.len)
656 return;
657 /* ...or '/' */
658 if (strnchr(filename.name, filename.len, '/'))
659 return;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400660 if (filename.name[0] == '.') {
661 if (filename.len == 1)
662 return;
663 if (filename.len == 2 && filename.name[1] == '.')
664 return;
665 }
Linus Torvalds8387ff22016-06-10 07:51:30 -0700666 filename.hash = full_name_hash(parent, filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400667
Trond Myklebust4a201d62010-10-23 14:53:23 -0400668 dentry = d_lookup(parent, &filename);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400669again:
670 if (!dentry) {
671 dentry = d_alloc_parallel(parent, &filename, &wq);
672 if (IS_ERR(dentry))
673 return;
674 }
675 if (!d_in_lookup(dentry)) {
Trond Myklebust6c441c22015-02-22 16:35:36 -0500676 /* Is there a mountpoint here? If so, just exit */
677 if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
678 &entry->fattr->fsid))
679 goto out;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400680 if (nfs_same_file(dentry, entry)) {
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400681 if (!entry->fh->size)
682 goto out;
Trond Myklebusta1147b82020-02-05 09:01:52 -0500683 nfs_set_verifier(dentry, dir_verifier);
David Howells2b0143b2015-03-17 22:25:59 +0000684 status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -0400685 if (!status)
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400686 nfs_setsecurity(d_inode(dentry), entry->fattr);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400687 goto out;
688 } else {
Eric W. Biederman5542aa22014-02-13 09:46:25 -0800689 d_invalidate(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400690 dput(dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400691 dentry = NULL;
692 goto again;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400693 }
694 }
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400695 if (!entry->fh->size) {
696 d_lookup_done(dentry);
697 goto out;
698 }
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400699
Anna Schumakercf7ab002021-10-22 13:11:11 -0400700 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
Al Viro41d28bc2014-10-12 22:24:21 -0400701 alias = d_splice_alias(inode, dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400702 d_lookup_done(dentry);
703 if (alias) {
704 if (IS_ERR(alias))
705 goto out;
706 dput(dentry);
707 dentry = alias;
708 }
Trond Myklebusta1147b82020-02-05 09:01:52 -0500709 nfs_set_verifier(dentry, dir_verifier);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400710out:
711 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400712}
713
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400714/* Perform conversion from xdr to cache array */
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500715static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
716 struct nfs_entry *entry,
717 struct page **xdr_pages,
Trond Myklebust35df59d2020-11-06 20:38:47 -0500718 unsigned int buflen,
719 struct page **arrays,
720 size_t narrays)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400721{
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500722 struct address_space *mapping = desc->file->f_mapping;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400723 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400724 struct xdr_buf buf;
Trond Myklebust35df59d2020-11-06 20:38:47 -0500725 struct page *scratch, *new, *page = *arrays;
Trond Myklebust5c346852010-11-20 12:43:45 -0500726 int status;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400727
Trond Myklebust66502392011-01-08 17:45:38 -0500728 scratch = alloc_page(GFP_KERNEL);
729 if (scratch == NULL)
730 return -ENOMEM;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400731
Benny Halevyf7da7a12011-05-19 14:16:47 -0400732 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
Chuck Lever0ae4c3e2020-11-11 15:52:47 -0500733 xdr_set_scratch_page(&stream, scratch);
Bryan Schumaker99424382010-10-21 16:33:16 -0400734
735 do {
Anna Schumakerb1db9a42021-10-22 13:11:01 -0400736 if (entry->fattr->label)
737 entry->fattr->label->len = NFS4_MAXLABELLEN;
Jeffrey Mitchelld33030e2020-09-15 16:42:52 -0500738
Bryan Schumaker99424382010-10-21 16:33:16 -0400739 status = xdr_decode(desc, entry, &stream);
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500740 if (status != 0)
Bryan Schumaker99424382010-10-21 16:33:16 -0400741 break;
Trond Myklebust5c346852010-11-20 12:43:45 -0500742
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400743 if (desc->plus)
Trond Myklebusta1147b82020-02-05 09:01:52 -0500744 nfs_prime_dcache(file_dentry(desc->file), entry,
745 desc->dir_verifier);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500746
Max Kellermanndb531db2019-07-12 16:18:06 +0200747 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500748 if (status != -ENOSPC)
749 continue;
Bryan Schumaker99424382010-10-21 16:33:16 -0400750
Trond Myklebust35df59d2020-11-06 20:38:47 -0500751 if (page->mapping != mapping) {
752 if (!--narrays)
753 break;
754 new = nfs_readdir_page_array_alloc(entry->prev_cookie,
755 GFP_KERNEL);
756 if (!new)
757 break;
758 arrays++;
759 *arrays = page = new;
760 } else {
761 new = nfs_readdir_page_get_next(mapping,
762 page->index + 1,
763 entry->prev_cookie);
764 if (!new)
765 break;
766 if (page != *arrays)
767 nfs_readdir_page_unlock_and_put(page);
768 page = new;
769 }
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500770 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500771 } while (!status && !entry->eof);
Bryan Schumaker99424382010-10-21 16:33:16 -0400772
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500773 switch (status) {
774 case -EBADCOOKIE:
775 if (entry->eof) {
776 nfs_readdir_page_set_eof(page);
777 status = 0;
778 }
779 break;
780 case -ENOSPC:
781 case -EAGAIN:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200782 status = 0;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500783 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400784 }
Trond Myklebust66502392011-01-08 17:45:38 -0500785
Trond Myklebust35df59d2020-11-06 20:38:47 -0500786 if (page != *arrays)
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500787 nfs_readdir_page_unlock_and_put(page);
788
Trond Myklebust66502392011-01-08 17:45:38 -0500789 put_page(scratch);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500790 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400791}
792
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500793static void nfs_readdir_free_pages(struct page **pages, size_t npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400794{
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500795 while (npages--)
796 put_page(pages[npages]);
797 kfree(pages);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400798}
799
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400800/*
zhangliguangbf211ca2019-02-16 08:38:40 +0800801 * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
802 * to nfs_readdir_free_pages()
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400803 */
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500804static struct page **nfs_readdir_alloc_pages(size_t npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400805{
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500806 struct page **pages;
807 size_t i;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400808
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500809 pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
810 if (!pages)
811 return NULL;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400812 for (i = 0; i < npages; i++) {
813 struct page *page = alloc_page(GFP_KERNEL);
814 if (page == NULL)
815 goto out_freepages;
816 pages[i] = page;
817 }
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500818 return pages;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400819
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400820out_freepages:
Anna Schumakerc7e96682015-07-13 14:01:25 -0400821 nfs_readdir_free_pages(pages, i);
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500822 return NULL;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400823}
824
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500825static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
Trond Myklebust35df59d2020-11-06 20:38:47 -0500826 __be32 *verf_arg, __be32 *verf_res,
827 struct page **arrays, size_t narrays)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400828{
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500829 struct page **pages;
Trond Myklebust35df59d2020-11-06 20:38:47 -0500830 struct page *page = *arrays;
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500831 struct nfs_entry *entry;
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500832 size_t array_size;
Trond Myklebustb593c092020-11-02 20:06:12 -0500833 struct inode *inode = file_inode(desc->file);
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500834 size_t dtsize = NFS_SERVER(inode)->dtsize;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500835 int status = -ENOMEM;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400836
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500837 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
838 if (!entry)
839 return -ENOMEM;
840 entry->cookie = nfs_readdir_page_last_cookie(page);
841 entry->fh = nfs_alloc_fhandle();
Anna Schumakerb1db9a42021-10-22 13:11:01 -0400842 entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500843 entry->server = NFS_SERVER(inode);
844 if (entry->fh == NULL || entry->fattr == NULL)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400845 goto out;
846
Trond Myklebust1a34c8c92020-11-01 14:26:47 -0500847 array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
848 pages = nfs_readdir_alloc_pages(array_size);
849 if (!pages)
Anna Schumakerb1db9a42021-10-22 13:11:01 -0400850 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400851
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400852 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500853 unsigned int pglen;
Trond Myklebustb593c092020-11-02 20:06:12 -0500854 status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie,
855 pages, dtsize,
856 verf_res);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400857 if (status < 0)
858 break;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500859
Trond Myklebustac396122010-11-15 20:26:22 -0500860 pglen = status;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500861 if (pglen == 0) {
862 nfs_readdir_page_set_eof(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500863 break;
864 }
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500865
Nagendra S Tomaree3707a2021-03-16 10:25:14 +0000866 verf_arg = verf_res;
867
Trond Myklebust35df59d2020-11-06 20:38:47 -0500868 status = nfs_readdir_page_filler(desc, entry, pages, pglen,
869 arrays, narrays);
trondmy@kernel.orgd9c4e392022-01-18 19:25:42 -0500870 } while (!status && nfs_readdir_page_needs_filling(page) &&
871 page_mapping(page));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400872
Anna Schumakerc7e96682015-07-13 14:01:25 -0400873 nfs_readdir_free_pages(pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400874out:
Trond Myklebust6b75cf92020-11-02 08:55:03 -0500875 nfs_free_fattr(entry->fattr);
876 nfs_free_fhandle(entry->fh);
877 kfree(entry);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400878 return status;
879}
880
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500881static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300883 put_page(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500887static void
888nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500890 unlock_page(desc->page);
891 nfs_readdir_page_put(desc);
892}
893
894static struct page *
895nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
896{
897 return nfs_readdir_page_get_locked(desc->file->f_mapping,
898 desc->page_index,
899 desc->last_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400903 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Trond Myklebust114de382020-02-02 17:53:54 -0500904 * and locks the page to prevent removal from the page cache.
Olivier Galibert00a92642005-06-22 17:16:29 +0000905 */
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500906static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000907{
Dai Ngo227823d2020-01-22 20:45:39 -0500908 struct inode *inode = file_inode(desc->file);
909 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb593c092020-11-02 20:06:12 -0500910 __be32 verf[NFS_DIR_VERIFIER_SIZE];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400911 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000912
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500913 desc->page = nfs_readdir_page_get_cached(desc);
914 if (!desc->page)
915 return -ENOMEM;
916 if (nfs_readdir_page_needs_filling(desc->page)) {
Trond Myklebust35df59d2020-11-06 20:38:47 -0500917 res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
918 &desc->page, 1);
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500919 if (res < 0) {
920 nfs_readdir_page_unlock_and_put_cached(desc);
921 if (res == -EBADCOOKIE || res == -ENOTSYNC) {
922 invalidate_inode_pages2(desc->file->f_mapping);
923 desc->page_index = 0;
924 return -EAGAIN;
925 }
926 return res;
Dai Ngo227823d2020-01-22 20:45:39 -0500927 }
Trond Myklebustf892c412021-03-17 08:46:19 -0400928 /*
929 * Set the cookie verifier if the page cache was empty
930 */
931 if (desc->page_index == 0)
932 memcpy(nfsi->cookieverf, verf,
933 sizeof(nfsi->cookieverf));
Trond Myklebust114de382020-02-02 17:53:54 -0500934 }
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500935 res = nfs_readdir_search_array(desc);
Trond Myklebustff81dfb2021-09-28 14:33:44 -0400936 if (res == 0)
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500937 return 0;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500938 nfs_readdir_page_unlock_and_put_cached(desc);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400939 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000940}
941
Trond Myklebust794092c2020-11-06 20:47:05 -0500942static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc)
943{
944 struct address_space *mapping = desc->file->f_mapping;
945 struct inode *dir = file_inode(desc->file);
946 unsigned int dtsize = NFS_SERVER(dir)->dtsize;
947 loff_t size = i_size_read(dir);
948
949 /*
950 * Default to uncached readdir if the page cache is empty, and
951 * we're looking for a non-zero cookie in a large directory.
952 */
953 return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize;
954}
955
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400956/* Search for desc->dir_cookie from the beginning of the page cache */
Trond Myklebust6c981ef2020-11-03 07:42:04 -0500957static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500959 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000960
Trond Myklebust794092c2020-11-06 20:47:05 -0500961 if (nfs_readdir_dont_search_cache(desc))
962 return -EBADCOOKIE;
963
Trond Myklebust47c716c2010-12-07 12:44:56 -0500964 do {
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500965 if (desc->page_index == 0) {
966 desc->current_index = 0;
967 desc->prev_index = 0;
968 desc->last_cookie = 0;
969 }
Trond Myklebust114de382020-02-02 17:53:54 -0500970 res = find_and_lock_cache_page(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500971 } while (res == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return res;
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/*
976 * Once we've found the start of the dirent within a page: fill 'er up...
977 */
Trond Myklebust13884ff2021-03-16 07:57:40 -0400978static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
979 const __be32 *verf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct file *file = desc->file;
Trond Myklebustdbeaf8c2020-11-01 18:20:03 -0500982 struct nfs_cache_array *array;
983 unsigned int i = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400984
Fabian Frederick0795bf82017-05-03 20:52:21 +0200985 array = kmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400986 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500987 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Trond Myklebustece0b422010-11-20 13:55:33 -0500989 ent = &array->array[i];
Trond Myklebusta52a8a62020-11-01 19:17:29 -0500990 if (!dir_emit(desc->ctx, ent->name, ent->name_len,
Al Viro23db8622013-05-17 16:34:50 -0400991 nfs_compat_user_ino64(ent->ino), ent->d_type)) {
Thomas Meyer6089dd02017-10-07 16:02:21 +0200992 desc->eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500994 }
Trond Myklebust13884ff2021-03-16 07:57:40 -0400995 memcpy(desc->verf, verf, sizeof(desc->verf));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400996 if (i < (array->size-1))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500997 desc->dir_cookie = array->array[i+1].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400998 else
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500999 desc->dir_cookie = array->last_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001000 if (nfs_readdir_use_cookie(file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001001 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001002 else
1003 desc->ctx->pos++;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001004 if (desc->duped != 0)
1005 desc->duped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -05001007 if (array->page_is_eof)
Thomas Meyer6089dd02017-10-07 16:02:21 +02001008 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -04001009
Fabian Frederick0795bf82017-05-03 20:52:21 +02001010 kunmap(desc->page);
Trond Myklebustdbeaf8c2020-11-01 18:20:03 -05001011 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1012 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015/*
1016 * If we cannot find a cookie in our cache, we suspect that this is
1017 * because it points to a deleted file, so we ask the server to return
1018 * whatever it thinks is the next entry. We then feed this to filldir.
1019 * If all goes well, we should then be able to find our way round the
1020 * cache on the next call to readdir_search_pagecache();
1021 *
1022 * NOTE: we cannot add the anonymous page to the pagecache because
1023 * the data it contains might not be page aligned. Besides,
1024 * we should already have a complete representation of the
1025 * directory in the page cache by the time we get here.
1026 */
Trond Myklebust6c981ef2020-11-03 07:42:04 -05001027static int uncached_readdir(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Trond Myklebust35df59d2020-11-06 20:38:47 -05001029 struct page **arrays;
1030 size_t i, sz = 512;
Trond Myklebustb593c092020-11-02 20:06:12 -05001031 __be32 verf[NFS_DIR_VERIFIER_SIZE];
Trond Myklebust35df59d2020-11-06 20:38:47 -05001032 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Trond Myklebust35df59d2020-11-06 20:38:47 -05001034 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001035 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Trond Myklebust35df59d2020-11-06 20:38:47 -05001037 arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
1038 if (!arrays)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 goto out;
Trond Myklebust35df59d2020-11-06 20:38:47 -05001040 arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
1041 if (!arrays[0])
1042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -05001044 desc->page_index = 0;
trondmy@kernel.orgce292d82022-01-18 19:52:16 -05001045 desc->cache_entry_index = 0;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001046 desc->last_cookie = desc->dir_cookie;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001047 desc->duped = 0;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -05001048
Trond Myklebust35df59d2020-11-06 20:38:47 -05001049 status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -04001050
Trond Myklebust35df59d2020-11-06 20:38:47 -05001051 for (i = 0; !desc->eof && i < sz && arrays[i]; i++) {
1052 desc->page = arrays[i];
Trond Myklebust13884ff2021-03-16 07:57:40 -04001053 nfs_do_filldir(desc, verf);
Trond Myklebust35df59d2020-11-06 20:38:47 -05001054 }
1055 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Trond Myklebust35df59d2020-11-06 20:38:47 -05001057
1058 for (i = 0; i < sz && arrays[i]; i++)
1059 nfs_readdir_page_array_free(arrays[i]);
1060out:
1061 kfree(arrays);
1062 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Olivier Galibert00a92642005-06-22 17:16:29 +00001066/* The file offset position represents the dirent entry number. A
1067 last cookie cache takes care of the common case of reading the
1068 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Al Viro23db8622013-05-17 16:34:50 -04001070static int nfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Miklos Szeredibe62a1a2016-03-26 16:14:39 -04001072 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +00001073 struct inode *inode = d_inode(dentry);
Trond Myklebust13884ff2021-03-16 07:57:40 -04001074 struct nfs_inode *nfsi = NFS_I(inode);
Al Viro23db8622013-05-17 16:34:50 -04001075 struct nfs_open_dir_context *dir_ctx = file->private_data;
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001076 struct nfs_readdir_descriptor *desc;
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001077 pgoff_t page_index;
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001078 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Al Viro6de14722013-09-16 10:53:17 -04001080 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
1081 file, (long long)ctx->pos);
Chuck Lever91d5b472006-03-20 13:44:14 -05001082 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
Al Viro23db8622013-05-17 16:34:50 -04001085 * ctx->pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001086 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +00001087 * to either find the entry with the appropriate number or
1088 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001090 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) {
Scott Mayhew07b5ce82013-07-05 17:49:31 -04001091 res = nfs_revalidate_mapping(inode, file->f_mapping);
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001092 if (res < 0)
1093 goto out;
1094 }
1095
1096 res = -ENOMEM;
1097 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1098 if (!desc)
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001099 goto out;
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001100 desc->file = file;
1101 desc->ctx = ctx;
1102 desc->plus = nfs_use_readdirplus(inode, ctx);
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001103
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001104 spin_lock(&file->f_lock);
1105 desc->dir_cookie = dir_ctx->dir_cookie;
1106 desc->dup_cookie = dir_ctx->dup_cookie;
1107 desc->duped = dir_ctx->duped;
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001108 page_index = dir_ctx->page_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001109 desc->attr_gencount = dir_ctx->attr_gencount;
Trond Myklebustb593c092020-11-02 20:06:12 -05001110 memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001111 spin_unlock(&file->f_lock);
Trond Myklebust565277f2007-10-15 18:17:53 -04001112
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001113 if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) &&
1114 list_is_singular(&nfsi->open_files))
1115 invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1);
1116
Trond Myklebust47c716c2010-12-07 12:44:56 -05001117 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +00001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -05001121 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* This means either end of directory */
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001123 if (desc->dir_cookie && !desc->eof) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /* Or that the server has 'lost' a cookie */
Al Viro23db8622013-05-17 16:34:50 -04001125 res = uncached_readdir(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -05001126 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 continue;
Trond Myklebust9fff59e2020-11-02 20:11:32 -05001128 if (res == -EBADCOOKIE || res == -ENOTSYNC)
1129 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
1132 }
1133 if (res == -ETOOSMALL && desc->plus) {
Trond Myklebust13884ff2021-03-16 07:57:40 -04001134 clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -04001136 desc->page_index = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001137 desc->plus = false;
1138 desc->eof = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 continue;
1140 }
1141 if (res < 0)
1142 break;
1143
Trond Myklebust13884ff2021-03-16 07:57:40 -04001144 nfs_do_filldir(desc, nfsi->cookieverf);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -05001145 nfs_readdir_page_unlock_and_put_cached(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -05001146 } while (!desc->eof);
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001147
1148 spin_lock(&file->f_lock);
1149 dir_ctx->dir_cookie = desc->dir_cookie;
1150 dir_ctx->dup_cookie = desc->dup_cookie;
1151 dir_ctx->duped = desc->duped;
1152 dir_ctx->attr_gencount = desc->attr_gencount;
Trond Myklebustff81dfb2021-09-28 14:33:44 -04001153 dir_ctx->page_index = desc->page_index;
Trond Myklebustb593c092020-11-02 20:06:12 -05001154 memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001155 spin_unlock(&file->f_lock);
1156
Trond Myklebust6b75cf92020-11-02 08:55:03 -05001157 kfree(desc);
1158
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001159out:
Al Viro6de14722013-09-16 10:53:17 -04001160 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001161 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Andrew Morton965c8e52012-12-17 15:59:39 -08001164static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001165{
Bryan Schumaker480c2002011-03-23 14:48:29 -04001166 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Chuck Leverb84e06c2008-06-11 17:55:34 -04001167
Al Viro6de14722013-09-16 10:53:17 -04001168 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1169 filp, offset, whence);
Chuck Leverb84e06c2008-06-11 17:55:34 -04001170
Andrew Morton965c8e52012-12-17 15:59:39 -08001171 switch (whence) {
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001172 default:
1173 return -EINVAL;
1174 case SEEK_SET:
1175 if (offset < 0)
1176 return -EINVAL;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001177 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001178 break;
1179 case SEEK_CUR:
1180 if (offset == 0)
1181 return filp->f_pos;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001182 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001183 offset += filp->f_pos;
1184 if (offset < 0) {
Trond Myklebust83f2c452020-10-30 17:57:29 -04001185 spin_unlock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001186 return -EINVAL;
1187 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001188 }
1189 if (offset != filp->f_pos) {
1190 filp->f_pos = offset;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001191 if (nfs_readdir_use_cookie(filp))
1192 dir_ctx->dir_cookie = offset;
1193 else
1194 dir_ctx->dir_cookie = 0;
Trond Myklebustb593c092020-11-02 20:06:12 -05001195 if (offset == 0)
1196 memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -04001197 dir_ctx->duped = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001198 }
Trond Myklebust83f2c452020-10-30 17:57:29 -04001199 spin_unlock(&filp->f_lock);
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001200 return offset;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/*
1204 * All directory operations under NFS are synchronous, so fsync()
1205 * is a dummy operation.
1206 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001207static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1208 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Al Viro6de14722013-09-16 10:53:17 -04001210 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001211
Trond Myklebust11decaf2020-10-30 17:57:30 -04001212 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 0;
1214}
1215
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001216/**
1217 * nfs_force_lookup_revalidate - Mark the directory as having changed
Trond Myklebust302fad72019-02-18 13:32:38 -05001218 * @dir: pointer to directory inode
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001219 *
1220 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1221 * full lookup on all child dentries of 'dir' whenever a change occurs
1222 * on the server that might have invalidated our dcache.
1223 *
Trond Myklebustefeda802020-02-05 09:01:54 -05001224 * Note that we reserve bit '0' as a tag to let us know when a dentry
1225 * was revalidated while holding a delegation on its inode.
1226 *
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001227 * The caller should be holding dir->i_lock
1228 */
1229void nfs_force_lookup_revalidate(struct inode *dir)
1230{
Trond Myklebustefeda802020-02-05 09:01:54 -05001231 NFS_I(dir)->cache_change_attribute += 2;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001232}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001233EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001234
Trond Myklebustefeda802020-02-05 09:01:54 -05001235/**
1236 * nfs_verify_change_attribute - Detects NFS remote directory changes
1237 * @dir: pointer to parent directory inode
1238 * @verf: previously saved change attribute
1239 *
1240 * Return "false" if the verifiers doesn't match the change attribute.
1241 * This would usually indicate that the directory contents have changed on
1242 * the server, and that any dentries need revalidating.
1243 */
1244static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1245{
1246 return (verf & ~1UL) == nfs_save_change_attribute(dir);
1247}
1248
1249static void nfs_set_verifier_delegated(unsigned long *verf)
1250{
1251 *verf |= 1UL;
1252}
1253
1254#if IS_ENABLED(CONFIG_NFS_V4)
1255static void nfs_unset_verifier_delegated(unsigned long *verf)
1256{
1257 *verf &= ~1UL;
1258}
1259#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1260
1261static bool nfs_test_verifier_delegated(unsigned long verf)
1262{
1263 return verf & 1;
1264}
1265
1266static bool nfs_verifier_is_delegated(struct dentry *dentry)
1267{
1268 return nfs_test_verifier_delegated(dentry->d_time);
1269}
1270
1271static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1272{
1273 struct inode *inode = d_inode(dentry);
Trond Myklebustcec08f42021-09-29 08:12:53 -04001274 struct inode *dir = d_inode(dentry->d_parent);
Trond Myklebustefeda802020-02-05 09:01:54 -05001275
Trond Myklebustcec08f42021-09-29 08:12:53 -04001276 if (!nfs_verify_change_attribute(dir, verf))
1277 return;
Trond Myklebustefeda802020-02-05 09:01:54 -05001278 if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1279 nfs_set_verifier_delegated(&verf);
Trond Myklebustefeda802020-02-05 09:01:54 -05001280 dentry->d_time = verf;
1281}
1282
1283/**
1284 * nfs_set_verifier - save a parent directory verifier in the dentry
1285 * @dentry: pointer to dentry
1286 * @verf: verifier to save
1287 *
1288 * Saves the parent directory verifier in @dentry. If the inode has
1289 * a delegation, we also tag the dentry as having been revalidated
1290 * while holding a delegation so that we know we don't have to
1291 * look it up again after a directory change.
1292 */
1293void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1294{
1295
1296 spin_lock(&dentry->d_lock);
1297 nfs_set_verifier_locked(dentry, verf);
1298 spin_unlock(&dentry->d_lock);
1299}
1300EXPORT_SYMBOL_GPL(nfs_set_verifier);
1301
1302#if IS_ENABLED(CONFIG_NFS_V4)
1303/**
1304 * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1305 * @inode: pointer to inode
1306 *
1307 * Iterates through the dentries in the inode alias list and clears
1308 * the tag used to indicate that the dentry has been revalidated
1309 * while holding a delegation.
1310 * This function is intended for use when the delegation is being
1311 * returned or revoked.
1312 */
1313void nfs_clear_verifier_delegated(struct inode *inode)
1314{
1315 struct dentry *alias;
1316
1317 if (!inode)
1318 return;
1319 spin_lock(&inode->i_lock);
1320 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1321 spin_lock(&alias->d_lock);
1322 nfs_unset_verifier_delegated(&alias->d_time);
1323 spin_unlock(&alias->d_lock);
1324 }
1325 spin_unlock(&inode->i_lock);
1326}
1327EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1328#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1329
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001330static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
1331{
1332 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
1333 d_really_is_negative(dentry))
1334 return dentry->d_time == inode_peek_iversion_raw(dir);
1335 return nfs_verify_change_attribute(dir, dentry->d_time);
1336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/*
1339 * A check for whether or not the parent directory has changed.
1340 * In the case it has, we assume that the dentries are untrustworthy
1341 * and may need to be looked up again.
NeilBrown912a1082014-07-14 11:28:20 +10001342 * If rcu_walk prevents us from performing a full check, return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
NeilBrown912a1082014-07-14 11:28:20 +10001344static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1345 int rcu_walk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 if (IS_ROOT(dentry))
1348 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001349 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1350 return 0;
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001351 if (!nfs_dentry_verify_change(dir, dentry))
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001352 return 0;
1353 /* Revalidate nfsi->cache_change_attribute before we declare a match */
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001354 if (nfs_mapping_need_revalidate_inode(dir)) {
1355 if (rcu_walk)
1356 return 0;
1357 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1358 return 0;
1359 }
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001360 if (!nfs_dentry_verify_change(dir, dentry))
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001361 return 0;
1362 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/*
Trond Myklebusta12802c2007-10-02 19:13:04 -04001366 * Use intent information to check whether or not we're going to do
1367 * an O_EXCL create using this path component.
1368 */
Al Virofa3c56b2012-06-10 15:36:40 -04001369static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
Trond Myklebusta12802c2007-10-02 19:13:04 -04001370{
1371 if (NFS_PROTO(dir)->version == 2)
1372 return 0;
Al Virofa3c56b2012-06-10 15:36:40 -04001373 return flags & LOOKUP_EXCL;
Trond Myklebusta12802c2007-10-02 19:13:04 -04001374}
1375
1376/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001377 * Inode and filehandle revalidation for lookups.
1378 *
1379 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1380 * or if the intent information indicates that we're about to open this
1381 * particular file and the "nocto" mount flag is not set.
1382 *
1383 */
Trond Myklebust65a0c142012-12-14 17:51:40 -05001384static
Al Virofa3c56b2012-06-10 15:36:40 -04001385int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust65a0c142012-12-14 17:51:40 -05001388 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
David Howells36d43a42011-01-14 18:45:42 +00001390 if (IS_AUTOMOUNT(inode))
Trond Myklebust4e99a1f2008-03-06 12:34:59 -05001391 return 0;
Trond Myklebust47921922018-05-10 10:08:36 -04001392
1393 if (flags & LOOKUP_OPEN) {
1394 switch (inode->i_mode & S_IFMT) {
1395 case S_IFREG:
1396 /* A NFSv4 OPEN will revalidate later */
1397 if (server->caps & NFS_CAP_ATOMIC_OPEN)
1398 goto out;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001399 fallthrough;
Trond Myklebust47921922018-05-10 10:08:36 -04001400 case S_IFDIR:
1401 if (server->flags & NFS_MOUNT_NOCTO)
1402 break;
1403 /* NFS close-to-open cache consistency validation */
1404 goto out_force;
1405 }
1406 }
1407
Al Virofacc3532012-06-10 15:33:51 -04001408 /* VFS wants an on-the-wire revalidation */
Al Virofa3c56b2012-06-10 15:36:40 -04001409 if (flags & LOOKUP_REVAL)
Al Virofacc3532012-06-10 15:33:51 -04001410 goto out_force;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001411out:
Lance Sheltona61246c2018-07-16 13:05:36 -04001412 return (inode->i_nlink == 0) ? -ESTALE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413out_force:
NeilBrown1fa1e382014-07-14 11:28:20 +10001414 if (flags & LOOKUP_RCU)
1415 return -ECHILD;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001416 ret = __nfs_revalidate_inode(server, inode);
1417 if (ret != 0)
1418 return ret;
1419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001422static void nfs_mark_dir_for_revalidate(struct inode *inode)
1423{
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001424 spin_lock(&inode->i_lock);
Trond Myklebusta6a361c2021-09-28 11:24:57 -04001425 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001426 spin_unlock(&inode->i_lock);
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/*
1430 * We judge how long we want to trust negative
1431 * dentries by looking at the parent inode mtime.
1432 *
1433 * If parent mtime has changed, we revalidate, else we wait for a
1434 * period corresponding to the parent's attribute cache timeout value.
NeilBrown912a1082014-07-14 11:28:20 +10001435 *
1436 * If LOOKUP_RCU prevents us from performing a full check, return 1
1437 * suggesting a reval is needed.
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001438 *
1439 * Note that when creating a new file, or looking up a rename target,
1440 * then it shouldn't be necessary to revalidate a negative dentry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
1442static inline
1443int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
Al Virofa3c56b2012-06-10 15:36:40 -04001444 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001446 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001448 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1449 return 1;
Trond Myklebust98ca3ee2021-12-17 15:36:55 -05001450 /* Case insensitive server? Revalidate negative dentries */
1451 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1452 return 1;
NeilBrown912a1082014-07-14 11:28:20 +10001453 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001456static int
1457nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1458 struct inode *inode, int error)
1459{
1460 switch (error) {
1461 case 1:
1462 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
1463 __func__, dentry);
1464 return 1;
1465 case 0:
Trond Myklebust47397912021-03-08 14:42:52 -05001466 /*
1467 * We can't d_drop the root of a disconnected tree:
1468 * its d_hash is on the s_anon list and d_drop() would hide
1469 * it from shrink_dcache_for_unmount(), leading to busy
1470 * inodes on unmount and further oopses.
1471 */
1472 if (inode && IS_ROOT(dentry))
1473 return 1;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001474 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
1475 __func__, dentry);
1476 return 0;
1477 }
1478 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
1479 __func__, dentry, error);
1480 return error;
1481}
1482
1483static int
1484nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1485 unsigned int flags)
1486{
1487 int ret = 1;
1488 if (nfs_neg_need_reval(dir, dentry, flags)) {
1489 if (flags & LOOKUP_RCU)
1490 return -ECHILD;
1491 ret = 0;
1492 }
1493 return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1494}
1495
1496static int
1497nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1498 struct inode *inode)
1499{
1500 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1501 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1502}
1503
1504static int
1505nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
1506 struct inode *inode)
1507{
1508 struct nfs_fh *fhandle;
1509 struct nfs_fattr *fattr;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001510 unsigned long dir_verifier;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001511 int ret;
1512
1513 ret = -ENOMEM;
1514 fhandle = nfs_alloc_fhandle();
Anna Schumaker9558a002021-10-22 13:11:04 -04001515 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1516 if (fhandle == NULL || fattr == NULL)
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001517 goto out;
1518
Trond Myklebusta1147b82020-02-05 09:01:52 -05001519 dir_verifier = nfs_save_change_attribute(dir);
Anna Schumaker9558a002021-10-22 13:11:04 -04001520 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001521 if (ret < 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001522 switch (ret) {
1523 case -ESTALE:
1524 case -ENOENT:
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001525 ret = 0;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001526 break;
1527 case -ETIMEDOUT:
1528 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1529 ret = 1;
1530 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001531 goto out;
1532 }
1533 ret = 0;
1534 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1535 goto out;
1536 if (nfs_refresh_inode(inode, fattr) < 0)
1537 goto out;
1538
Anna Schumakerdd225cb2021-10-22 13:11:12 -04001539 nfs_setsecurity(inode, fattr);
Trond Myklebusta1147b82020-02-05 09:01:52 -05001540 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001541
1542 /* set a readdirplus hint that we had a cache miss */
1543 nfs_force_use_readdirplus(dir);
1544 ret = 1;
1545out:
1546 nfs_free_fattr(fattr);
1547 nfs_free_fhandle(fhandle);
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001548
1549 /*
1550 * If the lookup failed despite the dentry change attribute being
1551 * a match, then we should revalidate the directory cache.
1552 */
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001553 if (!ret && nfs_dentry_verify_change(dir, dentry))
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001554 nfs_mark_dir_for_revalidate(dir);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001555 return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558/*
1559 * This is called every time the dcache has a lookup hit,
1560 * and we should check whether we can really trust that
1561 * lookup.
1562 *
1563 * NOTE! The hit can be a negative hit too, don't assume
1564 * we have an inode!
1565 *
1566 * If the parent directory is seen to have changed, we throw out the
1567 * cached dentry and do a new lookup.
1568 */
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001569static int
1570nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1571 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Chuck Lever91d5b472006-03-20 13:44:14 -05001576 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
David Howells2b0143b2015-03-17 22:25:59 +00001577 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001579 if (!inode)
1580 return nfs_lookup_revalidate_negative(dir, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001583 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1584 __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto out_bad;
1586 }
1587
Trond Myklebustefeda802020-02-05 09:01:54 -05001588 if (nfs_verifier_is_delegated(dentry))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001589 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /* Force a full look up iff the parent directory has changed */
Trond Myklebust73dd6842018-05-10 10:13:09 -04001592 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
NeilBrown912a1082014-07-14 11:28:20 +10001593 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
NeilBrowncc896842017-07-05 12:22:20 +10001594 error = nfs_lookup_verify_inode(inode, flags);
1595 if (error) {
NeilBrowncc896842017-07-05 12:22:20 +10001596 if (error == -ESTALE)
Trond Myklebust82e7ca12021-03-08 14:42:51 -05001597 nfs_mark_dir_for_revalidate(dir);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001598 goto out_bad;
NeilBrown1fa1e382014-07-14 11:28:20 +10001599 }
Trond Myklebust63519fb2016-11-19 11:21:54 -05001600 nfs_advise_use_readdirplus(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto out_valid;
1602 }
1603
NeilBrown912a1082014-07-14 11:28:20 +10001604 if (flags & LOOKUP_RCU)
1605 return -ECHILD;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (NFS_STALE(inode))
1608 goto out_bad;
1609
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001610 trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001611 error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001612 trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001613 return error;
1614out_valid:
1615 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1616out_bad:
1617 if (flags & LOOKUP_RCU)
1618 return -ECHILD;
1619 return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001622static int
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001623__nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1624 int (*reval)(struct inode *, struct dentry *, unsigned int))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001625{
1626 struct dentry *parent;
1627 struct inode *dir;
1628 int ret;
David Quigleyaa9c2662013-05-22 12:50:44 -04001629
NeilBrownd51ac1a2014-07-14 11:28:20 +10001630 if (flags & LOOKUP_RCU) {
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001631 parent = READ_ONCE(dentry->d_parent);
1632 dir = d_inode_rcu(parent);
1633 if (!dir)
1634 return -ECHILD;
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001635 ret = reval(dir, dentry, flags);
Mark Rutland6aa7de02017-10-23 14:07:29 -07001636 if (parent != READ_ONCE(dentry->d_parent))
NeilBrownd51ac1a2014-07-14 11:28:20 +10001637 return -ECHILD;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001638 } else {
1639 parent = dget_parent(dentry);
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001640 ret = reval(d_inode(parent), dentry, flags);
NeilBrownd51ac1a2014-07-14 11:28:20 +10001641 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001646static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1647{
1648 return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651/*
David Howells2b0143b2015-03-17 22:25:59 +00001652 * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001653 * when we don't really care about the dentry name. This is called when a
1654 * pathwalk ends on a dentry that was not found via a normal lookup in the
1655 * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1656 *
1657 * In this situation, we just want to verify that the inode itself is OK
1658 * since the dentry might have changed on the server.
1659 */
1660static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1661{
David Howells2b0143b2015-03-17 22:25:59 +00001662 struct inode *inode = d_inode(dentry);
Trond Myklebust9cdd1d32016-12-16 18:04:47 -05001663 int error = 0;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001664
1665 /*
1666 * I believe we can only get a negative dentry here in the case of a
1667 * procfs-style symlink. Just assume it's correct for now, but we may
1668 * eventually need to do something more here.
1669 */
1670 if (!inode) {
Al Viro6de14722013-09-16 10:53:17 -04001671 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1672 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001673 return 1;
1674 }
1675
1676 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001677 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1678 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001679 return 0;
1680 }
1681
NeilBrownb6887412017-08-25 17:34:41 +10001682 error = nfs_lookup_verify_inode(inode, flags);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001683 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1684 __func__, inode->i_ino, error ? "invalid" : "valid");
1685 return !error;
1686}
1687
1688/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * This is called from dput() when d_count is going to 0.
1690 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001691static int nfs_dentry_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Al Viro6de14722013-09-16 10:53:17 -04001693 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1694 dentry, dentry->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Trond Myklebust77f11192008-01-28 19:43:19 -05001696 /* Unhash any dentry with a stale inode */
David Howells2b0143b2015-03-17 22:25:59 +00001697 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
Trond Myklebust77f11192008-01-28 19:43:19 -05001698 return 1;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1701 /* Unhash it, so that ->d_iput() would be called */
1702 return 1;
1703 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001704 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /* Unhash it, so that ancestors of killed async unlink
1706 * files will be cleaned up during umount */
1707 return 1;
1708 }
1709 return 0;
1710
1711}
1712
Trond Myklebust1f018452012-12-14 16:38:46 -05001713/* Ensure that we revalidate inode->i_nlink */
Trond Myklebust1b83d702008-06-11 15:44:04 -04001714static void nfs_drop_nlink(struct inode *inode)
1715{
1716 spin_lock(&inode->i_lock);
Trond Myklebust1f018452012-12-14 16:38:46 -05001717 /* drop the inode if we're reasonably sure this is the last link */
Trond Myklebust59a707b2018-04-08 18:11:18 -04001718 if (inode->i_nlink > 0)
1719 drop_nlink(inode);
1720 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustac46b3d2021-03-08 14:42:54 -05001721 nfs_set_cache_invalid(
1722 inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
Trond Myklebust1301e422021-04-01 14:57:56 -04001723 NFS_INO_INVALID_NLINK);
Trond Myklebust1b83d702008-06-11 15:44:04 -04001724 spin_unlock(&inode->i_lock);
1725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727/*
1728 * Called when the dentry loses inode.
1729 * We use it to clean up silly-renamed files.
1730 */
1731static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1732{
1733 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001734 nfs_complete_unlink(dentry, inode);
Trond Myklebust1f018452012-12-14 16:38:46 -05001735 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 iput(inode);
1738}
1739
Al Virob1942c52011-03-16 05:44:14 -04001740static void nfs_d_release(struct dentry *dentry)
1741{
1742 /* free cached devname value, if it survived that far */
1743 if (unlikely(dentry->d_fsdata)) {
1744 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1745 WARN_ON(1);
1746 else
1747 kfree(dentry->d_fsdata);
1748 }
1749}
1750
Al Virof786aa92009-02-20 05:51:22 +00001751const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .d_revalidate = nfs_lookup_revalidate,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001753 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 .d_delete = nfs_dentry_delete,
1755 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001756 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001757 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758};
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001759EXPORT_SYMBOL_GPL(nfs_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Bryan Schumaker597d9282012-07-16 16:39:10 -04001761struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 struct dentry *res;
1764 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001765 struct nfs_fh *fhandle = NULL;
1766 struct nfs_fattr *fattr = NULL;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001767 unsigned long dir_verifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Al Viro6de14722013-09-16 10:53:17 -04001770 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
Chuck Lever91d5b472006-03-20 13:44:14 -05001771 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Al Viro130f9ab2016-03-07 22:40:43 -05001773 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1774 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Trond Myklebustfd684072006-09-05 12:27:44 -04001776 /*
1777 * If we're doing an exclusive create, optimize away the lookup
1778 * but don't hash the dentry.
1779 */
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001780 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
Al Viro130f9ab2016-03-07 22:40:43 -05001781 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001783 res = ERR_PTR(-ENOMEM);
1784 fhandle = nfs_alloc_fhandle();
Anna Schumaker9558a002021-10-22 13:11:04 -04001785 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001786 if (fhandle == NULL || fattr == NULL)
1787 goto out;
1788
Trond Myklebusta1147b82020-02-05 09:01:52 -05001789 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001790 trace_nfs_lookup_enter(dir, dentry, flags);
Anna Schumaker9558a002021-10-22 13:11:04 -04001791 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001792 if (error == -ENOENT) {
1793 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1794 dir_verifier = inode_peek_iversion_raw(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto no_entry;
Trond Myklebust8ce37ab2021-12-17 15:36:56 -05001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (error < 0) {
1798 res = ERR_PTR(error);
Anna Schumaker9558a002021-10-22 13:11:04 -04001799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Anna Schumakercf7ab002021-10-22 13:11:11 -04001801 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Namhyung Kimbf0c84f2010-12-28 17:02:46 +00001802 res = ERR_CAST(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001803 if (IS_ERR(res))
Anna Schumaker9558a002021-10-22 13:11:04 -04001804 goto out;
David Howells54ceac42006-08-22 20:06:13 -04001805
Trond Myklebust63519fb2016-11-19 11:21:54 -05001806 /* Notify readdir to use READDIRPLUS */
1807 nfs_force_use_readdirplus(dir);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809no_entry:
Al Viro41d28bc2014-10-12 22:24:21 -04001810 res = d_splice_alias(inode, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001811 if (res != NULL) {
1812 if (IS_ERR(res))
Anna Schumaker9558a002021-10-22 13:11:04 -04001813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001815 }
Trond Myklebusta1147b82020-02-05 09:01:52 -05001816 nfs_set_verifier(dentry, dir_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817out:
Anna Schumaker9558a002021-10-22 13:11:04 -04001818 trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001819 nfs_free_fattr(fattr);
1820 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return res;
1822}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001823EXPORT_SYMBOL_GPL(nfs_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Trond Myklebust00bdadc2021-12-17 15:36:57 -05001825void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
1826{
1827 /* Case insensitive server? Revalidate dentries */
1828 if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
1829 d_prune_aliases(inode);
1830}
1831EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
1832
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001833#if IS_ENABLED(CONFIG_NFS_V4)
Al Viro0b728e12012-06-10 16:03:43 -04001834static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Al Virof786aa92009-02-20 05:51:22 +00001836const struct dentry_operations nfs4_dentry_operations = {
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001837 .d_revalidate = nfs4_lookup_revalidate,
NeilBrownb6887412017-08-25 17:34:41 +10001838 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 .d_delete = nfs_dentry_delete,
1840 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001841 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001842 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843};
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001844EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Al Viro8a5e9292011-06-25 19:15:54 -04001846static fmode_t flags_to_mode(int flags)
1847{
1848 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1849 if ((flags & O_ACCMODE) != O_WRONLY)
1850 res |= FMODE_READ;
1851 if ((flags & O_ACCMODE) != O_RDONLY)
1852 res |= FMODE_WRITE;
1853 return res;
1854}
1855
NeilBrown532d4de2016-10-13 15:26:47 +11001856static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001857{
NeilBrown532d4de2016-10-13 15:26:47 +11001858 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001859}
1860
1861static int do_open(struct inode *inode, struct file *filp)
1862{
David Howellsf1fe29b2013-09-27 11:20:03 +01001863 nfs_fscache_open_file(inode, filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001864 return 0;
1865}
1866
Al Virod9585272012-06-22 12:39:14 +04001867static int nfs_finish_open(struct nfs_open_context *ctx,
1868 struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -04001869 struct file *file, unsigned open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001870{
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001871 int err;
1872
Al Virobe12af32018-06-08 11:44:56 -04001873 err = finish_open(file, dentry, do_open);
Al Viro30d90492012-06-22 12:40:19 +04001874 if (err)
Al Virod9585272012-06-22 12:39:14 +04001875 goto out;
NeilBrowneaa2b822017-07-03 15:27:26 +10001876 if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1877 nfs_file_set_open_context(file, ctx);
1878 else
Trond Myklebust98214212019-08-09 12:15:07 -04001879 err = -EOPENSTALE;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001880out:
Al Virod9585272012-06-22 12:39:14 +04001881 return err;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001882}
1883
Bryan Schumaker73a79702012-07-16 16:39:12 -04001884int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1885 struct file *file, unsigned open_flags,
Al Viro44907d72018-06-08 13:32:02 -04001886 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Al Viroc94c0952016-07-05 09:49:21 -04001888 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001889 struct nfs_open_context *ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001890 struct dentry *res;
1891 struct iattr attr = { .ia_valid = ATTR_OPEN };
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001892 struct inode *inode;
Trond Myklebust1472b832013-08-20 11:59:41 -04001893 unsigned int lookup_flags = 0;
Trond Myklebust68eaba42021-12-17 15:36:58 -05001894 unsigned long dir_verifier;
Al Viroc94c0952016-07-05 09:49:21 -04001895 bool switched = false;
Al Viro73a09dd2018-06-08 13:22:02 -04001896 int created = 0;
Trond Myklebust898f6352010-10-23 11:24:25 -04001897 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001899 /* Expect a negative dentry */
David Howells2b0143b2015-03-17 22:25:59 +00001900 BUG_ON(d_inode(dentry));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001901
Niels de Vos1e8968c2013-12-17 18:20:16 +01001902 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001903 dir->i_sb->s_id, dir->i_ino, dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001904
Jeff Layton9597c132013-08-02 11:39:32 -04001905 err = nfs_check_flags(open_flags);
1906 if (err)
1907 return err;
1908
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001909 /* NFS only supports OPEN on regular files */
1910 if ((open_flags & O_DIRECTORY)) {
Al Viro00699ad2016-07-05 09:44:53 -04001911 if (!d_in_lookup(dentry)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001912 /*
1913 * Hashed negative dentry with O_DIRECTORY: dentry was
1914 * revalidated and is fine, no need to perform lookup
1915 * again
1916 */
Al Virod9585272012-06-22 12:39:14 +04001917 return -ENOENT;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001918 }
Trond Myklebust1472b832013-08-20 11:59:41 -04001919 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001923 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
Al Virod9585272012-06-22 12:39:14 +04001924 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001926 if (open_flags & O_CREAT) {
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001927 struct nfs_server *server = NFS_SERVER(dir);
1928
1929 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1930 mode &= ~current_umask();
1931
Trond Myklebust536e43d2012-01-17 22:04:26 -05001932 attr.ia_valid |= ATTR_MODE;
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001933 attr.ia_mode = mode;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001934 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001935 if (open_flags & O_TRUNC) {
1936 attr.ia_valid |= ATTR_SIZE;
1937 attr.ia_size = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001938 }
1939
Al Viroc94c0952016-07-05 09:49:21 -04001940 if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1941 d_drop(dentry);
1942 switched = true;
1943 dentry = d_alloc_parallel(dentry->d_parent,
1944 &dentry->d_name, &wq);
1945 if (IS_ERR(dentry))
1946 return PTR_ERR(dentry);
1947 if (unlikely(!d_in_lookup(dentry)))
1948 return finish_no_open(file, dentry);
1949 }
1950
NeilBrown532d4de2016-10-13 15:26:47 +11001951 ctx = create_nfs_open_context(dentry, open_flags, file);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001952 err = PTR_ERR(ctx);
1953 if (IS_ERR(ctx))
Al Virod9585272012-06-22 12:39:14 +04001954 goto out;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001955
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001956 trace_nfs_atomic_open_enter(dir, ctx, open_flags);
Al Viro73a09dd2018-06-08 13:22:02 -04001957 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
1958 if (created)
1959 file->f_mode |= FMODE_CREATED;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001960 if (IS_ERR(inode)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001961 err = PTR_ERR(inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001962 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001963 put_nfs_open_context(ctx);
Al Virod20cb712016-06-20 13:14:36 -04001964 d_drop(dentry);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001965 switch (err) {
1966 case -ENOENT:
Peng Tao774d9512017-06-29 06:34:50 -07001967 d_splice_alias(NULL, dentry);
Trond Myklebust68eaba42021-12-17 15:36:58 -05001968 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1969 dir_verifier = inode_peek_iversion_raw(dir);
1970 else
1971 dir_verifier = nfs_save_change_attribute(dir);
1972 nfs_set_verifier(dentry, dir_verifier);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001973 break;
1974 case -EISDIR:
1975 case -ENOTDIR:
1976 goto no_open;
1977 case -ELOOP:
1978 if (!(open_flags & O_NOFOLLOW))
Trond Myklebust6f926b52005-10-18 14:20:18 -07001979 goto no_open;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* case -EINVAL: */
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001982 default:
1983 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
Al Virod9585272012-06-22 12:39:14 +04001985 goto out;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001986 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001987
Al Virob452a452018-06-08 13:06:28 -04001988 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001989 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001990 put_nfs_open_context(ctx);
Al Virod9585272012-06-22 12:39:14 +04001991out:
Al Viroc94c0952016-07-05 09:49:21 -04001992 if (unlikely(switched)) {
1993 d_lookup_done(dentry);
1994 dput(dentry);
1995 }
Al Virod9585272012-06-22 12:39:14 +04001996 return err;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998no_open:
Trond Myklebust1472b832013-08-20 11:59:41 -04001999 res = nfs_lookup(dir, dentry, lookup_flags);
Trond Myklebustac795162022-01-06 18:24:02 -05002000 if (!res) {
2001 inode = d_inode(dentry);
2002 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2003 !S_ISDIR(inode->i_mode))
2004 res = ERR_PTR(-ENOTDIR);
Trond Myklebust1751fc12022-01-06 18:24:03 -05002005 else if (inode && S_ISREG(inode->i_mode))
2006 res = ERR_PTR(-EOPENSTALE);
Trond Myklebustac795162022-01-06 18:24:02 -05002007 } else if (!IS_ERR(res)) {
2008 inode = d_inode(res);
2009 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2010 !S_ISDIR(inode->i_mode)) {
2011 dput(res);
2012 res = ERR_PTR(-ENOTDIR);
Trond Myklebust1751fc12022-01-06 18:24:03 -05002013 } else if (inode && S_ISREG(inode->i_mode)) {
2014 dput(res);
2015 res = ERR_PTR(-EOPENSTALE);
Trond Myklebustac795162022-01-06 18:24:02 -05002016 }
2017 }
Al Viroc94c0952016-07-05 09:49:21 -04002018 if (switched) {
2019 d_lookup_done(dentry);
2020 if (!res)
2021 res = dentry;
2022 else
2023 dput(dentry);
2024 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02002025 if (IS_ERR(res))
Al Viroc94c0952016-07-05 09:49:21 -04002026 return PTR_ERR(res);
Al Viroe45198a2012-06-10 06:48:09 -04002027 return finish_no_open(file, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002029EXPORT_SYMBOL_GPL(nfs_atomic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002031static int
2032nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2033 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Nick Piggin657e94b2011-01-14 02:48:39 +00002035 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Al Virofa3c56b2012-06-10 15:36:40 -04002037 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002038 goto full_reval;
Miklos Szeredieda72af2012-06-05 15:10:21 +02002039 if (d_mountpoint(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002040 goto full_reval;
Trond Myklebust2b484292010-09-17 10:56:51 -04002041
David Howells2b0143b2015-03-17 22:25:59 +00002042 inode = d_inode(dentry);
Trond Myklebust2b484292010-09-17 10:56:51 -04002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* We can't create new files in nfs_open_revalidate(), so we
2045 * optimize away revalidation of negative dentries.
2046 */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002047 if (inode == NULL)
2048 goto full_reval;
NeilBrown49317a72014-07-14 11:28:20 +10002049
Trond Myklebustefeda802020-02-05 09:01:54 -05002050 if (nfs_verifier_is_delegated(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002051 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust216d5d062007-10-01 20:10:12 -04002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /* NFS only supports OPEN on regular files */
2054 if (!S_ISREG(inode->i_mode))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002055 goto full_reval;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /* We cannot do exclusive creation on a positive dentry */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002058 if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2059 goto reval_dentry;
2060
2061 /* Check if the directory changed */
2062 if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2063 goto reval_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02002065 /* Let f_op->open() actually open (and revalidate) the file */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002066 return 1;
2067reval_dentry:
2068 if (flags & LOOKUP_RCU)
2069 return -ECHILD;
zhangliguang42f72cf2019-02-12 09:38:33 +08002070 return nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust536e43d2012-01-17 22:04:26 -05002071
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002072full_reval:
2073 return nfs_do_lookup_revalidate(dir, dentry, flags);
2074}
Trond Myklebust535918f2010-09-17 10:56:51 -04002075
Trond Myklebustc7944eb2018-09-28 12:42:51 -04002076static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2077{
2078 return __nfs_lookup_revalidate(dentry, flags,
2079 nfs4_do_lookup_revalidate);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#endif /* CONFIG_NFSV4 */
2083
Benjamin Coddington406cd912019-09-13 08:29:02 -04002084struct dentry *
2085nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumakercc6f3292021-10-22 13:11:10 -04002086 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Trond Myklebustfab728e2007-09-29 17:41:33 -04002088 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002089 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct inode *inode;
Al Virob0c6108e2018-05-16 10:55:01 -04002091 struct dentry *d;
Benjamin Coddington406cd912019-09-13 08:29:02 -04002092 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Trond Myklebustfab728e2007-09-29 17:41:33 -04002094 d_drop(dentry);
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (fhandle->size == 0) {
Anna Schumaker9558a002021-10-22 13:11:04 -04002097 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04002099 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04002101 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (!(fattr->valid & NFS_ATTR_FATTR)) {
2103 struct nfs_server *server = NFS_SB(dentry->d_sb);
Trond Myklebusta841b542018-04-07 13:50:59 -04002104 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -04002105 fattr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04002107 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Anna Schumakercf7ab002021-10-22 13:11:11 -04002109 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Al Virob0c6108e2018-05-16 10:55:01 -04002110 d = d_splice_alias(inode, dentry);
Trond Myklebustfab728e2007-09-29 17:41:33 -04002111out:
2112 dput(parent);
Benjamin Coddington406cd912019-09-13 08:29:02 -04002113 return d;
Trond Myklebustfab728e2007-09-29 17:41:33 -04002114out_error:
Benjamin Coddington406cd912019-09-13 08:29:02 -04002115 d = ERR_PTR(error);
2116 goto out;
2117}
2118EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2119
2120/*
2121 * Code common to create, mkdir, and mknod.
2122 */
2123int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumakerd91bfc42021-10-22 13:11:09 -04002124 struct nfs_fattr *fattr)
Benjamin Coddington406cd912019-09-13 08:29:02 -04002125{
2126 struct dentry *d;
2127
Anna Schumakercc6f3292021-10-22 13:11:10 -04002128 d = nfs_add_or_obtain(dentry, fhandle, fattr);
Benjamin Coddington406cd912019-09-13 08:29:02 -04002129 if (IS_ERR(d))
2130 return PTR_ERR(d);
2131
2132 /* Callers don't care */
2133 dput(d);
2134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002136EXPORT_SYMBOL_GPL(nfs_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138/*
2139 * Following a failed create operation, we drop the dentry rather
2140 * than retain a negative dentry. This avoids a problem in the event
2141 * that the operation succeeded on the server, but an error in the
2142 * reply path made it appear to have failed.
2143 */
Christian Brauner549c7292021-01-21 14:19:43 +01002144int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2145 struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct iattr attr;
Al Viroebfc3b42012-06-10 18:05:36 -04002148 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Niels de Vos1e8968c2013-12-17 18:20:16 +01002151 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002152 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 attr.ia_mode = mode;
2155 attr.ia_valid = ATTR_MODE;
2156
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002157 trace_nfs_create_enter(dir, dentry, open_flags);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002158 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002159 trace_nfs_create_exit(dir, dentry, open_flags, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (error != 0)
2161 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return 0;
2163out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 d_drop(dentry);
2165 return error;
2166}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002167EXPORT_SYMBOL_GPL(nfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169/*
2170 * See comments for nfs_proc_create regarding failed operations.
2171 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002172int
Christian Brauner549c7292021-01-21 14:19:43 +01002173nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2174 struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 struct iattr attr;
2177 int status;
2178
Niels de Vos1e8968c2013-12-17 18:20:16 +01002179 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002180 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 attr.ia_mode = mode;
2183 attr.ia_valid = ATTR_MODE;
2184
Trond Myklebust1ca42382013-08-21 12:36:04 -04002185 trace_nfs_mknod_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002187 trace_nfs_mknod_exit(dir, dentry, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (status != 0)
2189 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return 0;
2191out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 d_drop(dentry);
2193 return status;
2194}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002195EXPORT_SYMBOL_GPL(nfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197/*
2198 * See comments for nfs_proc_create regarding failed operations.
2199 */
Christian Brauner549c7292021-01-21 14:19:43 +01002200int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2201 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct iattr attr;
2204 int error;
2205
Niels de Vos1e8968c2013-12-17 18:20:16 +01002206 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002207 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 attr.ia_valid = ATTR_MODE;
2210 attr.ia_mode = mode | S_IFDIR;
2211
Trond Myklebust1ca42382013-08-21 12:36:04 -04002212 trace_nfs_mkdir_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002214 trace_nfs_mkdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (error != 0)
2216 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218out_err:
2219 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return error;
2221}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002222EXPORT_SYMBOL_GPL(nfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002224static void nfs_dentry_handle_enoent(struct dentry *dentry)
2225{
Al Virodc3f4192015-05-18 10:10:34 -04002226 if (simple_positive(dentry))
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002227 d_delete(dentry);
2228}
2229
Trond Myklebust9019fb32021-07-07 21:43:09 -04002230static void nfs_dentry_remove_handle_error(struct inode *dir,
2231 struct dentry *dentry, int error)
2232{
2233 switch (error) {
2234 case -ENOENT:
2235 d_delete(dentry);
Trond Myklebust00bdadc2021-12-17 15:36:57 -05002236 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2237 break;
Trond Myklebust9019fb32021-07-07 21:43:09 -04002238 case 0:
Trond Myklebust00bdadc2021-12-17 15:36:57 -05002239 nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
Trond Myklebust9019fb32021-07-07 21:43:09 -04002240 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2241 }
2242}
2243
Bryan Schumaker597d9282012-07-16 16:39:10 -04002244int nfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 int error;
2247
Niels de Vos1e8968c2013-12-17 18:20:16 +01002248 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002249 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Trond Myklebust1ca42382013-08-21 12:36:04 -04002251 trace_nfs_rmdir_enter(dir, dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002252 if (d_really_is_positive(dentry)) {
Al Viro884be172016-04-28 23:56:31 -04002253 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002254 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2255 /* Ensure the VFS deletes this inode */
2256 switch (error) {
2257 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00002258 clear_nlink(d_inode(dentry));
Trond Myklebustba6c0592013-08-30 12:24:25 -04002259 break;
2260 case -ENOENT:
2261 nfs_dentry_handle_enoent(dentry);
2262 }
Al Viro884be172016-04-28 23:56:31 -04002263 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002264 } else
2265 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
Trond Myklebust9019fb32021-07-07 21:43:09 -04002266 nfs_dentry_remove_handle_error(dir, dentry, error);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002267 trace_nfs_rmdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return error;
2270}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002271EXPORT_SYMBOL_GPL(nfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273/*
2274 * Remove a file after making sure there are no pending writes,
2275 * and after checking that the file has only one user.
2276 *
2277 * We invalidate the attribute cache and free the inode prior to the operation
2278 * to avoid possible races if the server reuses the inode.
2279 */
2280static int nfs_safe_remove(struct dentry *dentry)
2281{
David Howells2b0143b2015-03-17 22:25:59 +00002282 struct inode *dir = d_inode(dentry->d_parent);
2283 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 int error = -EBUSY;
2285
Al Viro6de14722013-09-16 10:53:17 -04002286 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 /* If the dentry was sillyrenamed, we simply call d_delete() */
2289 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2290 error = 0;
2291 goto out;
2292 }
2293
Trond Myklebust1ca42382013-08-21 12:36:04 -04002294 trace_nfs_remove_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (inode != NULL) {
Trond Myklebust912678d2018-03-20 16:43:15 -04002296 error = NFS_PROTO(dir)->remove(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04002298 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 } else
Trond Myklebust912678d2018-03-20 16:43:15 -04002300 error = NFS_PROTO(dir)->remove(dir, dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002301 if (error == -ENOENT)
2302 nfs_dentry_handle_enoent(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002303 trace_nfs_remove_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304out:
2305 return error;
2306}
2307
2308/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
2309 * belongs to an active ".nfs..." file and we return -EBUSY.
2310 *
2311 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
2312 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002313int nfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 int error;
2316 int need_rehash = 0;
2317
Niels de Vos1e8968c2013-12-17 18:20:16 +01002318 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002319 dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Trond Myklebust1ca42382013-08-21 12:36:04 -04002321 trace_nfs_unlink_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 spin_lock(&dentry->d_lock);
Al Viro84d08fa2013-07-05 18:59:33 +04002323 if (d_count(dentry) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 spin_unlock(&dentry->d_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05002325 /* Start asynchronous writeout of the inode */
David Howells2b0143b2015-03-17 22:25:59 +00002326 write_inode_now(d_inode(dentry), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 error = nfs_sillyrename(dir, dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 if (!d_unhashed(dentry)) {
2331 __d_drop(dentry);
2332 need_rehash = 1;
2333 }
2334 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 error = nfs_safe_remove(dentry);
Trond Myklebust9019fb32021-07-07 21:43:09 -04002336 nfs_dentry_remove_handle_error(dir, dentry, error);
2337 if (need_rehash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 d_rehash(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002339out:
2340 trace_nfs_unlink_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return error;
2342}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002343EXPORT_SYMBOL_GPL(nfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Chuck Lever873101b2006-08-22 20:06:23 -04002345/*
2346 * To create a symbolic link, most file systems instantiate a new inode,
2347 * add a page to it containing the path, then write it out to the disk
2348 * using prepare_write/commit_write.
2349 *
2350 * Unfortunately the NFS client can't create the in-core inode first
2351 * because it needs a file handle to create an in-core inode (see
2352 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
2353 * symlink request has completed on the server.
2354 *
2355 * So instead we allocate a raw page, copy the symname into it, then do
2356 * the SYMLINK request with the page as the buffer. If it succeeds, we
2357 * now have a new file handle and can instantiate an in-core NFS inode
2358 * and move the raw page into its mapping.
2359 */
Christian Brauner549c7292021-01-21 14:19:43 +01002360int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2361 struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Chuck Lever873101b2006-08-22 20:06:23 -04002363 struct page *page;
2364 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04002366 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int error;
2368
Niels de Vos1e8968c2013-12-17 18:20:16 +01002369 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002370 dir->i_ino, dentry, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Chuck Lever873101b2006-08-22 20:06:23 -04002372 if (pathlen > PAGE_SIZE)
2373 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Chuck Lever873101b2006-08-22 20:06:23 -04002375 attr.ia_mode = S_IFLNK | S_IRWXUGO;
2376 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Al Viroe8ecde22016-01-14 17:52:59 -05002378 page = alloc_page(GFP_USER);
Trond Myklebust76566992008-06-11 15:44:22 -04002379 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04002380 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04002381
Al Viroe8ecde22016-01-14 17:52:59 -05002382 kaddr = page_address(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002383 memcpy(kaddr, symname, pathlen);
2384 if (pathlen < PAGE_SIZE)
2385 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
Chuck Lever873101b2006-08-22 20:06:23 -04002386
Trond Myklebust1ca42382013-08-21 12:36:04 -04002387 trace_nfs_symlink_enter(dir, dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -04002388 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002389 trace_nfs_symlink_exit(dir, dentry, error);
Chuck Lever873101b2006-08-22 20:06:23 -04002390 if (error != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01002391 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
Chuck Lever873101b2006-08-22 20:06:23 -04002392 dir->i_sb->s_id, dir->i_ino,
Al Viro6de14722013-09-16 10:53:17 -04002393 dentry, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04002395 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002396 return error;
2397 }
2398
Trond Myklebust342a67f2021-07-07 22:08:32 -04002399 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2400
Chuck Lever873101b2006-08-22 20:06:23 -04002401 /*
2402 * No big deal if we can't add this page to the page cache here.
2403 * READLINK will get the missing page from the server if needed.
2404 */
David Howells2b0143b2015-03-17 22:25:59 +00002405 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
Chuck Lever873101b2006-08-22 20:06:23 -04002406 GFP_KERNEL)) {
Chuck Lever873101b2006-08-22 20:06:23 -04002407 SetPageUptodate(page);
2408 unlock_page(page);
Rafael Aquinia0b54add2014-02-10 14:25:48 -08002409 /*
2410 * add_to_page_cache_lru() grabs an extra page refcount.
2411 * Drop it here to avoid leaking this page later.
2412 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002413 put_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002414 } else
2415 __free_page(page);
2416
Chuck Lever873101b2006-08-22 20:06:23 -04002417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002419EXPORT_SYMBOL_GPL(nfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Bryan Schumaker597d9282012-07-16 16:39:10 -04002421int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2423{
David Howells2b0143b2015-03-17 22:25:59 +00002424 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 int error;
2426
Al Viro6de14722013-09-16 10:53:17 -04002427 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2428 old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Trond Myklebust1fd10852013-08-21 13:54:44 -04002430 trace_nfs_link_enter(inode, dir, dentry);
Trond Myklebust9697d232007-10-02 21:58:05 -04002431 d_drop(dentry);
Trond Myklebust20497502021-12-15 16:38:15 -05002432 if (S_ISREG(inode->i_mode))
2433 nfs_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04002435 if (error == 0) {
Trond Myklebust342a67f2021-07-07 22:08:32 -04002436 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Al Viro7de9c6ee2010-10-23 11:11:40 -04002437 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04002438 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04002439 }
Trond Myklebust1fd10852013-08-21 13:54:44 -04002440 trace_nfs_link_exit(inode, dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return error;
2442}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002443EXPORT_SYMBOL_GPL(nfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445/*
2446 * RENAME
2447 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2448 * different file handle for the same inode after a rename (e.g. when
2449 * moving to a different directory). A fail-safe method to do so would
2450 * be to look up old_dir/old_name, create a link to new_dir/new_name and
2451 * rename the old file using the sillyrename stuff. This way, the original
2452 * file in old_dir will go away when the last process iput()s the inode.
2453 *
2454 * FIXED.
2455 *
2456 * It actually works quite well. One needs to have the possibility for
2457 * at least one ".nfs..." file in each directory the file ever gets
2458 * moved or linked to which happens automagically with the new
2459 * implementation that only depends on the dcache stuff instead of
2460 * using the inode layer
2461 *
2462 * Unfortunately, things are a little more complicated than indicated
2463 * above. For a cross-directory move, we want to make sure we can get
2464 * rid of the old inode after the operation. This means there must be
2465 * no pending writes (if it's a file), and the use count must be 1.
2466 * If these conditions are met, we can drop the dentries before doing
2467 * the rename.
2468 */
Christian Brauner549c7292021-01-21 14:19:43 +01002469int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2470 struct dentry *old_dentry, struct inode *new_dir,
2471 struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
David Howells2b0143b2015-03-17 22:25:59 +00002473 struct inode *old_inode = d_inode(old_dentry);
2474 struct inode *new_inode = d_inode(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002475 struct dentry *dentry = NULL, *rehash = NULL;
Jeff Layton80a491f2014-03-17 07:06:56 -04002476 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int error = -EBUSY;
2478
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002479 if (flags)
2480 return -EINVAL;
2481
Al Viro6de14722013-09-16 10:53:17 -04002482 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2483 old_dentry, new_dentry,
Al Viro84d08fa2013-07-05 18:59:33 +04002484 d_count(new_dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Trond Myklebust70ded202013-08-21 12:08:45 -04002486 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05002488 * For non-directories, check whether the target is busy and if so,
2489 * make a copy of the dentry and then do a silly-rename. If the
2490 * silly-rename succeeds, the copied dentry is hashed and becomes
2491 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 */
Miklos Szeredi27226102009-12-03 15:58:56 -05002493 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2494 /*
2495 * To prevent any new references to the target during the
2496 * rename, we unhash the dentry in advance.
2497 */
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002498 if (!d_unhashed(new_dentry)) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002499 d_drop(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002500 rehash = new_dentry;
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Al Viro84d08fa2013-07-05 18:59:33 +04002503 if (d_count(new_dentry) > 2) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002504 int err;
2505
2506 /* copy the target dentry's name */
2507 dentry = d_alloc(new_dentry->d_parent,
2508 &new_dentry->d_name);
2509 if (!dentry)
2510 goto out;
2511
2512 /* silly-rename the existing target ... */
2513 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05002514 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05002515 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002516
2517 new_dentry = dentry;
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002518 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002519 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05002520 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Trond Myklebust6ff9d992021-12-15 16:38:16 -05002523 if (S_ISREG(old_inode->i_mode))
2524 nfs_sync_inode(old_inode);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002525 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
Jeff Layton80a491f2014-03-17 07:06:56 -04002526 if (IS_ERR(task)) {
2527 error = PTR_ERR(task);
2528 goto out;
2529 }
2530
2531 error = rpc_wait_for_completion_task(task);
Benjamin Coddington818a8db2017-06-16 11:13:00 -04002532 if (error != 0) {
2533 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2534 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2535 smp_wmb();
2536 } else
Jeff Layton80a491f2014-03-17 07:06:56 -04002537 error = task->tk_status;
2538 rpc_put_task(task);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002539 /* Ensure the inode attributes are revalidated */
2540 if (error == 0) {
2541 spin_lock(&old_inode->i_lock);
2542 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustac46b3d2021-03-08 14:42:54 -05002543 nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2544 NFS_INO_INVALID_CTIME |
2545 NFS_INO_REVAL_FORCED);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002546 spin_unlock(&old_inode->i_lock);
2547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548out:
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002549 if (rehash)
2550 d_rehash(rehash);
Trond Myklebust70ded202013-08-21 12:08:45 -04002551 trace_nfs_rename_exit(old_dir, old_dentry,
2552 new_dir, new_dentry, error);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002553 if (!error) {
2554 if (new_inode != NULL)
2555 nfs_drop_nlink(new_inode);
2556 /*
2557 * The d_move() should be here instead of in an async RPC completion
2558 * handler because we need the proper locks to move the dentry. If
2559 * we're interrupted by a signal, the async RPC completion handler
2560 * should mark the directories for revalidation.
2561 */
2562 d_move(old_dentry, new_dentry);
Trond Myklebustd8032242017-11-06 15:28:04 -05002563 nfs_set_verifier(old_dentry,
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002564 nfs_save_change_attribute(new_dir));
2565 } else if (error == -ENOENT)
2566 nfs_dentry_handle_enoent(old_dentry);
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /* new dentry created? */
2569 if (dentry)
2570 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return error;
2572}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002573EXPORT_SYMBOL_GPL(nfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002575static DEFINE_SPINLOCK(nfs_access_lru_lock);
2576static LIST_HEAD(nfs_access_lru_list);
2577static atomic_long_t nfs_access_nr_entries;
2578
Trond Myklebusta8b373e2020-02-08 09:14:11 -05002579static unsigned long nfs_access_max_cachesize = 4*1024*1024;
Trond Myklebust3a505842014-07-21 13:53:48 -04002580module_param(nfs_access_max_cachesize, ulong, 0644);
2581MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2582
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002583static void nfs_access_free_entry(struct nfs_access_entry *entry)
2584{
NeilBrown6238aec2021-09-28 09:47:57 +10002585 put_group_info(entry->group_info);
NeilBrownf682a392014-07-14 11:28:20 +10002586 kfree_rcu(entry, rcu_head);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002587 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002588 atomic_long_dec(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002589 smp_mb__after_atomic();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002590}
2591
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002592static void nfs_access_free_list(struct list_head *head)
2593{
2594 struct nfs_access_entry *cache;
2595
2596 while (!list_empty(head)) {
2597 cache = list_entry(head->next, struct nfs_access_entry, lru);
2598 list_del(&cache->lru);
2599 nfs_access_free_entry(cache);
2600 }
2601}
2602
Trond Myklebust3a505842014-07-21 13:53:48 -04002603static unsigned long
2604nfs_do_access_cache_scan(unsigned int nr_to_scan)
Trond Myklebust979df722006-07-25 11:28:19 -04002605{
2606 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002607 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04002608 struct nfs_access_entry *cache;
Dave Chinner1ab6c492013-08-28 10:18:09 +10002609 long freed = 0;
Trond Myklebust979df722006-07-25 11:28:19 -04002610
Trond Myklebusta50f7952007-06-05 19:23:43 -04002611 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002612 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04002613 struct inode *inode;
2614
2615 if (nr_to_scan-- == 0)
2616 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002617 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04002618 spin_lock(&inode->i_lock);
2619 if (list_empty(&nfsi->access_cache_entry_lru))
2620 goto remove_lru_entry;
2621 cache = list_entry(nfsi->access_cache_entry_lru.next,
2622 struct nfs_access_entry, lru);
2623 list_move(&cache->lru, &head);
2624 rb_erase(&cache->rb_node, &nfsi->access_cache);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002625 freed++;
Trond Myklebust979df722006-07-25 11:28:19 -04002626 if (!list_empty(&nfsi->access_cache_entry_lru))
2627 list_move_tail(&nfsi->access_cache_inode_lru,
2628 &nfs_access_lru_list);
2629 else {
2630remove_lru_entry:
2631 list_del_init(&nfsi->access_cache_inode_lru);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002632 smp_mb__before_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002633 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002634 smp_mb__after_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002635 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002636 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002637 }
2638 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002639 nfs_access_free_list(&head);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002640 return freed;
2641}
2642
2643unsigned long
Trond Myklebust3a505842014-07-21 13:53:48 -04002644nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2645{
2646 int nr_to_scan = sc->nr_to_scan;
2647 gfp_t gfp_mask = sc->gfp_mask;
2648
2649 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2650 return SHRINK_STOP;
2651 return nfs_do_access_cache_scan(nr_to_scan);
2652}
2653
2654
2655unsigned long
Dave Chinner1ab6c492013-08-28 10:18:09 +10002656nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2657{
Glauber Costa55f841c2013-08-28 10:17:53 +10002658 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
Trond Myklebust979df722006-07-25 11:28:19 -04002659}
2660
Trond Myklebust3a505842014-07-21 13:53:48 -04002661static void
2662nfs_access_cache_enforce_limit(void)
2663{
2664 long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2665 unsigned long diff;
2666 unsigned int nr_to_scan;
2667
2668 if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2669 return;
2670 nr_to_scan = 100;
2671 diff = nr_entries - nfs_access_max_cachesize;
2672 if (diff < nr_to_scan)
2673 nr_to_scan = diff;
2674 nfs_do_access_cache_scan(nr_to_scan);
2675}
2676
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002677static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002678{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002679 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002680 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002681 struct nfs_access_entry *entry;
2682
2683 /* Unhook entries from the cache */
2684 while ((n = rb_first(root_node)) != NULL) {
2685 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2686 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002687 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002688 }
2689 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002690}
2691
2692void nfs_access_zap_cache(struct inode *inode)
2693{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002694 LIST_HEAD(head);
2695
2696 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2697 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002698 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002699 spin_lock(&nfs_access_lru_lock);
2700 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002701 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002702
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002703 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002704 __nfs_access_zap_cache(NFS_I(inode), &head);
2705 spin_unlock(&inode->i_lock);
2706 spin_unlock(&nfs_access_lru_lock);
2707 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002708}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04002709EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002710
NeilBrown6238aec2021-09-28 09:47:57 +10002711static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
2712{
2713 struct group_info *ga, *gb;
2714 int g;
2715
2716 if (uid_lt(a->fsuid, b->fsuid))
2717 return -1;
2718 if (uid_gt(a->fsuid, b->fsuid))
2719 return 1;
2720
2721 if (gid_lt(a->fsgid, b->fsgid))
2722 return -1;
2723 if (gid_gt(a->fsgid, b->fsgid))
2724 return 1;
2725
2726 ga = a->group_info;
2727 gb = b->group_info;
2728 if (ga == gb)
2729 return 0;
2730 if (ga == NULL)
2731 return -1;
2732 if (gb == NULL)
2733 return 1;
2734 if (ga->ngroups < gb->ngroups)
2735 return -1;
2736 if (ga->ngroups > gb->ngroups)
2737 return 1;
2738
2739 for (g = 0; g < ga->ngroups; g++) {
2740 if (gid_lt(ga->gid[g], gb->gid[g]))
2741 return -1;
2742 if (gid_gt(ga->gid[g], gb->gid[g]))
2743 return 1;
2744 }
2745 return 0;
2746}
2747
NeilBrownb68572e2018-12-03 11:30:30 +11002748static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002749{
2750 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002751
2752 while (n != NULL) {
NeilBrownb68572e2018-12-03 11:30:30 +11002753 struct nfs_access_entry *entry =
2754 rb_entry(n, struct nfs_access_entry, rb_node);
NeilBrown6238aec2021-09-28 09:47:57 +10002755 int cmp = access_cmp(cred, entry);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002756
NeilBrownb68572e2018-12-03 11:30:30 +11002757 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002758 n = n->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002759 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002760 n = n->rb_right;
2761 else
2762 return entry;
2763 }
2764 return NULL;
2765}
2766
NeilBrownb5e7b592021-09-28 09:47:57 +10002767static 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 -07002768{
Chuck Lever55296802005-08-18 11:24:09 -07002769 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002770 struct nfs_access_entry *cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002771 bool retry = true;
2772 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002774 spin_lock(&inode->i_lock);
Trond Myklebust57b69182016-06-03 17:07:19 -04002775 for(;;) {
2776 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2777 goto out_zap;
2778 cache = nfs_access_search_rbtree(inode, cred);
2779 err = -ENOENT;
2780 if (cache == NULL)
2781 goto out;
2782 /* Found an entry, is our attribute cache valid? */
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002783 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
Trond Myklebust57b69182016-06-03 17:07:19 -04002784 break;
Trond Myklebust5c965db2020-01-06 15:39:36 -05002785 if (!retry)
2786 break;
Trond Myklebust57b69182016-06-03 17:07:19 -04002787 err = -ECHILD;
2788 if (!may_block)
2789 goto out;
Trond Myklebust57b69182016-06-03 17:07:19 -04002790 spin_unlock(&inode->i_lock);
2791 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2792 if (err)
2793 return err;
2794 spin_lock(&inode->i_lock);
2795 retry = false;
2796 }
NeilBrownb5e7b592021-09-28 09:47:57 +10002797 *mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002798 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002799 err = 0;
2800out:
2801 spin_unlock(&inode->i_lock);
2802 return err;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002803out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002804 spin_unlock(&inode->i_lock);
2805 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002806 return -ENOENT;
2807}
2808
NeilBrownb5e7b592021-09-28 09:47:57 +10002809static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
NeilBrownf682a392014-07-14 11:28:20 +10002810{
2811 /* Only check the most recently returned cache entry,
2812 * but do it without locking.
2813 */
2814 struct nfs_inode *nfsi = NFS_I(inode);
2815 struct nfs_access_entry *cache;
2816 int err = -ECHILD;
2817 struct list_head *lh;
2818
2819 rcu_read_lock();
2820 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2821 goto out;
Madhuparna Bhowmik9f01eb52019-12-10 11:16:39 +05302822 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
NeilBrownf682a392014-07-14 11:28:20 +10002823 cache = list_entry(lh, struct nfs_access_entry, lru);
2824 if (lh == &nfsi->access_cache_entry_lru ||
NeilBrown6238aec2021-09-28 09:47:57 +10002825 access_cmp(cred, cache) != 0)
NeilBrownf682a392014-07-14 11:28:20 +10002826 cache = NULL;
2827 if (cache == NULL)
2828 goto out;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002829 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
NeilBrownf682a392014-07-14 11:28:20 +10002830 goto out;
NeilBrownb5e7b592021-09-28 09:47:57 +10002831 *mask = cache->mask;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002832 err = 0;
NeilBrownf682a392014-07-14 11:28:20 +10002833out:
2834 rcu_read_unlock();
2835 return err;
2836}
2837
NeilBrownb5e7b592021-09-28 09:47:57 +10002838int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2839 u32 *mask, bool may_block)
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002840{
2841 int status;
2842
NeilBrownb5e7b592021-09-28 09:47:57 +10002843 status = nfs_access_get_cached_rcu(inode, cred, mask);
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002844 if (status != 0)
NeilBrownb5e7b592021-09-28 09:47:57 +10002845 status = nfs_access_get_cached_locked(inode, cred, mask,
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002846 may_block);
2847
2848 return status;
2849}
2850EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2851
NeilBrown73fbb3f2021-09-28 09:47:57 +10002852static void nfs_access_add_rbtree(struct inode *inode,
2853 struct nfs_access_entry *set,
2854 const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002855{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002856 struct nfs_inode *nfsi = NFS_I(inode);
2857 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002858 struct rb_node **p = &root_node->rb_node;
2859 struct rb_node *parent = NULL;
2860 struct nfs_access_entry *entry;
NeilBrownb68572e2018-12-03 11:30:30 +11002861 int cmp;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002862
2863 spin_lock(&inode->i_lock);
2864 while (*p != NULL) {
2865 parent = *p;
2866 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
NeilBrown6238aec2021-09-28 09:47:57 +10002867 cmp = access_cmp(cred, entry);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002868
NeilBrownb68572e2018-12-03 11:30:30 +11002869 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002870 p = &parent->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002871 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002872 p = &parent->rb_right;
2873 else
2874 goto found;
2875 }
2876 rb_link_node(&set->rb_node, parent, p);
2877 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002878 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002879 spin_unlock(&inode->i_lock);
2880 return;
2881found:
2882 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002883 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2884 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002885 spin_unlock(&inode->i_lock);
2886 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
NeilBrown73fbb3f2021-09-28 09:47:57 +10002889void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
2890 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002892 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2893 if (cache == NULL)
2894 return;
2895 RB_CLEAR_NODE(&cache->rb_node);
NeilBrown6238aec2021-09-28 09:47:57 +10002896 cache->fsuid = cred->fsuid;
2897 cache->fsgid = cred->fsgid;
2898 cache->group_info = get_group_info(cred->group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002900
NeilBrownf682a392014-07-14 11:28:20 +10002901 /* The above field assignments must be visible
2902 * before this item appears on the lru. We cannot easily
2903 * use rcu_assign_pointer, so just force the memory barrier.
2904 */
2905 smp_wmb();
NeilBrown73fbb3f2021-09-28 09:47:57 +10002906 nfs_access_add_rbtree(inode, cache, cred);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002907
2908 /* Update accounting */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002909 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002910 atomic_long_inc(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002911 smp_mb__after_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002912
2913 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002914 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002915 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002916 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2917 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2918 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002919 spin_unlock(&nfs_access_lru_lock);
2920 }
Trond Myklebust3a505842014-07-21 13:53:48 -04002921 nfs_access_cache_enforce_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002923EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2924
Anna Schumaker3c181822017-07-26 10:14:55 -04002925#define NFS_MAY_READ (NFS_ACCESS_READ)
2926#define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
2927 NFS_ACCESS_EXTEND | \
2928 NFS_ACCESS_DELETE)
2929#define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
2930 NFS_ACCESS_EXTEND)
Trond Myklebustecbb9032017-07-11 17:54:35 -04002931#define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
Anna Schumaker3c181822017-07-26 10:14:55 -04002932#define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
2933#define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002934static int
Trond Myklebustecbb9032017-07-11 17:54:35 -04002935nfs_access_calc_mask(u32 access_result, umode_t umode)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002936{
2937 int mask = 0;
2938
2939 if (access_result & NFS_MAY_READ)
2940 mask |= MAY_READ;
Trond Myklebustecbb9032017-07-11 17:54:35 -04002941 if (S_ISDIR(umode)) {
2942 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
2943 mask |= MAY_WRITE;
2944 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
2945 mask |= MAY_EXEC;
2946 } else if (S_ISREG(umode)) {
2947 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2948 mask |= MAY_WRITE;
2949 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
2950 mask |= MAY_EXEC;
2951 } else if (access_result & NFS_MAY_WRITE)
2952 mask |= MAY_WRITE;
Trond Myklebust15d4b732017-07-11 17:54:32 -04002953 return mask;
2954}
2955
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002956void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2957{
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002958 entry->mask = access_result;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002959}
2960EXPORT_SYMBOL_GPL(nfs_access_set_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
NeilBrownb68572e2018-12-03 11:30:30 +11002962static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 struct nfs_access_entry cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002965 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
Trond Myklebuste8194b72020-01-06 15:25:12 -05002966 int cache_mask = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 int status;
2968
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002969 trace_nfs_access_enter(inode);
2970
NeilBrownb5e7b592021-09-28 09:47:57 +10002971 status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (status == 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002973 goto out_cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
NeilBrownf3324a22014-07-14 11:28:20 +10002975 status = -ECHILD;
Trond Myklebust57b69182016-06-03 17:07:19 -04002976 if (!may_block)
NeilBrownf3324a22014-07-14 11:28:20 +10002977 goto out;
2978
Anna Schumaker1750d922017-07-26 12:00:21 -04002979 /*
2980 * Determine which access bits we want to ask for...
2981 */
2982 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
Frank van der Linden72832a22020-06-23 22:38:58 +00002983 if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
2984 cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
2985 NFS_ACCESS_XALIST;
2986 }
Anna Schumaker1750d922017-07-26 12:00:21 -04002987 if (S_ISDIR(inode->i_mode))
2988 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
2989 else
2990 cache.mask |= NFS_ACCESS_EXECUTE;
NeilBrown73fbb3f2021-09-28 09:47:57 +10002991 status = NFS_PROTO(inode)->access(inode, &cache, cred);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002992 if (status != 0) {
2993 if (status == -ESTALE) {
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002994 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002995 nfs_set_inode_stale(inode);
2996 else
2997 nfs_zap_caches(inode);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002998 }
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002999 goto out;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04003000 }
NeilBrown73fbb3f2021-09-28 09:47:57 +10003001 nfs_access_add_cache(inode, &cache, cred);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003002out_cached:
Trond Myklebustecbb9032017-07-11 17:54:35 -04003003 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
Trond Myklebustbd8b2442017-07-11 17:54:34 -04003004 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003005 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006out:
Trond Myklebuste8194b72020-01-06 15:25:12 -05003007 trace_nfs_access_exit(inode, mask, cache_mask, status);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04003008 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
Trond Myklebustaf22f942007-08-10 17:45:10 -04003011static int nfs_open_permission_mask(int openflags)
3012{
3013 int mask = 0;
3014
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05003015 if (openflags & __FMODE_EXEC) {
3016 /* ONLY check exec rights */
3017 mask = MAY_EXEC;
3018 } else {
3019 if ((openflags & O_ACCMODE) != O_WRONLY)
3020 mask |= MAY_READ;
3021 if ((openflags & O_ACCMODE) != O_RDONLY)
3022 mask |= MAY_WRITE;
3023 }
3024
Trond Myklebustaf22f942007-08-10 17:45:10 -04003025 return mask;
3026}
3027
NeilBrownb68572e2018-12-03 11:30:30 +11003028int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
Trond Myklebustaf22f942007-08-10 17:45:10 -04003029{
3030 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3031}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04003032EXPORT_SYMBOL_GPL(nfs_may_open);
Trond Myklebustaf22f942007-08-10 17:45:10 -04003033
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003034static int nfs_execute_ok(struct inode *inode, int mask)
3035{
3036 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust21c3ba72016-12-16 18:40:03 -05003037 int ret = 0;
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003038
Trond Myklebust38258272018-07-24 14:27:11 -04003039 if (S_ISDIR(inode->i_mode))
3040 return 0;
Trond Myklebust720869e2021-04-13 09:41:16 -04003041 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
Trond Myklebust21c3ba72016-12-16 18:40:03 -05003042 if (mask & MAY_NOT_BLOCK)
3043 return -ECHILD;
3044 ret = __nfs_revalidate_inode(server, inode);
3045 }
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003046 if (ret == 0 && !execute_ok(inode))
3047 ret = -EACCES;
3048 return ret;
3049}
3050
Christian Brauner549c7292021-01-21 14:19:43 +01003051int nfs_permission(struct user_namespace *mnt_userns,
3052 struct inode *inode,
3053 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
NeilBrownb68572e2018-12-03 11:30:30 +11003055 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 int res = 0;
3057
Chuck Lever91d5b472006-03-20 13:44:14 -05003058 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
3059
Al Viroe6305c42008-07-15 21:03:57 -04003060 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 goto out;
3062 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04003063 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto force_lookup;
3065
3066 switch (inode->i_mode & S_IFMT) {
3067 case S_IFLNK:
3068 goto out;
3069 case S_IFREG:
Trond Myklebust762674f2015-12-26 21:54:58 -05003070 if ((mask & MAY_OPEN) &&
3071 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 break;
3074 case S_IFDIR:
3075 /*
3076 * Optimize away all write operations, since the server
3077 * will check permissions when we perform the op.
3078 */
3079 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
3080 goto out;
3081 }
3082
3083force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (!NFS_PROTO(inode)->access)
3085 goto out_notsup;
3086
Zhouyi Zhoueb095c12020-03-06 03:45:26 +00003087 res = nfs_do_access(inode, cred, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088out:
Trond Myklebust5c5fc092015-12-28 19:30:05 -05003089 if (!res && (mask & MAY_EXEC))
3090 res = nfs_execute_ok(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +02003091
Niels de Vos1e8968c2013-12-17 18:20:16 +01003092 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05003093 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return res;
3095out_notsup:
NeilBrownd51ac1a2014-07-14 11:28:20 +10003096 if (mask & MAY_NOT_BLOCK)
3097 return -ECHILD;
3098
Trond Myklebust720869e2021-04-13 09:41:16 -04003099 res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3100 NFS_INO_INVALID_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (res == 0)
Christian Brauner47291ba2021-01-21 14:19:24 +01003102 res = generic_permission(&init_user_ns, inode, mask);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05003103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04003105EXPORT_SYMBOL_GPL(nfs_permission);