blob: b9001123ec84e5d7b7a06c906fde5d232d8c2347 [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
Bryan Schumakerddda8e02012-07-30 16:05:23 -040021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/time.h>
23#include <linux/errno.h>
24#include <linux/stat.h>
25#include <linux/fcntl.h>
26#include <linux/string.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/sunrpc/clnt.h>
31#include <linux/nfs_fs.h>
32#include <linux/nfs_mount.h>
33#include <linux/pagemap.h>
Chuck Lever873101b2006-08-22 20:06:23 -040034#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/namei.h>
David Howells54ceac42006-08-22 20:06:13 -040036#include <linux/mount.h>
Mel Gormana0b8cab32013-07-03 15:02:32 -070037#include <linux/swap.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/sched.h>
Catalin Marinas04e4bd12010-11-11 12:53:47 +000039#include <linux/kmemleak.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000040#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050043#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080044#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040045#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Trond Myklebustf4ce1292013-08-19 18:59:33 -040047#include "nfstrace.h"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* #define NFS_DEBUG_VERBOSE 1 */
50
51static int nfs_opendir(struct inode *, struct file *);
Bryan Schumaker480c2002011-03-23 14:48:29 -040052static int nfs_closedir(struct inode *, struct file *);
Al Viro23db8622013-05-17 16:34:50 -040053static int nfs_readdir(struct file *, struct dir_context *);
Josef Bacik02c24a82011-07-16 20:44:56 -040054static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000055static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Trond Myklebust11de3b12010-12-01 14:17:06 -050056static void nfs_readdir_clear_array(struct page*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080058const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000059 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .read = generic_read_dir,
Trond Myklebust93a6ab72020-02-02 17:53:56 -050061 .iterate_shared = nfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .open = nfs_opendir,
Bryan Schumaker480c2002011-03-23 14:48:29 -040063 .release = nfs_closedir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .fsync = nfs_fsync_dir,
65};
66
Trond Myklebust11de3b12010-12-01 14:17:06 -050067const struct address_space_operations nfs_dir_aops = {
68 .freepage = nfs_readdir_clear_array,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040069};
70
NeilBrown684f39b2018-12-03 11:30:30 +110071static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
Bryan Schumaker480c2002011-03-23 14:48:29 -040072{
Trond Myklebust311324a2014-02-07 17:02:08 -050073 struct nfs_inode *nfsi = NFS_I(dir);
Bryan Schumaker480c2002011-03-23 14:48:29 -040074 struct nfs_open_dir_context *ctx;
75 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76 if (ctx != NULL) {
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -040077 ctx->duped = 0;
Trond Myklebust311324a2014-02-07 17:02:08 -050078 ctx->attr_gencount = nfsi->attr_gencount;
Bryan Schumaker480c2002011-03-23 14:48:29 -040079 ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -040080 ctx->dup_cookie = 0;
NeilBrown684f39b2018-12-03 11:30:30 +110081 ctx->cred = get_cred(cred);
Trond Myklebust311324a2014-02-07 17:02:08 -050082 spin_lock(&dir->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -040083 if (list_empty(&nfsi->open_files) &&
84 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
85 nfsi->cache_validity |= NFS_INO_INVALID_DATA |
86 NFS_INO_REVAL_FORCED;
Trond Myklebust311324a2014-02-07 17:02:08 -050087 list_add(&ctx->list, &nfsi->open_files);
88 spin_unlock(&dir->i_lock);
Trond Myklebust0c030802011-07-30 12:45:35 -040089 return ctx;
90 }
91 return ERR_PTR(-ENOMEM);
Bryan Schumaker480c2002011-03-23 14:48:29 -040092}
93
Trond Myklebust311324a2014-02-07 17:02:08 -050094static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
Bryan Schumaker480c2002011-03-23 14:48:29 -040095{
Trond Myklebust311324a2014-02-07 17:02:08 -050096 spin_lock(&dir->i_lock);
97 list_del(&ctx->list);
98 spin_unlock(&dir->i_lock);
NeilBrown684f39b2018-12-03 11:30:30 +110099 put_cred(ctx->cred);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400100 kfree(ctx);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Open file
105 */
106static int
107nfs_opendir(struct inode *inode, struct file *filp)
108{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400109 int res = 0;
110 struct nfs_open_dir_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Al Viro6de14722013-09-16 10:53:17 -0400112 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400113
114 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500115
NeilBrown684f39b2018-12-03 11:30:30 +1100116 ctx = alloc_nfs_open_dir_context(inode, current_cred());
Bryan Schumaker480c2002011-03-23 14:48:29 -0400117 if (IS_ERR(ctx)) {
118 res = PTR_ERR(ctx);
119 goto out;
120 }
121 filp->private_data = ctx;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400122out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return res;
124}
125
Bryan Schumaker480c2002011-03-23 14:48:29 -0400126static int
127nfs_closedir(struct inode *inode, struct file *filp)
128{
Al Viroa4555892014-10-21 20:11:25 -0400129 put_nfs_open_dir_context(file_inode(filp), filp->private_data);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400130 return 0;
131}
132
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400133struct nfs_cache_array_entry {
134 u64 cookie;
135 u64 ino;
136 struct qstr string;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500137 unsigned char d_type;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400138};
139
140struct nfs_cache_array {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400141 u64 last_cookie;
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500142 unsigned int size;
143 unsigned char page_full : 1,
144 page_is_eof : 1;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -0500145 struct nfs_cache_array_entry array[];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400146};
147
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500148typedef struct nfs_readdir_descriptor {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct file *file;
150 struct page *page;
Al Viro23db8622013-05-17 16:34:50 -0400151 struct dir_context *ctx;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500152 pgoff_t page_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500153 u64 dir_cookie;
Trond Myklebust0aded702010-11-30 21:56:32 -0500154 u64 last_cookie;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500155 u64 dup_cookie;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000156 loff_t current_index;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500157 loff_t prev_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400158
Trond Myklebusta1147b82020-02-05 09:01:52 -0500159 unsigned long dir_verifier;
Neil Brown1f4eab72007-04-16 09:35:27 +1000160 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400161 unsigned long gencount;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500162 unsigned long attr_gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400163 unsigned int cache_entry_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500164 signed char duped;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400165 bool plus;
166 bool eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167} nfs_readdir_descriptor_t;
168
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500169static void nfs_readdir_array_init(struct nfs_cache_array *array)
170{
171 memset(array, 0, sizeof(struct nfs_cache_array));
172}
173
174static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie)
Trond Myklebust4b310312020-02-02 17:53:53 -0500175{
176 struct nfs_cache_array *array;
177
178 array = kmap_atomic(page);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500179 nfs_readdir_array_init(array);
180 array->last_cookie = last_cookie;
Trond Myklebust4b310312020-02-02 17:53:53 -0500181 kunmap_atomic(array);
182}
183
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400184/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400185 * we are freeing strings created by nfs_add_to_readdir_array()
186 */
187static
Trond Myklebust11de3b12010-12-01 14:17:06 -0500188void nfs_readdir_clear_array(struct page *page)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400189{
Trond Myklebust11de3b12010-12-01 14:17:06 -0500190 struct nfs_cache_array *array;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400191 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500192
Cong Wang2b86ce22011-11-25 23:14:33 +0800193 array = kmap_atomic(page);
Benjamin Coddingtonb044f642017-03-10 17:07:46 -0500194 for (i = 0; i < array->size; i++)
195 kfree(array->array[i].string.name);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500196 nfs_readdir_array_init(array);
Cong Wang2b86ce22011-11-25 23:14:33 +0800197 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400198}
199
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500200static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
201{
202 array->page_is_eof = 1;
203 array->page_full = 1;
204}
205
206static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
207{
208 return array->page_full;
209}
210
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400211/*
212 * the caller is responsible for freeing qstr.name
213 * when called by nfs_readdir_add_to_array, the strings will be freed in
214 * nfs_clear_readdir_array()
215 */
216static
Trond Myklebust4a201d62010-10-23 14:53:23 -0400217int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400218{
219 string->len = len;
Trond Myklebust3803d672020-02-02 17:53:55 -0500220 string->name = kmemdup_nul(name, len, GFP_KERNEL);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400221 if (string->name == NULL)
222 return -ENOMEM;
Catalin Marinas04e4bd12010-11-11 12:53:47 +0000223 /*
224 * Avoid a kmemleak false positive. The pointer to the name is stored
225 * in a page cache page which kmemleak does not scan.
226 */
227 kmemleak_not_leak(string->name);
Linus Torvalds8387ff22016-06-10 07:51:30 -0700228 string->hash = full_name_hash(NULL, name, len);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400229 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400230}
231
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500232/*
233 * Check that the next array entry lies entirely within the page bounds
234 */
235static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
236{
237 struct nfs_cache_array_entry *cache_entry;
238
239 if (array->page_full)
240 return -ENOSPC;
241 cache_entry = &array->array[array->size + 1];
242 if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
243 array->page_full = 1;
244 return -ENOSPC;
245 }
246 return 0;
247}
248
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400249static
250int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
251{
Fabian Frederick0795bf82017-05-03 20:52:21 +0200252 struct nfs_cache_array *array = kmap(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400253 struct nfs_cache_array_entry *cache_entry;
254 int ret;
255
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500256 ret = nfs_readdir_array_can_expand(array);
257 if (ret)
Trond Myklebust30200932010-11-20 15:18:22 -0500258 goto out;
259
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500260 cache_entry = &array->array[array->size];
Trond Myklebust4a201d62010-10-23 14:53:23 -0400261 cache_entry->cookie = entry->prev_cookie;
262 cache_entry->ino = entry->ino;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500263 cache_entry->d_type = entry->d_type;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400264 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
265 if (ret)
266 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400267 array->last_cookie = entry->cookie;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500268 array->size++;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500269 if (entry->eof != 0)
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500270 nfs_readdir_array_set_eof(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400271out:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200272 kunmap(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400273 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400274}
275
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500276static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
277 pgoff_t index, u64 last_cookie)
278{
279 struct page *page;
280
281 page = grab_cache_page(mapping, index);
282 if (page && !PageUptodate(page)) {
283 nfs_readdir_page_init_array(page, last_cookie);
284 if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
285 nfs_zap_mapping(mapping->host, mapping);
286 SetPageUptodate(page);
287 }
288
289 return page;
290}
291
292static u64 nfs_readdir_page_last_cookie(struct page *page)
293{
294 struct nfs_cache_array *array;
295 u64 ret;
296
297 array = kmap_atomic(page);
298 ret = array->last_cookie;
299 kunmap_atomic(array);
300 return ret;
301}
302
303static bool nfs_readdir_page_needs_filling(struct page *page)
304{
305 struct nfs_cache_array *array;
306 bool ret;
307
308 array = kmap_atomic(page);
309 ret = !nfs_readdir_array_is_full(array);
310 kunmap_atomic(array);
311 return ret;
312}
313
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500314static void nfs_readdir_page_set_eof(struct page *page)
315{
316 struct nfs_cache_array *array;
317
318 array = kmap_atomic(page);
319 nfs_readdir_array_set_eof(array);
320 kunmap_atomic(array);
321}
322
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500323static void nfs_readdir_page_unlock_and_put(struct page *page)
324{
325 unlock_page(page);
326 put_page(page);
327}
328
329static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
330 pgoff_t index, u64 cookie)
331{
332 struct page *page;
333
334 page = nfs_readdir_page_get_locked(mapping, index, cookie);
335 if (page) {
336 if (nfs_readdir_page_last_cookie(page) == cookie)
337 return page;
338 nfs_readdir_page_unlock_and_put(page);
339 }
340 return NULL;
341}
342
Trond Myklebust59e356a2020-02-03 14:49:33 -0500343static inline
344int is_32bit_api(void)
345{
346#ifdef CONFIG_COMPAT
347 return in_compat_syscall();
348#else
349 return (BITS_PER_LONG == 32);
350#endif
351}
352
353static
354bool nfs_readdir_use_cookie(const struct file *filp)
355{
356 if ((filp->f_mode & FMODE_32BITHASH) ||
357 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
358 return false;
359 return true;
360}
361
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400362static
363int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
364{
Al Viro23db8622013-05-17 16:34:50 -0400365 loff_t diff = desc->ctx->pos - desc->current_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400366 unsigned int index;
367
368 if (diff < 0)
369 goto out_eof;
370 if (diff >= array->size) {
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500371 if (array->page_is_eof)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400372 goto out_eof;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400373 return -EAGAIN;
374 }
375
376 index = (unsigned int)diff;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500377 desc->dir_cookie = array->array[index].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400378 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400379 return 0;
380out_eof:
Thomas Meyer6089dd02017-10-07 16:02:21 +0200381 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400382 return -EBADCOOKIE;
383}
384
Jeff Layton4db72b42014-01-28 13:47:46 -0500385static bool
386nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
387{
388 if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
389 return false;
390 smp_rmb();
391 return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
392}
393
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400394static
395int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
396{
397 int i;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400398 loff_t new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400399 int status = -EAGAIN;
400
401 for (i = 0; i < array->size; i++) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500402 if (array->array[i].cookie == desc->dir_cookie) {
Al Viro496ad9a2013-01-23 17:07:38 -0500403 struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
Trond Myklebust0c030802011-07-30 12:45:35 -0400404
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400405 new_pos = desc->current_index + i;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500406 if (desc->attr_gencount != nfsi->attr_gencount ||
Jeff Layton4db72b42014-01-28 13:47:46 -0500407 !nfs_readdir_inode_mapping_valid(nfsi)) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500408 desc->duped = 0;
409 desc->attr_gencount = nfsi->attr_gencount;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500410 } else if (new_pos < desc->prev_index) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500411 if (desc->duped > 0
412 && desc->dup_cookie == desc->dir_cookie) {
Trond Myklebust0c030802011-07-30 12:45:35 -0400413 if (printk_ratelimit()) {
Al Viro6de14722013-09-16 10:53:17 -0400414 pr_notice("NFS: directory %pD2 contains a readdir loop."
Trond Myklebust0c030802011-07-30 12:45:35 -0400415 "Please contact your server vendor. "
Jeff Layton9581a4a2014-04-05 08:45:57 -0400416 "The file: %.*s has duplicate cookie %llu\n",
417 desc->file, array->array[i].string.len,
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500418 array->array[i].string.name, desc->dir_cookie);
Trond Myklebust0c030802011-07-30 12:45:35 -0400419 }
420 status = -ELOOP;
421 goto out;
422 }
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500423 desc->dup_cookie = desc->dir_cookie;
424 desc->duped = -1;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400425 }
Trond Myklebust59e356a2020-02-03 14:49:33 -0500426 if (nfs_readdir_use_cookie(desc->file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500427 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500428 else
429 desc->ctx->pos = new_pos;
430 desc->prev_index = new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400431 desc->cache_entry_index = i;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500432 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400433 }
434 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500435 if (array->page_is_eof) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500436 status = -EBADCOOKIE;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500437 if (desc->dir_cookie == array->last_cookie)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200438 desc->eof = true;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500439 }
Trond Myklebust0c030802011-07-30 12:45:35 -0400440out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400441 return status;
442}
443
444static
445int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
446{
447 struct nfs_cache_array *array;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500448 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400449
Trond Myklebusted092222020-11-01 13:34:32 -0500450 array = kmap_atomic(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400451
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500452 if (desc->dir_cookie == 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400453 status = nfs_readdir_search_for_pos(array, desc);
454 else
455 status = nfs_readdir_search_for_cookie(array, desc);
456
Trond Myklebust47c716c2010-12-07 12:44:56 -0500457 if (status == -EAGAIN) {
Trond Myklebust0aded702010-11-30 21:56:32 -0500458 desc->last_cookie = array->last_cookie;
Trond Myklebuste47c0852011-03-23 08:43:09 -0400459 desc->current_index += array->size;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500460 desc->page_index++;
461 }
Trond Myklebusted092222020-11-01 13:34:32 -0500462 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400463 return status;
464}
465
466/* Fill a page with xdr information before transferring to the cache page */
467static
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400468int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400469 struct nfs_entry *entry, struct file *file, struct inode *inode)
470{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400471 struct nfs_open_dir_context *ctx = file->private_data;
NeilBrown684f39b2018-12-03 11:30:30 +1100472 const struct cred *cred = ctx->cred;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400473 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int error;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 again:
477 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400478 gencount = nfs_inc_attr_generation_counter();
Trond Myklebusta1147b82020-02-05 09:01:52 -0500479 desc->dir_verifier = nfs_save_change_attribute(inode);
Miklos Szeredibe62a1a2016-03-26 16:14:39 -0400480 error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 NFS_SERVER(inode)->dtsize, desc->plus);
482 if (error < 0) {
483 /* We requested READDIRPLUS, but the server doesn't grok it */
484 if (error == -ENOTSUPP && desc->plus) {
485 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200486 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400487 desc->plus = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto again;
489 }
490 goto error;
491 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000492 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400493 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400494error:
495 return error;
496}
497
Chuck Lever573c4e12010-12-14 14:58:11 +0000498static int xdr_decode(nfs_readdir_descriptor_t *desc,
499 struct nfs_entry *entry, struct xdr_stream *xdr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400500{
Trond Myklebust59e356a2020-02-03 14:49:33 -0500501 struct inode *inode = file_inode(desc->file);
Chuck Lever573c4e12010-12-14 14:58:11 +0000502 int error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400503
Trond Myklebust59e356a2020-02-03 14:49:33 -0500504 error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
Chuck Lever573c4e12010-12-14 14:58:11 +0000505 if (error)
506 return error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400507 entry->fattr->time_start = desc->timestamp;
508 entry->fattr->gencount = desc->gencount;
509 return 0;
510}
511
Trond Myklebustfa923362015-02-23 18:51:32 -0500512/* Match file and dirent using either filehandle or fileid
513 * Note: caller is responsible for checking the fsid
514 */
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400515static
516int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
517{
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400518 struct inode *inode;
Trond Myklebustfa923362015-02-23 18:51:32 -0500519 struct nfs_inode *nfsi;
520
David Howells2b0143b2015-03-17 22:25:59 +0000521 if (d_really_is_negative(dentry))
522 return 0;
Trond Myklebustfa923362015-02-23 18:51:32 -0500523
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400524 inode = d_inode(dentry);
525 if (is_bad_inode(inode) || NFS_STALE(inode))
526 return 0;
527
528 nfsi = NFS_I(inode);
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400529 if (entry->fattr->fileid != nfsi->fileid)
530 return 0;
531 if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
532 return 0;
533 return 1;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400534}
535
536static
Al Viro23db8622013-05-17 16:34:50 -0400537bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400538{
539 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
540 return false;
541 if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
542 return true;
Al Viro23db8622013-05-17 16:34:50 -0400543 if (ctx->pos == 0)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400544 return true;
545 return false;
546}
547
548/*
Trond Myklebust63519fb2016-11-19 11:21:54 -0500549 * This function is called by the lookup and getattr code to request the
550 * use of readdirplus to accelerate any future lookups in the same
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400551 * directory.
552 */
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400553void nfs_advise_use_readdirplus(struct inode *dir)
554{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500555 struct nfs_inode *nfsi = NFS_I(dir);
556
557 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
558 !list_empty(&nfsi->open_files))
559 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400560}
561
Trond Myklebust311324a2014-02-07 17:02:08 -0500562/*
563 * This function is mainly for use by nfs_getattr().
564 *
565 * If this is an 'ls -l', we want to force use of readdirplus.
566 * Do this by checking if there is an active file descriptor
567 * and calling nfs_advise_use_readdirplus, then forcing a
568 * cache flush.
569 */
570void nfs_force_use_readdirplus(struct inode *dir)
571{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500572 struct nfs_inode *nfsi = NFS_I(dir);
573
574 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
575 !list_empty(&nfsi->open_files)) {
576 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
Dai Ngo227823d2020-01-22 20:45:39 -0500577 invalidate_mapping_pages(dir->i_mapping,
578 nfsi->page_index + 1, -1);
Trond Myklebust311324a2014-02-07 17:02:08 -0500579 }
580}
581
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400582static
Trond Myklebusta1147b82020-02-05 09:01:52 -0500583void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
584 unsigned long dir_verifier)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400585{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700586 struct qstr filename = QSTR_INIT(entry->name, entry->len);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400587 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400588 struct dentry *dentry;
589 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400590 struct inode *inode;
David Quigleyaa9c2662013-05-22 12:50:44 -0400591 int status;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400592
Trond Myklebustfa923362015-02-23 18:51:32 -0500593 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
594 return;
Trond Myklebust6c441c22015-02-22 16:35:36 -0500595 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
596 return;
Trond Myklebust78d04af2016-09-20 14:34:24 -0400597 if (filename.len == 0)
598 return;
599 /* Validate that the name doesn't contain any illegal '\0' */
600 if (strnlen(filename.name, filename.len) != filename.len)
601 return;
602 /* ...or '/' */
603 if (strnchr(filename.name, filename.len, '/'))
604 return;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400605 if (filename.name[0] == '.') {
606 if (filename.len == 1)
607 return;
608 if (filename.len == 2 && filename.name[1] == '.')
609 return;
610 }
Linus Torvalds8387ff22016-06-10 07:51:30 -0700611 filename.hash = full_name_hash(parent, filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400612
Trond Myklebust4a201d62010-10-23 14:53:23 -0400613 dentry = d_lookup(parent, &filename);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400614again:
615 if (!dentry) {
616 dentry = d_alloc_parallel(parent, &filename, &wq);
617 if (IS_ERR(dentry))
618 return;
619 }
620 if (!d_in_lookup(dentry)) {
Trond Myklebust6c441c22015-02-22 16:35:36 -0500621 /* Is there a mountpoint here? If so, just exit */
622 if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
623 &entry->fattr->fsid))
624 goto out;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400625 if (nfs_same_file(dentry, entry)) {
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400626 if (!entry->fh->size)
627 goto out;
Trond Myklebusta1147b82020-02-05 09:01:52 -0500628 nfs_set_verifier(dentry, dir_verifier);
David Howells2b0143b2015-03-17 22:25:59 +0000629 status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -0400630 if (!status)
David Howells2b0143b2015-03-17 22:25:59 +0000631 nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400632 goto out;
633 } else {
Eric W. Biederman5542aa22014-02-13 09:46:25 -0800634 d_invalidate(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400635 dput(dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400636 dentry = NULL;
637 goto again;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400638 }
639 }
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400640 if (!entry->fh->size) {
641 d_lookup_done(dentry);
642 goto out;
643 }
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400644
David Quigley1775fd32013-05-22 12:50:42 -0400645 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
Al Viro41d28bc2014-10-12 22:24:21 -0400646 alias = d_splice_alias(inode, dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400647 d_lookup_done(dentry);
648 if (alias) {
649 if (IS_ERR(alias))
650 goto out;
651 dput(dentry);
652 dentry = alias;
653 }
Trond Myklebusta1147b82020-02-05 09:01:52 -0500654 nfs_set_verifier(dentry, dir_verifier);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400655out:
656 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400657}
658
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400659/* Perform conversion from xdr to cache array */
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500660static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
661 struct nfs_entry *entry,
662 struct page **xdr_pages,
663 struct page *fillme, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400664{
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500665 struct address_space *mapping = desc->file->f_mapping;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400666 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400667 struct xdr_buf buf;
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500668 struct page *scratch, *new, *page = fillme;
Trond Myklebust5c346852010-11-20 12:43:45 -0500669 int status;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400670
Trond Myklebust66502392011-01-08 17:45:38 -0500671 scratch = alloc_page(GFP_KERNEL);
672 if (scratch == NULL)
673 return -ENOMEM;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400674
Benny Halevyf7da7a12011-05-19 14:16:47 -0400675 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
Trond Myklebust66502392011-01-08 17:45:38 -0500676 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Bryan Schumaker99424382010-10-21 16:33:16 -0400677
678 do {
Jeffrey Mitchelld33030e2020-09-15 16:42:52 -0500679 if (entry->label)
680 entry->label->len = NFS4_MAXLABELLEN;
681
Bryan Schumaker99424382010-10-21 16:33:16 -0400682 status = xdr_decode(desc, entry, &stream);
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500683 if (status != 0)
Bryan Schumaker99424382010-10-21 16:33:16 -0400684 break;
Trond Myklebust5c346852010-11-20 12:43:45 -0500685
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400686 if (desc->plus)
Trond Myklebusta1147b82020-02-05 09:01:52 -0500687 nfs_prime_dcache(file_dentry(desc->file), entry,
688 desc->dir_verifier);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500689
Max Kellermanndb531db2019-07-12 16:18:06 +0200690 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500691 if (status != -ENOSPC)
692 continue;
693
694 if (page->mapping != mapping)
695 break;
696 new = nfs_readdir_page_get_next(mapping, page->index + 1,
697 entry->prev_cookie);
698 if (!new)
699 break;
700 if (page != fillme)
701 nfs_readdir_page_unlock_and_put(page);
702 page = new;
703 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500704 } while (!status && !entry->eof);
Bryan Schumaker99424382010-10-21 16:33:16 -0400705
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500706 switch (status) {
707 case -EBADCOOKIE:
708 if (entry->eof) {
709 nfs_readdir_page_set_eof(page);
710 status = 0;
711 }
712 break;
713 case -ENOSPC:
714 case -EAGAIN:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200715 status = 0;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500716 break;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400717 }
Trond Myklebust66502392011-01-08 17:45:38 -0500718
Trond Myklebust3b2a09f2020-11-01 13:14:10 -0500719 if (page != fillme)
720 nfs_readdir_page_unlock_and_put(page);
721
Trond Myklebust66502392011-01-08 17:45:38 -0500722 put_page(scratch);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500723 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400724}
725
726static
Anna Schumakerc7e96682015-07-13 14:01:25 -0400727void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400728{
729 unsigned int i;
730 for (i = 0; i < npages; i++)
731 put_page(pages[i]);
732}
733
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400734/*
zhangliguangbf211ca2019-02-16 08:38:40 +0800735 * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
736 * to nfs_readdir_free_pages()
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400737 */
738static
Anna Schumakerc7e96682015-07-13 14:01:25 -0400739int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400740{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400741 unsigned int i;
742
743 for (i = 0; i < npages; i++) {
744 struct page *page = alloc_page(GFP_KERNEL);
745 if (page == NULL)
746 goto out_freepages;
747 pages[i] = page;
748 }
Trond Myklebust66502392011-01-08 17:45:38 -0500749 return 0;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400750
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400751out_freepages:
Anna Schumakerc7e96682015-07-13 14:01:25 -0400752 nfs_readdir_free_pages(pages, i);
Trond Myklebust66502392011-01-08 17:45:38 -0500753 return -ENOMEM;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400754}
755
756static
757int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
758{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400759 struct page *pages[NFS_MAX_READDIR_PAGES];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400760 struct nfs_entry entry;
761 struct file *file = desc->file;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500762 int status = -ENOMEM;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400763 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400764
765 entry.prev_cookie = 0;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500766 entry.cookie = nfs_readdir_page_last_cookie(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400767 entry.eof = 0;
768 entry.fh = nfs_alloc_fhandle();
769 entry.fattr = nfs_alloc_fattr();
Chuck Lever573c4e12010-12-14 14:58:11 +0000770 entry.server = NFS_SERVER(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400771 if (entry.fh == NULL || entry.fattr == NULL)
772 goto out;
773
David Quigley14c43f72013-05-22 12:50:43 -0400774 entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
775 if (IS_ERR(entry.label)) {
776 status = PTR_ERR(entry.label);
777 goto out;
778 }
779
Anna Schumakerc7e96682015-07-13 14:01:25 -0400780 status = nfs_readdir_alloc_pages(pages, array_size);
Trond Myklebust66502392011-01-08 17:45:38 -0500781 if (status < 0)
Trond Myklebuste762a632020-11-01 13:31:20 -0500782 goto out_release_label;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400783 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500784 unsigned int pglen;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400785 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400786
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400787 if (status < 0)
788 break;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500789
Trond Myklebustac396122010-11-15 20:26:22 -0500790 pglen = status;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500791 if (pglen == 0) {
792 nfs_readdir_page_set_eof(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500793 break;
794 }
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500795
796 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
Trond Myklebuste762a632020-11-01 13:31:20 -0500797 } while (!status && nfs_readdir_page_needs_filling(page));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400798
Anna Schumakerc7e96682015-07-13 14:01:25 -0400799 nfs_readdir_free_pages(pages, array_size);
Trond Myklebuste762a632020-11-01 13:31:20 -0500800out_release_label:
David Quigley14c43f72013-05-22 12:50:43 -0400801 nfs4_label_free(entry.label);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400802out:
803 nfs_free_fattr(entry.fattr);
804 nfs_free_fhandle(entry.fh);
805 return status;
806}
807
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500808static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300810 put_page(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500814static void
815nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500817 unlock_page(desc->page);
818 nfs_readdir_page_put(desc);
819}
820
821static struct page *
822nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
823{
824 return nfs_readdir_page_get_locked(desc->file->f_mapping,
825 desc->page_index,
826 desc->last_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400830 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Trond Myklebust114de382020-02-02 17:53:54 -0500831 * and locks the page to prevent removal from the page cache.
Olivier Galibert00a92642005-06-22 17:16:29 +0000832 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400833static
Trond Myklebust114de382020-02-02 17:53:54 -0500834int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000835{
Dai Ngo227823d2020-01-22 20:45:39 -0500836 struct inode *inode = file_inode(desc->file);
837 struct nfs_inode *nfsi = NFS_I(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400838 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000839
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500840 desc->page = nfs_readdir_page_get_cached(desc);
841 if (!desc->page)
842 return -ENOMEM;
843 if (nfs_readdir_page_needs_filling(desc->page)) {
844 res = nfs_readdir_xdr_to_array(desc, desc->page, inode);
845 if (res < 0)
846 goto error;
Trond Myklebust114de382020-02-02 17:53:54 -0500847 }
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500848 res = nfs_readdir_search_array(desc);
849 if (res == 0) {
850 nfsi->page_index = desc->page_index;
851 return 0;
852 }
Trond Myklebust114de382020-02-02 17:53:54 -0500853error:
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500854 nfs_readdir_page_unlock_and_put_cached(desc);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400855 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000856}
857
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400858/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static inline
860int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
861{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500862 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000863
Trond Myklebust0aded702010-11-30 21:56:32 -0500864 if (desc->page_index == 0) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500865 desc->current_index = 0;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500866 desc->prev_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500867 desc->last_cookie = 0;
868 }
Trond Myklebust47c716c2010-12-07 12:44:56 -0500869 do {
Trond Myklebust114de382020-02-02 17:53:54 -0500870 res = find_and_lock_cache_page(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500871 } while (res == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return res;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * Once we've found the start of the dirent within a page: fill 'er up...
877 */
878static
Al Viro23db8622013-05-17 16:34:50 -0400879int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400882 int i = 0;
883 int res = 0;
884 struct nfs_cache_array *array = NULL;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400885
Fabian Frederick0795bf82017-05-03 20:52:21 +0200886 array = kmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400887 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500888 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Trond Myklebustece0b422010-11-20 13:55:33 -0500890 ent = &array->array[i];
Al Viro23db8622013-05-17 16:34:50 -0400891 if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
892 nfs_compat_user_ino64(ent->ino), ent->d_type)) {
Thomas Meyer6089dd02017-10-07 16:02:21 +0200893 desc->eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500895 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400896 if (i < (array->size-1))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500897 desc->dir_cookie = array->array[i+1].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400898 else
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500899 desc->dir_cookie = array->last_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500900 if (nfs_readdir_use_cookie(file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500901 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500902 else
903 desc->ctx->pos++;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500904 if (desc->duped != 0)
905 desc->duped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500907 if (array->page_is_eof)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200908 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400909
Fabian Frederick0795bf82017-05-03 20:52:21 +0200910 kunmap(desc->page);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500911 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500912 (unsigned long long)desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return res;
914}
915
916/*
917 * If we cannot find a cookie in our cache, we suspect that this is
918 * because it points to a deleted file, so we ask the server to return
919 * whatever it thinks is the next entry. We then feed this to filldir.
920 * If all goes well, we should then be able to find our way round the
921 * cache on the next call to readdir_search_pagecache();
922 *
923 * NOTE: we cannot add the anonymous page to the pagecache because
924 * the data it contains might not be page aligned. Besides,
925 * we should already have a complete representation of the
926 * directory in the page cache by the time we get here.
927 */
928static inline
Al Viro23db8622013-05-17 16:34:50 -0400929int uncached_readdir(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct page *page = NULL;
932 int status;
Al Viro496ad9a2013-01-23 17:07:38 -0500933 struct inode *inode = file_inode(desc->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500935 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500936 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 page = alloc_page(GFP_HIGHUSER);
939 if (!page) {
940 status = -ENOMEM;
941 goto out;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500944 desc->page_index = 0;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500945 desc->last_cookie = desc->dir_cookie;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500946 desc->page = page;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500947 desc->duped = 0;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500948
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500949 nfs_readdir_page_init_array(page, desc->dir_cookie);
Trond Myklebust85f86072010-11-20 13:24:49 -0500950 status = nfs_readdir_xdr_to_array(desc, page, inode);
951 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400952 goto out_release;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400953
Al Viro23db8622013-05-17 16:34:50 -0400954 status = nfs_do_filldir(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Trond Myklebust114de382020-02-02 17:53:54 -0500956 out_release:
957 nfs_readdir_clear_array(desc->page);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500958 nfs_readdir_page_put(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500960 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700961 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Olivier Galibert00a92642005-06-22 17:16:29 +0000965/* The file offset position represents the dirent entry number. A
966 last cookie cache takes care of the common case of reading the
967 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Al Viro23db8622013-05-17 16:34:50 -0400969static int nfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Miklos Szeredibe62a1a2016-03-26 16:14:39 -0400971 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +0000972 struct inode *inode = d_inode(dentry);
Al Viro23db8622013-05-17 16:34:50 -0400973 struct nfs_open_dir_context *dir_ctx = file->private_data;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500974 nfs_readdir_descriptor_t my_desc = {
975 .file = file,
976 .ctx = ctx,
Trond Myklebust59e356a2020-02-03 14:49:33 -0500977 .plus = nfs_use_readdirplus(inode, ctx),
978 },
979 *desc = &my_desc;
Scott Mayhew07b5ce82013-07-05 17:49:31 -0400980 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Al Viro6de14722013-09-16 10:53:17 -0400982 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
983 file, (long long)ctx->pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500984 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /*
Al Viro23db8622013-05-17 16:34:50 -0400987 * ctx->pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000988 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000989 * to either find the entry with the appropriate number or
990 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Trond Myklebust79f687a32016-11-19 10:54:55 -0500992 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
Scott Mayhew07b5ce82013-07-05 17:49:31 -0400993 res = nfs_revalidate_mapping(inode, file->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500994 if (res < 0)
995 goto out;
996
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500997 spin_lock(&file->f_lock);
998 desc->dir_cookie = dir_ctx->dir_cookie;
999 desc->dup_cookie = dir_ctx->dup_cookie;
1000 desc->duped = dir_ctx->duped;
1001 desc->attr_gencount = dir_ctx->attr_gencount;
1002 spin_unlock(&file->f_lock);
1003
Trond Myklebust47c716c2010-12-07 12:44:56 -05001004 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +00001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -05001008 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* This means either end of directory */
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001010 if (desc->dir_cookie && !desc->eof) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Or that the server has 'lost' a cookie */
Al Viro23db8622013-05-17 16:34:50 -04001012 res = uncached_readdir(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -05001013 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 continue;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 }
1018 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +02001019 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -04001021 desc->page_index = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001022 desc->plus = false;
1023 desc->eof = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 continue;
1025 }
1026 if (res < 0)
1027 break;
1028
Al Viro23db8622013-05-17 16:34:50 -04001029 res = nfs_do_filldir(desc);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -05001030 nfs_readdir_page_unlock_and_put_cached(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -05001031 if (res < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
Trond Myklebust47c716c2010-12-07 12:44:56 -05001033 } while (!desc->eof);
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001034
1035 spin_lock(&file->f_lock);
1036 dir_ctx->dir_cookie = desc->dir_cookie;
1037 dir_ctx->dup_cookie = desc->dup_cookie;
1038 dir_ctx->duped = desc->duped;
1039 dir_ctx->attr_gencount = desc->attr_gencount;
1040 spin_unlock(&file->f_lock);
1041
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001042out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001043 if (res > 0)
1044 res = 0;
Al Viro6de14722013-09-16 10:53:17 -04001045 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001046 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Andrew Morton965c8e52012-12-17 15:59:39 -08001049static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001050{
Bryan Schumaker480c2002011-03-23 14:48:29 -04001051 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Chuck Leverb84e06c2008-06-11 17:55:34 -04001052
Al Viro6de14722013-09-16 10:53:17 -04001053 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1054 filp, offset, whence);
Chuck Leverb84e06c2008-06-11 17:55:34 -04001055
Andrew Morton965c8e52012-12-17 15:59:39 -08001056 switch (whence) {
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001057 default:
1058 return -EINVAL;
1059 case SEEK_SET:
1060 if (offset < 0)
1061 return -EINVAL;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001062 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001063 break;
1064 case SEEK_CUR:
1065 if (offset == 0)
1066 return filp->f_pos;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001067 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001068 offset += filp->f_pos;
1069 if (offset < 0) {
Trond Myklebust83f2c452020-10-30 17:57:29 -04001070 spin_unlock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001071 return -EINVAL;
1072 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001073 }
1074 if (offset != filp->f_pos) {
1075 filp->f_pos = offset;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001076 if (nfs_readdir_use_cookie(filp))
1077 dir_ctx->dir_cookie = offset;
1078 else
1079 dir_ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -04001080 dir_ctx->duped = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001081 }
Trond Myklebust83f2c452020-10-30 17:57:29 -04001082 spin_unlock(&filp->f_lock);
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001083 return offset;
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/*
1087 * All directory operations under NFS are synchronous, so fsync()
1088 * is a dummy operation.
1089 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001090static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1091 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Al Viro6de14722013-09-16 10:53:17 -04001093 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001094
Trond Myklebust11decaf2020-10-30 17:57:30 -04001095 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001099/**
1100 * nfs_force_lookup_revalidate - Mark the directory as having changed
Trond Myklebust302fad72019-02-18 13:32:38 -05001101 * @dir: pointer to directory inode
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001102 *
1103 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1104 * full lookup on all child dentries of 'dir' whenever a change occurs
1105 * on the server that might have invalidated our dcache.
1106 *
Trond Myklebustefeda802020-02-05 09:01:54 -05001107 * Note that we reserve bit '0' as a tag to let us know when a dentry
1108 * was revalidated while holding a delegation on its inode.
1109 *
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001110 * The caller should be holding dir->i_lock
1111 */
1112void nfs_force_lookup_revalidate(struct inode *dir)
1113{
Trond Myklebustefeda802020-02-05 09:01:54 -05001114 NFS_I(dir)->cache_change_attribute += 2;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001115}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001116EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001117
Trond Myklebustefeda802020-02-05 09:01:54 -05001118/**
1119 * nfs_verify_change_attribute - Detects NFS remote directory changes
1120 * @dir: pointer to parent directory inode
1121 * @verf: previously saved change attribute
1122 *
1123 * Return "false" if the verifiers doesn't match the change attribute.
1124 * This would usually indicate that the directory contents have changed on
1125 * the server, and that any dentries need revalidating.
1126 */
1127static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1128{
1129 return (verf & ~1UL) == nfs_save_change_attribute(dir);
1130}
1131
1132static void nfs_set_verifier_delegated(unsigned long *verf)
1133{
1134 *verf |= 1UL;
1135}
1136
1137#if IS_ENABLED(CONFIG_NFS_V4)
1138static void nfs_unset_verifier_delegated(unsigned long *verf)
1139{
1140 *verf &= ~1UL;
1141}
1142#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1143
1144static bool nfs_test_verifier_delegated(unsigned long verf)
1145{
1146 return verf & 1;
1147}
1148
1149static bool nfs_verifier_is_delegated(struct dentry *dentry)
1150{
1151 return nfs_test_verifier_delegated(dentry->d_time);
1152}
1153
1154static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1155{
1156 struct inode *inode = d_inode(dentry);
1157
1158 if (!nfs_verifier_is_delegated(dentry) &&
1159 !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1160 goto out;
1161 if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1162 nfs_set_verifier_delegated(&verf);
1163out:
1164 dentry->d_time = verf;
1165}
1166
1167/**
1168 * nfs_set_verifier - save a parent directory verifier in the dentry
1169 * @dentry: pointer to dentry
1170 * @verf: verifier to save
1171 *
1172 * Saves the parent directory verifier in @dentry. If the inode has
1173 * a delegation, we also tag the dentry as having been revalidated
1174 * while holding a delegation so that we know we don't have to
1175 * look it up again after a directory change.
1176 */
1177void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1178{
1179
1180 spin_lock(&dentry->d_lock);
1181 nfs_set_verifier_locked(dentry, verf);
1182 spin_unlock(&dentry->d_lock);
1183}
1184EXPORT_SYMBOL_GPL(nfs_set_verifier);
1185
1186#if IS_ENABLED(CONFIG_NFS_V4)
1187/**
1188 * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1189 * @inode: pointer to inode
1190 *
1191 * Iterates through the dentries in the inode alias list and clears
1192 * the tag used to indicate that the dentry has been revalidated
1193 * while holding a delegation.
1194 * This function is intended for use when the delegation is being
1195 * returned or revoked.
1196 */
1197void nfs_clear_verifier_delegated(struct inode *inode)
1198{
1199 struct dentry *alias;
1200
1201 if (!inode)
1202 return;
1203 spin_lock(&inode->i_lock);
1204 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1205 spin_lock(&alias->d_lock);
1206 nfs_unset_verifier_delegated(&alias->d_time);
1207 spin_unlock(&alias->d_lock);
1208 }
1209 spin_unlock(&inode->i_lock);
1210}
1211EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1212#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/*
1215 * A check for whether or not the parent directory has changed.
1216 * In the case it has, we assume that the dentries are untrustworthy
1217 * and may need to be looked up again.
NeilBrown912a1082014-07-14 11:28:20 +10001218 * If rcu_walk prevents us from performing a full check, return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
NeilBrown912a1082014-07-14 11:28:20 +10001220static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1221 int rcu_walk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 if (IS_ROOT(dentry))
1224 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001225 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1226 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001227 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1228 return 0;
1229 /* Revalidate nfsi->cache_change_attribute before we declare a match */
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001230 if (nfs_mapping_need_revalidate_inode(dir)) {
1231 if (rcu_walk)
1232 return 0;
1233 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1234 return 0;
1235 }
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001236 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1237 return 0;
1238 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
Trond Myklebusta12802c2007-10-02 19:13:04 -04001242 * Use intent information to check whether or not we're going to do
1243 * an O_EXCL create using this path component.
1244 */
Al Virofa3c56b2012-06-10 15:36:40 -04001245static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
Trond Myklebusta12802c2007-10-02 19:13:04 -04001246{
1247 if (NFS_PROTO(dir)->version == 2)
1248 return 0;
Al Virofa3c56b2012-06-10 15:36:40 -04001249 return flags & LOOKUP_EXCL;
Trond Myklebusta12802c2007-10-02 19:13:04 -04001250}
1251
1252/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001253 * Inode and filehandle revalidation for lookups.
1254 *
1255 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1256 * or if the intent information indicates that we're about to open this
1257 * particular file and the "nocto" mount flag is not set.
1258 *
1259 */
Trond Myklebust65a0c142012-12-14 17:51:40 -05001260static
Al Virofa3c56b2012-06-10 15:36:40 -04001261int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust65a0c142012-12-14 17:51:40 -05001264 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
David Howells36d43a42011-01-14 18:45:42 +00001266 if (IS_AUTOMOUNT(inode))
Trond Myklebust4e99a1f2008-03-06 12:34:59 -05001267 return 0;
Trond Myklebust47921922018-05-10 10:08:36 -04001268
1269 if (flags & LOOKUP_OPEN) {
1270 switch (inode->i_mode & S_IFMT) {
1271 case S_IFREG:
1272 /* A NFSv4 OPEN will revalidate later */
1273 if (server->caps & NFS_CAP_ATOMIC_OPEN)
1274 goto out;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001275 fallthrough;
Trond Myklebust47921922018-05-10 10:08:36 -04001276 case S_IFDIR:
1277 if (server->flags & NFS_MOUNT_NOCTO)
1278 break;
1279 /* NFS close-to-open cache consistency validation */
1280 goto out_force;
1281 }
1282 }
1283
Al Virofacc3532012-06-10 15:33:51 -04001284 /* VFS wants an on-the-wire revalidation */
Al Virofa3c56b2012-06-10 15:36:40 -04001285 if (flags & LOOKUP_REVAL)
Al Virofacc3532012-06-10 15:33:51 -04001286 goto out_force;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001287out:
Lance Sheltona61246c2018-07-16 13:05:36 -04001288 return (inode->i_nlink == 0) ? -ESTALE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289out_force:
NeilBrown1fa1e382014-07-14 11:28:20 +10001290 if (flags & LOOKUP_RCU)
1291 return -ECHILD;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001292 ret = __nfs_revalidate_inode(server, inode);
1293 if (ret != 0)
1294 return ret;
1295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298/*
1299 * We judge how long we want to trust negative
1300 * dentries by looking at the parent inode mtime.
1301 *
1302 * If parent mtime has changed, we revalidate, else we wait for a
1303 * period corresponding to the parent's attribute cache timeout value.
NeilBrown912a1082014-07-14 11:28:20 +10001304 *
1305 * If LOOKUP_RCU prevents us from performing a full check, return 1
1306 * suggesting a reval is needed.
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001307 *
1308 * Note that when creating a new file, or looking up a rename target,
1309 * then it shouldn't be necessary to revalidate a negative dentry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
1311static inline
1312int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
Al Virofa3c56b2012-06-10 15:36:40 -04001313 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001315 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001317 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1318 return 1;
NeilBrown912a1082014-07-14 11:28:20 +10001319 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001322static int
1323nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1324 struct inode *inode, int error)
1325{
1326 switch (error) {
1327 case 1:
1328 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
1329 __func__, dentry);
1330 return 1;
1331 case 0:
1332 nfs_mark_for_revalidate(dir);
1333 if (inode && S_ISDIR(inode->i_mode)) {
1334 /* Purge readdir caches. */
1335 nfs_zap_caches(inode);
1336 /*
1337 * We can't d_drop the root of a disconnected tree:
1338 * its d_hash is on the s_anon list and d_drop() would hide
1339 * it from shrink_dcache_for_unmount(), leading to busy
1340 * inodes on unmount and further oopses.
1341 */
1342 if (IS_ROOT(dentry))
1343 return 1;
1344 }
1345 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
1346 __func__, dentry);
1347 return 0;
1348 }
1349 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
1350 __func__, dentry, error);
1351 return error;
1352}
1353
1354static int
1355nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1356 unsigned int flags)
1357{
1358 int ret = 1;
1359 if (nfs_neg_need_reval(dir, dentry, flags)) {
1360 if (flags & LOOKUP_RCU)
1361 return -ECHILD;
1362 ret = 0;
1363 }
1364 return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1365}
1366
1367static int
1368nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1369 struct inode *inode)
1370{
1371 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1372 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1373}
1374
1375static int
1376nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
1377 struct inode *inode)
1378{
1379 struct nfs_fh *fhandle;
1380 struct nfs_fattr *fattr;
1381 struct nfs4_label *label;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001382 unsigned long dir_verifier;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001383 int ret;
1384
1385 ret = -ENOMEM;
1386 fhandle = nfs_alloc_fhandle();
1387 fattr = nfs_alloc_fattr();
1388 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1389 if (fhandle == NULL || fattr == NULL || IS_ERR(label))
1390 goto out;
1391
Trond Myklebusta1147b82020-02-05 09:01:52 -05001392 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001393 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001394 if (ret < 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001395 switch (ret) {
1396 case -ESTALE:
1397 case -ENOENT:
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001398 ret = 0;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001399 break;
1400 case -ETIMEDOUT:
1401 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1402 ret = 1;
1403 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001404 goto out;
1405 }
1406 ret = 0;
1407 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1408 goto out;
1409 if (nfs_refresh_inode(inode, fattr) < 0)
1410 goto out;
1411
1412 nfs_setsecurity(inode, fattr, label);
Trond Myklebusta1147b82020-02-05 09:01:52 -05001413 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001414
1415 /* set a readdirplus hint that we had a cache miss */
1416 nfs_force_use_readdirplus(dir);
1417 ret = 1;
1418out:
1419 nfs_free_fattr(fattr);
1420 nfs_free_fhandle(fhandle);
1421 nfs4_label_free(label);
1422 return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/*
1426 * This is called every time the dcache has a lookup hit,
1427 * and we should check whether we can really trust that
1428 * lookup.
1429 *
1430 * NOTE! The hit can be a negative hit too, don't assume
1431 * we have an inode!
1432 *
1433 * If the parent directory is seen to have changed, we throw out the
1434 * cached dentry and do a new lookup.
1435 */
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001436static int
1437nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1438 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Chuck Lever91d5b472006-03-20 13:44:14 -05001443 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
David Howells2b0143b2015-03-17 22:25:59 +00001444 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001446 if (!inode)
1447 return nfs_lookup_revalidate_negative(dir, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001450 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1451 __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 goto out_bad;
1453 }
1454
Trond Myklebustefeda802020-02-05 09:01:54 -05001455 if (nfs_verifier_is_delegated(dentry))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001456 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /* Force a full look up iff the parent directory has changed */
Trond Myklebust73dd6842018-05-10 10:13:09 -04001459 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
NeilBrown912a1082014-07-14 11:28:20 +10001460 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
NeilBrowncc896842017-07-05 12:22:20 +10001461 error = nfs_lookup_verify_inode(inode, flags);
1462 if (error) {
NeilBrowncc896842017-07-05 12:22:20 +10001463 if (error == -ESTALE)
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001464 nfs_zap_caches(dir);
1465 goto out_bad;
NeilBrown1fa1e382014-07-14 11:28:20 +10001466 }
Trond Myklebust63519fb2016-11-19 11:21:54 -05001467 nfs_advise_use_readdirplus(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto out_valid;
1469 }
1470
NeilBrown912a1082014-07-14 11:28:20 +10001471 if (flags & LOOKUP_RCU)
1472 return -ECHILD;
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (NFS_STALE(inode))
1475 goto out_bad;
1476
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001477 trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001478 error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001479 trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001480 return error;
1481out_valid:
1482 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1483out_bad:
1484 if (flags & LOOKUP_RCU)
1485 return -ECHILD;
1486 return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001489static int
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001490__nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1491 int (*reval)(struct inode *, struct dentry *, unsigned int))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001492{
1493 struct dentry *parent;
1494 struct inode *dir;
1495 int ret;
David Quigleyaa9c2662013-05-22 12:50:44 -04001496
NeilBrownd51ac1a2014-07-14 11:28:20 +10001497 if (flags & LOOKUP_RCU) {
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001498 parent = READ_ONCE(dentry->d_parent);
1499 dir = d_inode_rcu(parent);
1500 if (!dir)
1501 return -ECHILD;
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001502 ret = reval(dir, dentry, flags);
Mark Rutland6aa7de02017-10-23 14:07:29 -07001503 if (parent != READ_ONCE(dentry->d_parent))
NeilBrownd51ac1a2014-07-14 11:28:20 +10001504 return -ECHILD;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001505 } else {
1506 parent = dget_parent(dentry);
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001507 ret = reval(d_inode(parent), dentry, flags);
NeilBrownd51ac1a2014-07-14 11:28:20 +10001508 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001513static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1514{
1515 return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/*
David Howells2b0143b2015-03-17 22:25:59 +00001519 * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001520 * when we don't really care about the dentry name. This is called when a
1521 * pathwalk ends on a dentry that was not found via a normal lookup in the
1522 * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1523 *
1524 * In this situation, we just want to verify that the inode itself is OK
1525 * since the dentry might have changed on the server.
1526 */
1527static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1528{
David Howells2b0143b2015-03-17 22:25:59 +00001529 struct inode *inode = d_inode(dentry);
Trond Myklebust9cdd1d32016-12-16 18:04:47 -05001530 int error = 0;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001531
1532 /*
1533 * I believe we can only get a negative dentry here in the case of a
1534 * procfs-style symlink. Just assume it's correct for now, but we may
1535 * eventually need to do something more here.
1536 */
1537 if (!inode) {
Al Viro6de14722013-09-16 10:53:17 -04001538 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1539 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001540 return 1;
1541 }
1542
1543 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001544 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1545 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001546 return 0;
1547 }
1548
NeilBrownb6887412017-08-25 17:34:41 +10001549 error = nfs_lookup_verify_inode(inode, flags);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001550 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1551 __func__, inode->i_ino, error ? "invalid" : "valid");
1552 return !error;
1553}
1554
1555/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 * This is called from dput() when d_count is going to 0.
1557 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001558static int nfs_dentry_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Al Viro6de14722013-09-16 10:53:17 -04001560 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1561 dentry, dentry->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Trond Myklebust77f11192008-01-28 19:43:19 -05001563 /* Unhash any dentry with a stale inode */
David Howells2b0143b2015-03-17 22:25:59 +00001564 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
Trond Myklebust77f11192008-01-28 19:43:19 -05001565 return 1;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1568 /* Unhash it, so that ->d_iput() would be called */
1569 return 1;
1570 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001571 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* Unhash it, so that ancestors of killed async unlink
1573 * files will be cleaned up during umount */
1574 return 1;
1575 }
1576 return 0;
1577
1578}
1579
Trond Myklebust1f018452012-12-14 16:38:46 -05001580/* Ensure that we revalidate inode->i_nlink */
Trond Myklebust1b83d702008-06-11 15:44:04 -04001581static void nfs_drop_nlink(struct inode *inode)
1582{
1583 spin_lock(&inode->i_lock);
Trond Myklebust1f018452012-12-14 16:38:46 -05001584 /* drop the inode if we're reasonably sure this is the last link */
Trond Myklebust59a707b2018-04-08 18:11:18 -04001585 if (inode->i_nlink > 0)
1586 drop_nlink(inode);
1587 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust16e14372018-03-20 16:53:31 -04001588 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
1589 | NFS_INO_INVALID_CTIME
Trond Myklebust59a707b2018-04-08 18:11:18 -04001590 | NFS_INO_INVALID_OTHER
1591 | NFS_INO_REVAL_FORCED;
Trond Myklebust1b83d702008-06-11 15:44:04 -04001592 spin_unlock(&inode->i_lock);
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595/*
1596 * Called when the dentry loses inode.
1597 * We use it to clean up silly-renamed files.
1598 */
1599static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1600{
Neil Brown83672d32007-02-26 12:48:25 +11001601 if (S_ISDIR(inode->i_mode))
1602 /* drop any readdir cache as it could easily be old */
1603 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001606 nfs_complete_unlink(dentry, inode);
Trond Myklebust1f018452012-12-14 16:38:46 -05001607 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 iput(inode);
1610}
1611
Al Virob1942c52011-03-16 05:44:14 -04001612static void nfs_d_release(struct dentry *dentry)
1613{
1614 /* free cached devname value, if it survived that far */
1615 if (unlikely(dentry->d_fsdata)) {
1616 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1617 WARN_ON(1);
1618 else
1619 kfree(dentry->d_fsdata);
1620 }
1621}
1622
Al Virof786aa92009-02-20 05:51:22 +00001623const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 .d_revalidate = nfs_lookup_revalidate,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001625 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 .d_delete = nfs_dentry_delete,
1627 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001628 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001629 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630};
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001631EXPORT_SYMBOL_GPL(nfs_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Bryan Schumaker597d9282012-07-16 16:39:10 -04001633struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct dentry *res;
1636 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001637 struct nfs_fh *fhandle = NULL;
1638 struct nfs_fattr *fattr = NULL;
David Quigley1775fd32013-05-22 12:50:42 -04001639 struct nfs4_label *label = NULL;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001640 unsigned long dir_verifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Al Viro6de14722013-09-16 10:53:17 -04001643 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
Chuck Lever91d5b472006-03-20 13:44:14 -05001644 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Al Viro130f9ab2016-03-07 22:40:43 -05001646 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1647 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Trond Myklebustfd684072006-09-05 12:27:44 -04001649 /*
1650 * If we're doing an exclusive create, optimize away the lookup
1651 * but don't hash the dentry.
1652 */
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001653 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
Al Viro130f9ab2016-03-07 22:40:43 -05001654 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001656 res = ERR_PTR(-ENOMEM);
1657 fhandle = nfs_alloc_fhandle();
1658 fattr = nfs_alloc_fattr();
1659 if (fhandle == NULL || fattr == NULL)
1660 goto out;
1661
David Quigley14c43f72013-05-22 12:50:43 -04001662 label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
1663 if (IS_ERR(label))
1664 goto out;
1665
Trond Myklebusta1147b82020-02-05 09:01:52 -05001666 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001667 trace_nfs_lookup_enter(dir, dentry, flags);
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001668 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (error == -ENOENT)
1670 goto no_entry;
1671 if (error < 0) {
1672 res = ERR_PTR(error);
Al Virobf130912016-05-29 15:14:14 -04001673 goto out_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
David Quigley1775fd32013-05-22 12:50:42 -04001675 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
Namhyung Kimbf0c84f2010-12-28 17:02:46 +00001676 res = ERR_CAST(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001677 if (IS_ERR(res))
Al Virobf130912016-05-29 15:14:14 -04001678 goto out_label;
David Howells54ceac42006-08-22 20:06:13 -04001679
Trond Myklebust63519fb2016-11-19 11:21:54 -05001680 /* Notify readdir to use READDIRPLUS */
1681 nfs_force_use_readdirplus(dir);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683no_entry:
Al Viro41d28bc2014-10-12 22:24:21 -04001684 res = d_splice_alias(inode, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001685 if (res != NULL) {
1686 if (IS_ERR(res))
Al Virobf130912016-05-29 15:14:14 -04001687 goto out_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001689 }
Trond Myklebusta1147b82020-02-05 09:01:52 -05001690 nfs_set_verifier(dentry, dir_verifier);
Al Virobf130912016-05-29 15:14:14 -04001691out_label:
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001692 trace_nfs_lookup_exit(dir, dentry, flags, error);
David Quigley14c43f72013-05-22 12:50:43 -04001693 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001695 nfs_free_fattr(fattr);
1696 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return res;
1698}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001699EXPORT_SYMBOL_GPL(nfs_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001701#if IS_ENABLED(CONFIG_NFS_V4)
Al Viro0b728e12012-06-10 16:03:43 -04001702static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Al Virof786aa92009-02-20 05:51:22 +00001704const struct dentry_operations nfs4_dentry_operations = {
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001705 .d_revalidate = nfs4_lookup_revalidate,
NeilBrownb6887412017-08-25 17:34:41 +10001706 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 .d_delete = nfs_dentry_delete,
1708 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001709 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001710 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711};
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001712EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Al Viro8a5e9292011-06-25 19:15:54 -04001714static fmode_t flags_to_mode(int flags)
1715{
1716 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1717 if ((flags & O_ACCMODE) != O_WRONLY)
1718 res |= FMODE_READ;
1719 if ((flags & O_ACCMODE) != O_RDONLY)
1720 res |= FMODE_WRITE;
1721 return res;
1722}
1723
NeilBrown532d4de2016-10-13 15:26:47 +11001724static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001725{
NeilBrown532d4de2016-10-13 15:26:47 +11001726 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001727}
1728
1729static int do_open(struct inode *inode, struct file *filp)
1730{
David Howellsf1fe29b2013-09-27 11:20:03 +01001731 nfs_fscache_open_file(inode, filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001732 return 0;
1733}
1734
Al Virod9585272012-06-22 12:39:14 +04001735static int nfs_finish_open(struct nfs_open_context *ctx,
1736 struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -04001737 struct file *file, unsigned open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001738{
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001739 int err;
1740
Al Virobe12af32018-06-08 11:44:56 -04001741 err = finish_open(file, dentry, do_open);
Al Viro30d90492012-06-22 12:40:19 +04001742 if (err)
Al Virod9585272012-06-22 12:39:14 +04001743 goto out;
NeilBrowneaa2b822017-07-03 15:27:26 +10001744 if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1745 nfs_file_set_open_context(file, ctx);
1746 else
Trond Myklebust98214212019-08-09 12:15:07 -04001747 err = -EOPENSTALE;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001748out:
Al Virod9585272012-06-22 12:39:14 +04001749 return err;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001750}
1751
Bryan Schumaker73a79702012-07-16 16:39:12 -04001752int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1753 struct file *file, unsigned open_flags,
Al Viro44907d72018-06-08 13:32:02 -04001754 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Al Viroc94c0952016-07-05 09:49:21 -04001756 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001757 struct nfs_open_context *ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001758 struct dentry *res;
1759 struct iattr attr = { .ia_valid = ATTR_OPEN };
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001760 struct inode *inode;
Trond Myklebust1472b832013-08-20 11:59:41 -04001761 unsigned int lookup_flags = 0;
Al Viroc94c0952016-07-05 09:49:21 -04001762 bool switched = false;
Al Viro73a09dd2018-06-08 13:22:02 -04001763 int created = 0;
Trond Myklebust898f6352010-10-23 11:24:25 -04001764 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001766 /* Expect a negative dentry */
David Howells2b0143b2015-03-17 22:25:59 +00001767 BUG_ON(d_inode(dentry));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001768
Niels de Vos1e8968c2013-12-17 18:20:16 +01001769 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001770 dir->i_sb->s_id, dir->i_ino, dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001771
Jeff Layton9597c132013-08-02 11:39:32 -04001772 err = nfs_check_flags(open_flags);
1773 if (err)
1774 return err;
1775
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001776 /* NFS only supports OPEN on regular files */
1777 if ((open_flags & O_DIRECTORY)) {
Al Viro00699ad2016-07-05 09:44:53 -04001778 if (!d_in_lookup(dentry)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001779 /*
1780 * Hashed negative dentry with O_DIRECTORY: dentry was
1781 * revalidated and is fine, no need to perform lookup
1782 * again
1783 */
Al Virod9585272012-06-22 12:39:14 +04001784 return -ENOENT;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001785 }
Trond Myklebust1472b832013-08-20 11:59:41 -04001786 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001790 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
Al Virod9585272012-06-22 12:39:14 +04001791 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001793 if (open_flags & O_CREAT) {
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001794 struct nfs_server *server = NFS_SERVER(dir);
1795
1796 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1797 mode &= ~current_umask();
1798
Trond Myklebust536e43d2012-01-17 22:04:26 -05001799 attr.ia_valid |= ATTR_MODE;
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001800 attr.ia_mode = mode;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001801 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001802 if (open_flags & O_TRUNC) {
1803 attr.ia_valid |= ATTR_SIZE;
1804 attr.ia_size = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001805 }
1806
Al Viroc94c0952016-07-05 09:49:21 -04001807 if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1808 d_drop(dentry);
1809 switched = true;
1810 dentry = d_alloc_parallel(dentry->d_parent,
1811 &dentry->d_name, &wq);
1812 if (IS_ERR(dentry))
1813 return PTR_ERR(dentry);
1814 if (unlikely(!d_in_lookup(dentry)))
1815 return finish_no_open(file, dentry);
1816 }
1817
NeilBrown532d4de2016-10-13 15:26:47 +11001818 ctx = create_nfs_open_context(dentry, open_flags, file);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001819 err = PTR_ERR(ctx);
1820 if (IS_ERR(ctx))
Al Virod9585272012-06-22 12:39:14 +04001821 goto out;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001822
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001823 trace_nfs_atomic_open_enter(dir, ctx, open_flags);
Al Viro73a09dd2018-06-08 13:22:02 -04001824 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
1825 if (created)
1826 file->f_mode |= FMODE_CREATED;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001827 if (IS_ERR(inode)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001828 err = PTR_ERR(inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001829 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001830 put_nfs_open_context(ctx);
Al Virod20cb712016-06-20 13:14:36 -04001831 d_drop(dentry);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001832 switch (err) {
1833 case -ENOENT:
Peng Tao774d9512017-06-29 06:34:50 -07001834 d_splice_alias(NULL, dentry);
Trond Myklebust809fd142014-10-23 19:33:14 +03001835 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001836 break;
1837 case -EISDIR:
1838 case -ENOTDIR:
1839 goto no_open;
1840 case -ELOOP:
1841 if (!(open_flags & O_NOFOLLOW))
Trond Myklebust6f926b52005-10-18 14:20:18 -07001842 goto no_open;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* case -EINVAL: */
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001845 default:
1846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Al Virod9585272012-06-22 12:39:14 +04001848 goto out;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001849 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001850
Al Virob452a452018-06-08 13:06:28 -04001851 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001852 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001853 put_nfs_open_context(ctx);
Al Virod9585272012-06-22 12:39:14 +04001854out:
Al Viroc94c0952016-07-05 09:49:21 -04001855 if (unlikely(switched)) {
1856 d_lookup_done(dentry);
1857 dput(dentry);
1858 }
Al Virod9585272012-06-22 12:39:14 +04001859 return err;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861no_open:
Trond Myklebust1472b832013-08-20 11:59:41 -04001862 res = nfs_lookup(dir, dentry, lookup_flags);
Al Viroc94c0952016-07-05 09:49:21 -04001863 if (switched) {
1864 d_lookup_done(dentry);
1865 if (!res)
1866 res = dentry;
1867 else
1868 dput(dentry);
1869 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001870 if (IS_ERR(res))
Al Viroc94c0952016-07-05 09:49:21 -04001871 return PTR_ERR(res);
Al Viroe45198a2012-06-10 06:48:09 -04001872 return finish_no_open(file, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001874EXPORT_SYMBOL_GPL(nfs_atomic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001876static int
1877nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1878 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Nick Piggin657e94b2011-01-14 02:48:39 +00001880 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Al Virofa3c56b2012-06-10 15:36:40 -04001882 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001883 goto full_reval;
Miklos Szeredieda72af2012-06-05 15:10:21 +02001884 if (d_mountpoint(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001885 goto full_reval;
Trond Myklebust2b484292010-09-17 10:56:51 -04001886
David Howells2b0143b2015-03-17 22:25:59 +00001887 inode = d_inode(dentry);
Trond Myklebust2b484292010-09-17 10:56:51 -04001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* We can't create new files in nfs_open_revalidate(), so we
1890 * optimize away revalidation of negative dentries.
1891 */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001892 if (inode == NULL)
1893 goto full_reval;
NeilBrown49317a72014-07-14 11:28:20 +10001894
Trond Myklebustefeda802020-02-05 09:01:54 -05001895 if (nfs_verifier_is_delegated(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001896 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust216d5d062007-10-01 20:10:12 -04001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* NFS only supports OPEN on regular files */
1899 if (!S_ISREG(inode->i_mode))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001900 goto full_reval;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* We cannot do exclusive creation on a positive dentry */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001903 if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1904 goto reval_dentry;
1905
1906 /* Check if the directory changed */
1907 if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1908 goto reval_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001910 /* Let f_op->open() actually open (and revalidate) the file */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001911 return 1;
1912reval_dentry:
1913 if (flags & LOOKUP_RCU)
1914 return -ECHILD;
zhangliguang42f72cf2019-02-12 09:38:33 +08001915 return nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust536e43d2012-01-17 22:04:26 -05001916
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001917full_reval:
1918 return nfs_do_lookup_revalidate(dir, dentry, flags);
1919}
Trond Myklebust535918f2010-09-17 10:56:51 -04001920
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001921static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1922{
1923 return __nfs_lookup_revalidate(dentry, flags,
1924 nfs4_do_lookup_revalidate);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927#endif /* CONFIG_NFSV4 */
1928
Benjamin Coddington406cd912019-09-13 08:29:02 -04001929struct dentry *
1930nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04001931 struct nfs_fattr *fattr,
1932 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001934 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001935 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct inode *inode;
Al Virob0c6108e2018-05-16 10:55:01 -04001937 struct dentry *d;
Benjamin Coddington406cd912019-09-13 08:29:02 -04001938 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Trond Myklebustfab728e2007-09-29 17:41:33 -04001940 d_drop(dentry);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (fhandle->size == 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001943 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001945 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001947 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1949 struct nfs_server *server = NFS_SB(dentry->d_sb);
Trond Myklebusta841b542018-04-07 13:50:59 -04001950 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1951 fattr, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001953 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
David Quigley1775fd32013-05-22 12:50:42 -04001955 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
Al Virob0c6108e2018-05-16 10:55:01 -04001956 d = d_splice_alias(inode, dentry);
Trond Myklebustfab728e2007-09-29 17:41:33 -04001957out:
1958 dput(parent);
Benjamin Coddington406cd912019-09-13 08:29:02 -04001959 return d;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001960out_error:
1961 nfs_mark_for_revalidate(dir);
Benjamin Coddington406cd912019-09-13 08:29:02 -04001962 d = ERR_PTR(error);
1963 goto out;
1964}
1965EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1966
1967/*
1968 * Code common to create, mkdir, and mknod.
1969 */
1970int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1971 struct nfs_fattr *fattr,
1972 struct nfs4_label *label)
1973{
1974 struct dentry *d;
1975
1976 d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1977 if (IS_ERR(d))
1978 return PTR_ERR(d);
1979
1980 /* Callers don't care */
1981 dput(d);
1982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001984EXPORT_SYMBOL_GPL(nfs_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986/*
1987 * Following a failed create operation, we drop the dentry rather
1988 * than retain a negative dentry. This avoids a problem in the event
1989 * that the operation succeeded on the server, but an error in the
1990 * reply path made it appear to have failed.
1991 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04001992int nfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04001993 umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct iattr attr;
Al Viroebfc3b42012-06-10 18:05:36 -04001996 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Niels de Vos1e8968c2013-12-17 18:20:16 +01001999 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002000 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 attr.ia_mode = mode;
2003 attr.ia_valid = ATTR_MODE;
2004
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002005 trace_nfs_create_enter(dir, dentry, open_flags);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002006 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002007 trace_nfs_create_exit(dir, dentry, open_flags, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (error != 0)
2009 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 d_drop(dentry);
2013 return error;
2014}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002015EXPORT_SYMBOL_GPL(nfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017/*
2018 * See comments for nfs_proc_create regarding failed operations.
2019 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002020int
Al Viro1a67aaf2011-07-26 01:52:52 -04002021nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct iattr attr;
2024 int status;
2025
Niels de Vos1e8968c2013-12-17 18:20:16 +01002026 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002027 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 attr.ia_mode = mode;
2030 attr.ia_valid = ATTR_MODE;
2031
Trond Myklebust1ca42382013-08-21 12:36:04 -04002032 trace_nfs_mknod_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002034 trace_nfs_mknod_exit(dir, dentry, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (status != 0)
2036 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
2038out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 d_drop(dentry);
2040 return status;
2041}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002042EXPORT_SYMBOL_GPL(nfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044/*
2045 * See comments for nfs_proc_create regarding failed operations.
2046 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002047int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct iattr attr;
2050 int error;
2051
Niels de Vos1e8968c2013-12-17 18:20:16 +01002052 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002053 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 attr.ia_valid = ATTR_MODE;
2056 attr.ia_mode = mode | S_IFDIR;
2057
Trond Myklebust1ca42382013-08-21 12:36:04 -04002058 trace_nfs_mkdir_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002060 trace_nfs_mkdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (error != 0)
2062 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
2064out_err:
2065 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return error;
2067}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002068EXPORT_SYMBOL_GPL(nfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002070static void nfs_dentry_handle_enoent(struct dentry *dentry)
2071{
Al Virodc3f4192015-05-18 10:10:34 -04002072 if (simple_positive(dentry))
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002073 d_delete(dentry);
2074}
2075
Bryan Schumaker597d9282012-07-16 16:39:10 -04002076int nfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 int error;
2079
Niels de Vos1e8968c2013-12-17 18:20:16 +01002080 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002081 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Trond Myklebust1ca42382013-08-21 12:36:04 -04002083 trace_nfs_rmdir_enter(dir, dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002084 if (d_really_is_positive(dentry)) {
Al Viro884be172016-04-28 23:56:31 -04002085 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002086 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2087 /* Ensure the VFS deletes this inode */
2088 switch (error) {
2089 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00002090 clear_nlink(d_inode(dentry));
Trond Myklebustba6c0592013-08-30 12:24:25 -04002091 break;
2092 case -ENOENT:
2093 nfs_dentry_handle_enoent(dentry);
2094 }
Al Viro884be172016-04-28 23:56:31 -04002095 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002096 } else
2097 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002098 trace_nfs_rmdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 return error;
2101}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002102EXPORT_SYMBOL_GPL(nfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104/*
2105 * Remove a file after making sure there are no pending writes,
2106 * and after checking that the file has only one user.
2107 *
2108 * We invalidate the attribute cache and free the inode prior to the operation
2109 * to avoid possible races if the server reuses the inode.
2110 */
2111static int nfs_safe_remove(struct dentry *dentry)
2112{
David Howells2b0143b2015-03-17 22:25:59 +00002113 struct inode *dir = d_inode(dentry->d_parent);
2114 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 int error = -EBUSY;
2116
Al Viro6de14722013-09-16 10:53:17 -04002117 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 /* If the dentry was sillyrenamed, we simply call d_delete() */
2120 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2121 error = 0;
2122 goto out;
2123 }
2124
Trond Myklebust1ca42382013-08-21 12:36:04 -04002125 trace_nfs_remove_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (inode != NULL) {
Trond Myklebust912678d2018-03-20 16:43:15 -04002127 error = NFS_PROTO(dir)->remove(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04002129 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 } else
Trond Myklebust912678d2018-03-20 16:43:15 -04002131 error = NFS_PROTO(dir)->remove(dir, dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002132 if (error == -ENOENT)
2133 nfs_dentry_handle_enoent(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002134 trace_nfs_remove_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135out:
2136 return error;
2137}
2138
2139/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
2140 * belongs to an active ".nfs..." file and we return -EBUSY.
2141 *
2142 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
2143 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002144int nfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 int error;
2147 int need_rehash = 0;
2148
Niels de Vos1e8968c2013-12-17 18:20:16 +01002149 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002150 dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Trond Myklebust1ca42382013-08-21 12:36:04 -04002152 trace_nfs_unlink_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 spin_lock(&dentry->d_lock);
Al Viro84d08fa2013-07-05 18:59:33 +04002154 if (d_count(dentry) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 spin_unlock(&dentry->d_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05002156 /* Start asynchronous writeout of the inode */
David Howells2b0143b2015-03-17 22:25:59 +00002157 write_inode_now(d_inode(dentry), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 error = nfs_sillyrename(dir, dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161 if (!d_unhashed(dentry)) {
2162 __d_drop(dentry);
2163 need_rehash = 1;
2164 }
2165 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002167 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2169 } else if (need_rehash)
2170 d_rehash(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002171out:
2172 trace_nfs_unlink_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return error;
2174}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002175EXPORT_SYMBOL_GPL(nfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Chuck Lever873101b2006-08-22 20:06:23 -04002177/*
2178 * To create a symbolic link, most file systems instantiate a new inode,
2179 * add a page to it containing the path, then write it out to the disk
2180 * using prepare_write/commit_write.
2181 *
2182 * Unfortunately the NFS client can't create the in-core inode first
2183 * because it needs a file handle to create an in-core inode (see
2184 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
2185 * symlink request has completed on the server.
2186 *
2187 * So instead we allocate a raw page, copy the symname into it, then do
2188 * the SYMLINK request with the page as the buffer. If it succeeds, we
2189 * now have a new file handle and can instantiate an in-core NFS inode
2190 * and move the raw page into its mapping.
2191 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002192int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Chuck Lever873101b2006-08-22 20:06:23 -04002194 struct page *page;
2195 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04002197 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 int error;
2199
Niels de Vos1e8968c2013-12-17 18:20:16 +01002200 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002201 dir->i_ino, dentry, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Chuck Lever873101b2006-08-22 20:06:23 -04002203 if (pathlen > PAGE_SIZE)
2204 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Chuck Lever873101b2006-08-22 20:06:23 -04002206 attr.ia_mode = S_IFLNK | S_IRWXUGO;
2207 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Al Viroe8ecde22016-01-14 17:52:59 -05002209 page = alloc_page(GFP_USER);
Trond Myklebust76566992008-06-11 15:44:22 -04002210 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04002211 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04002212
Al Viroe8ecde22016-01-14 17:52:59 -05002213 kaddr = page_address(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002214 memcpy(kaddr, symname, pathlen);
2215 if (pathlen < PAGE_SIZE)
2216 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
Chuck Lever873101b2006-08-22 20:06:23 -04002217
Trond Myklebust1ca42382013-08-21 12:36:04 -04002218 trace_nfs_symlink_enter(dir, dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -04002219 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002220 trace_nfs_symlink_exit(dir, dentry, error);
Chuck Lever873101b2006-08-22 20:06:23 -04002221 if (error != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01002222 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
Chuck Lever873101b2006-08-22 20:06:23 -04002223 dir->i_sb->s_id, dir->i_ino,
Al Viro6de14722013-09-16 10:53:17 -04002224 dentry, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04002226 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002227 return error;
2228 }
2229
2230 /*
2231 * No big deal if we can't add this page to the page cache here.
2232 * READLINK will get the missing page from the server if needed.
2233 */
David Howells2b0143b2015-03-17 22:25:59 +00002234 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
Chuck Lever873101b2006-08-22 20:06:23 -04002235 GFP_KERNEL)) {
Chuck Lever873101b2006-08-22 20:06:23 -04002236 SetPageUptodate(page);
2237 unlock_page(page);
Rafael Aquinia0b54add2014-02-10 14:25:48 -08002238 /*
2239 * add_to_page_cache_lru() grabs an extra page refcount.
2240 * Drop it here to avoid leaking this page later.
2241 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002242 put_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002243 } else
2244 __free_page(page);
2245
Chuck Lever873101b2006-08-22 20:06:23 -04002246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002248EXPORT_SYMBOL_GPL(nfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Bryan Schumaker597d9282012-07-16 16:39:10 -04002250int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2252{
David Howells2b0143b2015-03-17 22:25:59 +00002253 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int error;
2255
Al Viro6de14722013-09-16 10:53:17 -04002256 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2257 old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Trond Myklebust1fd10852013-08-21 13:54:44 -04002259 trace_nfs_link_enter(inode, dir, dentry);
Trond Myklebust9697d232007-10-02 21:58:05 -04002260 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04002262 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04002263 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04002264 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04002265 }
Trond Myklebust1fd10852013-08-21 13:54:44 -04002266 trace_nfs_link_exit(inode, dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return error;
2268}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002269EXPORT_SYMBOL_GPL(nfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271/*
2272 * RENAME
2273 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2274 * different file handle for the same inode after a rename (e.g. when
2275 * moving to a different directory). A fail-safe method to do so would
2276 * be to look up old_dir/old_name, create a link to new_dir/new_name and
2277 * rename the old file using the sillyrename stuff. This way, the original
2278 * file in old_dir will go away when the last process iput()s the inode.
2279 *
2280 * FIXED.
2281 *
2282 * It actually works quite well. One needs to have the possibility for
2283 * at least one ".nfs..." file in each directory the file ever gets
2284 * moved or linked to which happens automagically with the new
2285 * implementation that only depends on the dcache stuff instead of
2286 * using the inode layer
2287 *
2288 * Unfortunately, things are a little more complicated than indicated
2289 * above. For a cross-directory move, we want to make sure we can get
2290 * rid of the old inode after the operation. This means there must be
2291 * no pending writes (if it's a file), and the use count must be 1.
2292 * If these conditions are met, we can drop the dentries before doing
2293 * the rename.
2294 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002295int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002296 struct inode *new_dir, struct dentry *new_dentry,
2297 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
David Howells2b0143b2015-03-17 22:25:59 +00002299 struct inode *old_inode = d_inode(old_dentry);
2300 struct inode *new_inode = d_inode(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002301 struct dentry *dentry = NULL, *rehash = NULL;
Jeff Layton80a491f2014-03-17 07:06:56 -04002302 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 int error = -EBUSY;
2304
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002305 if (flags)
2306 return -EINVAL;
2307
Al Viro6de14722013-09-16 10:53:17 -04002308 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2309 old_dentry, new_dentry,
Al Viro84d08fa2013-07-05 18:59:33 +04002310 d_count(new_dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Trond Myklebust70ded202013-08-21 12:08:45 -04002312 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05002314 * For non-directories, check whether the target is busy and if so,
2315 * make a copy of the dentry and then do a silly-rename. If the
2316 * silly-rename succeeds, the copied dentry is hashed and becomes
2317 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
Miklos Szeredi27226102009-12-03 15:58:56 -05002319 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2320 /*
2321 * To prevent any new references to the target during the
2322 * rename, we unhash the dentry in advance.
2323 */
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002324 if (!d_unhashed(new_dentry)) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002325 d_drop(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002326 rehash = new_dentry;
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Al Viro84d08fa2013-07-05 18:59:33 +04002329 if (d_count(new_dentry) > 2) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002330 int err;
2331
2332 /* copy the target dentry's name */
2333 dentry = d_alloc(new_dentry->d_parent,
2334 &new_dentry->d_name);
2335 if (!dentry)
2336 goto out;
2337
2338 /* silly-rename the existing target ... */
2339 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05002340 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05002341 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002342
2343 new_dentry = dentry;
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002344 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002345 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05002346 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002349 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
Jeff Layton80a491f2014-03-17 07:06:56 -04002350 if (IS_ERR(task)) {
2351 error = PTR_ERR(task);
2352 goto out;
2353 }
2354
2355 error = rpc_wait_for_completion_task(task);
Benjamin Coddington818a8db2017-06-16 11:13:00 -04002356 if (error != 0) {
2357 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2358 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2359 smp_wmb();
2360 } else
Jeff Layton80a491f2014-03-17 07:06:56 -04002361 error = task->tk_status;
2362 rpc_put_task(task);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002363 /* Ensure the inode attributes are revalidated */
2364 if (error == 0) {
2365 spin_lock(&old_inode->i_lock);
2366 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2367 NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
2368 | NFS_INO_INVALID_CTIME
2369 | NFS_INO_REVAL_FORCED;
2370 spin_unlock(&old_inode->i_lock);
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372out:
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002373 if (rehash)
2374 d_rehash(rehash);
Trond Myklebust70ded202013-08-21 12:08:45 -04002375 trace_nfs_rename_exit(old_dir, old_dentry,
2376 new_dir, new_dentry, error);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002377 if (!error) {
2378 if (new_inode != NULL)
2379 nfs_drop_nlink(new_inode);
2380 /*
2381 * The d_move() should be here instead of in an async RPC completion
2382 * handler because we need the proper locks to move the dentry. If
2383 * we're interrupted by a signal, the async RPC completion handler
2384 * should mark the directories for revalidation.
2385 */
2386 d_move(old_dentry, new_dentry);
Trond Myklebustd8032242017-11-06 15:28:04 -05002387 nfs_set_verifier(old_dentry,
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002388 nfs_save_change_attribute(new_dir));
2389 } else if (error == -ENOENT)
2390 nfs_dentry_handle_enoent(old_dentry);
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /* new dentry created? */
2393 if (dentry)
2394 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return error;
2396}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002397EXPORT_SYMBOL_GPL(nfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002399static DEFINE_SPINLOCK(nfs_access_lru_lock);
2400static LIST_HEAD(nfs_access_lru_list);
2401static atomic_long_t nfs_access_nr_entries;
2402
Trond Myklebusta8b373e2020-02-08 09:14:11 -05002403static unsigned long nfs_access_max_cachesize = 4*1024*1024;
Trond Myklebust3a505842014-07-21 13:53:48 -04002404module_param(nfs_access_max_cachesize, ulong, 0644);
2405MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2406
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002407static void nfs_access_free_entry(struct nfs_access_entry *entry)
2408{
NeilBrownb68572e2018-12-03 11:30:30 +11002409 put_cred(entry->cred);
NeilBrownf682a392014-07-14 11:28:20 +10002410 kfree_rcu(entry, rcu_head);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002411 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002412 atomic_long_dec(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002413 smp_mb__after_atomic();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002414}
2415
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002416static void nfs_access_free_list(struct list_head *head)
2417{
2418 struct nfs_access_entry *cache;
2419
2420 while (!list_empty(head)) {
2421 cache = list_entry(head->next, struct nfs_access_entry, lru);
2422 list_del(&cache->lru);
2423 nfs_access_free_entry(cache);
2424 }
2425}
2426
Trond Myklebust3a505842014-07-21 13:53:48 -04002427static unsigned long
2428nfs_do_access_cache_scan(unsigned int nr_to_scan)
Trond Myklebust979df722006-07-25 11:28:19 -04002429{
2430 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002431 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04002432 struct nfs_access_entry *cache;
Dave Chinner1ab6c492013-08-28 10:18:09 +10002433 long freed = 0;
Trond Myklebust979df722006-07-25 11:28:19 -04002434
Trond Myklebusta50f7952007-06-05 19:23:43 -04002435 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002436 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04002437 struct inode *inode;
2438
2439 if (nr_to_scan-- == 0)
2440 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002441 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04002442 spin_lock(&inode->i_lock);
2443 if (list_empty(&nfsi->access_cache_entry_lru))
2444 goto remove_lru_entry;
2445 cache = list_entry(nfsi->access_cache_entry_lru.next,
2446 struct nfs_access_entry, lru);
2447 list_move(&cache->lru, &head);
2448 rb_erase(&cache->rb_node, &nfsi->access_cache);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002449 freed++;
Trond Myklebust979df722006-07-25 11:28:19 -04002450 if (!list_empty(&nfsi->access_cache_entry_lru))
2451 list_move_tail(&nfsi->access_cache_inode_lru,
2452 &nfs_access_lru_list);
2453 else {
2454remove_lru_entry:
2455 list_del_init(&nfsi->access_cache_inode_lru);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002456 smp_mb__before_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002457 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002458 smp_mb__after_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002459 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002460 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002461 }
2462 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002463 nfs_access_free_list(&head);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002464 return freed;
2465}
2466
2467unsigned long
Trond Myklebust3a505842014-07-21 13:53:48 -04002468nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2469{
2470 int nr_to_scan = sc->nr_to_scan;
2471 gfp_t gfp_mask = sc->gfp_mask;
2472
2473 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2474 return SHRINK_STOP;
2475 return nfs_do_access_cache_scan(nr_to_scan);
2476}
2477
2478
2479unsigned long
Dave Chinner1ab6c492013-08-28 10:18:09 +10002480nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2481{
Glauber Costa55f841c2013-08-28 10:17:53 +10002482 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
Trond Myklebust979df722006-07-25 11:28:19 -04002483}
2484
Trond Myklebust3a505842014-07-21 13:53:48 -04002485static void
2486nfs_access_cache_enforce_limit(void)
2487{
2488 long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2489 unsigned long diff;
2490 unsigned int nr_to_scan;
2491
2492 if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2493 return;
2494 nr_to_scan = 100;
2495 diff = nr_entries - nfs_access_max_cachesize;
2496 if (diff < nr_to_scan)
2497 nr_to_scan = diff;
2498 nfs_do_access_cache_scan(nr_to_scan);
2499}
2500
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002501static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002502{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002503 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002504 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002505 struct nfs_access_entry *entry;
2506
2507 /* Unhook entries from the cache */
2508 while ((n = rb_first(root_node)) != NULL) {
2509 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2510 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002511 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002512 }
2513 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002514}
2515
2516void nfs_access_zap_cache(struct inode *inode)
2517{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002518 LIST_HEAD(head);
2519
2520 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2521 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002522 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002523 spin_lock(&nfs_access_lru_lock);
2524 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002525 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002526
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002527 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002528 __nfs_access_zap_cache(NFS_I(inode), &head);
2529 spin_unlock(&inode->i_lock);
2530 spin_unlock(&nfs_access_lru_lock);
2531 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002532}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04002533EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002534
NeilBrownb68572e2018-12-03 11:30:30 +11002535static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002536{
2537 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002538
2539 while (n != NULL) {
NeilBrownb68572e2018-12-03 11:30:30 +11002540 struct nfs_access_entry *entry =
2541 rb_entry(n, struct nfs_access_entry, rb_node);
2542 int cmp = cred_fscmp(cred, entry->cred);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002543
NeilBrownb68572e2018-12-03 11:30:30 +11002544 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002545 n = n->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002546 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002547 n = n->rb_right;
2548 else
2549 return entry;
2550 }
2551 return NULL;
2552}
2553
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002554static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Chuck Lever55296802005-08-18 11:24:09 -07002556 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002557 struct nfs_access_entry *cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002558 bool retry = true;
2559 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002561 spin_lock(&inode->i_lock);
Trond Myklebust57b69182016-06-03 17:07:19 -04002562 for(;;) {
2563 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2564 goto out_zap;
2565 cache = nfs_access_search_rbtree(inode, cred);
2566 err = -ENOENT;
2567 if (cache == NULL)
2568 goto out;
2569 /* Found an entry, is our attribute cache valid? */
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002570 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
Trond Myklebust57b69182016-06-03 17:07:19 -04002571 break;
Trond Myklebust5c965db2020-01-06 15:39:36 -05002572 if (!retry)
2573 break;
Trond Myklebust57b69182016-06-03 17:07:19 -04002574 err = -ECHILD;
2575 if (!may_block)
2576 goto out;
Trond Myklebust57b69182016-06-03 17:07:19 -04002577 spin_unlock(&inode->i_lock);
2578 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2579 if (err)
2580 return err;
2581 spin_lock(&inode->i_lock);
2582 retry = false;
2583 }
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002584 res->cred = cache->cred;
2585 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002586 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002587 err = 0;
2588out:
2589 spin_unlock(&inode->i_lock);
2590 return err;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002591out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002592 spin_unlock(&inode->i_lock);
2593 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002594 return -ENOENT;
2595}
2596
NeilBrownb68572e2018-12-03 11:30:30 +11002597static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
NeilBrownf682a392014-07-14 11:28:20 +10002598{
2599 /* Only check the most recently returned cache entry,
2600 * but do it without locking.
2601 */
2602 struct nfs_inode *nfsi = NFS_I(inode);
2603 struct nfs_access_entry *cache;
2604 int err = -ECHILD;
2605 struct list_head *lh;
2606
2607 rcu_read_lock();
2608 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2609 goto out;
Madhuparna Bhowmik9f01eb52019-12-10 11:16:39 +05302610 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
NeilBrownf682a392014-07-14 11:28:20 +10002611 cache = list_entry(lh, struct nfs_access_entry, lru);
2612 if (lh == &nfsi->access_cache_entry_lru ||
Trond Myklebust9a206de2020-01-26 17:31:14 -05002613 cred_fscmp(cred, cache->cred) != 0)
NeilBrownf682a392014-07-14 11:28:20 +10002614 cache = NULL;
2615 if (cache == NULL)
2616 goto out;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002617 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
NeilBrownf682a392014-07-14 11:28:20 +10002618 goto out;
NeilBrownf682a392014-07-14 11:28:20 +10002619 res->cred = cache->cred;
2620 res->mask = cache->mask;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002621 err = 0;
NeilBrownf682a392014-07-14 11:28:20 +10002622out:
2623 rcu_read_unlock();
2624 return err;
2625}
2626
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002627int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2628nfs_access_entry *res, bool may_block)
2629{
2630 int status;
2631
2632 status = nfs_access_get_cached_rcu(inode, cred, res);
2633 if (status != 0)
2634 status = nfs_access_get_cached_locked(inode, cred, res,
2635 may_block);
2636
2637 return status;
2638}
2639EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2640
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002641static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2642{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002643 struct nfs_inode *nfsi = NFS_I(inode);
2644 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002645 struct rb_node **p = &root_node->rb_node;
2646 struct rb_node *parent = NULL;
2647 struct nfs_access_entry *entry;
NeilBrownb68572e2018-12-03 11:30:30 +11002648 int cmp;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002649
2650 spin_lock(&inode->i_lock);
2651 while (*p != NULL) {
2652 parent = *p;
2653 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
NeilBrownb68572e2018-12-03 11:30:30 +11002654 cmp = cred_fscmp(set->cred, entry->cred);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002655
NeilBrownb68572e2018-12-03 11:30:30 +11002656 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002657 p = &parent->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002658 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002659 p = &parent->rb_right;
2660 else
2661 goto found;
2662 }
2663 rb_link_node(&set->rb_node, parent, p);
2664 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002665 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002666 spin_unlock(&inode->i_lock);
2667 return;
2668found:
2669 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002670 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2671 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002672 spin_unlock(&inode->i_lock);
2673 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002676void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002678 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2679 if (cache == NULL)
2680 return;
2681 RB_CLEAR_NODE(&cache->rb_node);
NeilBrownb68572e2018-12-03 11:30:30 +11002682 cache->cred = get_cred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002684
NeilBrownf682a392014-07-14 11:28:20 +10002685 /* The above field assignments must be visible
2686 * before this item appears on the lru. We cannot easily
2687 * use rcu_assign_pointer, so just force the memory barrier.
2688 */
2689 smp_wmb();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002690 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002691
2692 /* Update accounting */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002693 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002694 atomic_long_inc(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002695 smp_mb__after_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002696
2697 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002698 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002699 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002700 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2701 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2702 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002703 spin_unlock(&nfs_access_lru_lock);
2704 }
Trond Myklebust3a505842014-07-21 13:53:48 -04002705 nfs_access_cache_enforce_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002707EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2708
Anna Schumaker3c181822017-07-26 10:14:55 -04002709#define NFS_MAY_READ (NFS_ACCESS_READ)
2710#define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
2711 NFS_ACCESS_EXTEND | \
2712 NFS_ACCESS_DELETE)
2713#define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
2714 NFS_ACCESS_EXTEND)
Trond Myklebustecbb9032017-07-11 17:54:35 -04002715#define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
Anna Schumaker3c181822017-07-26 10:14:55 -04002716#define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
2717#define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002718static int
Trond Myklebustecbb9032017-07-11 17:54:35 -04002719nfs_access_calc_mask(u32 access_result, umode_t umode)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002720{
2721 int mask = 0;
2722
2723 if (access_result & NFS_MAY_READ)
2724 mask |= MAY_READ;
Trond Myklebustecbb9032017-07-11 17:54:35 -04002725 if (S_ISDIR(umode)) {
2726 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
2727 mask |= MAY_WRITE;
2728 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
2729 mask |= MAY_EXEC;
2730 } else if (S_ISREG(umode)) {
2731 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2732 mask |= MAY_WRITE;
2733 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
2734 mask |= MAY_EXEC;
2735 } else if (access_result & NFS_MAY_WRITE)
2736 mask |= MAY_WRITE;
Trond Myklebust15d4b732017-07-11 17:54:32 -04002737 return mask;
2738}
2739
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002740void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2741{
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002742 entry->mask = access_result;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002743}
2744EXPORT_SYMBOL_GPL(nfs_access_set_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
NeilBrownb68572e2018-12-03 11:30:30 +11002746static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
2748 struct nfs_access_entry cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002749 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
Trond Myklebuste8194b72020-01-06 15:25:12 -05002750 int cache_mask = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 int status;
2752
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002753 trace_nfs_access_enter(inode);
2754
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002755 status = nfs_access_get_cached(inode, cred, &cache, may_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (status == 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002757 goto out_cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
NeilBrownf3324a22014-07-14 11:28:20 +10002759 status = -ECHILD;
Trond Myklebust57b69182016-06-03 17:07:19 -04002760 if (!may_block)
NeilBrownf3324a22014-07-14 11:28:20 +10002761 goto out;
2762
Anna Schumaker1750d922017-07-26 12:00:21 -04002763 /*
2764 * Determine which access bits we want to ask for...
2765 */
2766 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
Frank van der Linden72832a22020-06-23 22:38:58 +00002767 if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
2768 cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
2769 NFS_ACCESS_XALIST;
2770 }
Anna Schumaker1750d922017-07-26 12:00:21 -04002771 if (S_ISDIR(inode->i_mode))
2772 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
2773 else
2774 cache.mask |= NFS_ACCESS_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 cache.cred = cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002777 if (status != 0) {
2778 if (status == -ESTALE) {
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002779 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002780 nfs_set_inode_stale(inode);
2781 else
2782 nfs_zap_caches(inode);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002783 }
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002784 goto out;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 nfs_access_add_cache(inode, &cache);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002787out_cached:
Trond Myklebustecbb9032017-07-11 17:54:35 -04002788 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002789 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002790 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791out:
Trond Myklebuste8194b72020-01-06 15:25:12 -05002792 trace_nfs_access_exit(inode, mask, cache_mask, status);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002793 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
Trond Myklebustaf22f942007-08-10 17:45:10 -04002796static int nfs_open_permission_mask(int openflags)
2797{
2798 int mask = 0;
2799
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002800 if (openflags & __FMODE_EXEC) {
2801 /* ONLY check exec rights */
2802 mask = MAY_EXEC;
2803 } else {
2804 if ((openflags & O_ACCMODE) != O_WRONLY)
2805 mask |= MAY_READ;
2806 if ((openflags & O_ACCMODE) != O_RDONLY)
2807 mask |= MAY_WRITE;
2808 }
2809
Trond Myklebustaf22f942007-08-10 17:45:10 -04002810 return mask;
2811}
2812
NeilBrownb68572e2018-12-03 11:30:30 +11002813int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
Trond Myklebustaf22f942007-08-10 17:45:10 -04002814{
2815 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2816}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002817EXPORT_SYMBOL_GPL(nfs_may_open);
Trond Myklebustaf22f942007-08-10 17:45:10 -04002818
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002819static int nfs_execute_ok(struct inode *inode, int mask)
2820{
2821 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002822 int ret = 0;
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002823
Trond Myklebust38258272018-07-24 14:27:11 -04002824 if (S_ISDIR(inode->i_mode))
2825 return 0;
Trond Myklebustcf834022018-07-20 16:19:35 -04002826 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002827 if (mask & MAY_NOT_BLOCK)
2828 return -ECHILD;
2829 ret = __nfs_revalidate_inode(server, inode);
2830 }
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002831 if (ret == 0 && !execute_ok(inode))
2832 ret = -EACCES;
2833 return ret;
2834}
2835
Al Viro10556cb22011-06-20 19:28:19 -04002836int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
NeilBrownb68572e2018-12-03 11:30:30 +11002838 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 int res = 0;
2840
Chuck Lever91d5b472006-03-20 13:44:14 -05002841 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2842
Al Viroe6305c42008-07-15 21:03:57 -04002843 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 goto out;
2845 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002846 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 goto force_lookup;
2848
2849 switch (inode->i_mode & S_IFMT) {
2850 case S_IFLNK:
2851 goto out;
2852 case S_IFREG:
Trond Myklebust762674f2015-12-26 21:54:58 -05002853 if ((mask & MAY_OPEN) &&
2854 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 break;
2857 case S_IFDIR:
2858 /*
2859 * Optimize away all write operations, since the server
2860 * will check permissions when we perform the op.
2861 */
2862 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2863 goto out;
2864 }
2865
2866force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if (!NFS_PROTO(inode)->access)
2868 goto out_notsup;
2869
Zhouyi Zhoueb095c12020-03-06 03:45:26 +00002870 res = nfs_do_access(inode, cred, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871out:
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002872 if (!res && (mask & MAY_EXEC))
2873 res = nfs_execute_ok(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +02002874
Niels de Vos1e8968c2013-12-17 18:20:16 +01002875 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002876 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return res;
2878out_notsup:
NeilBrownd51ac1a2014-07-14 11:28:20 +10002879 if (mask & MAY_NOT_BLOCK)
2880 return -ECHILD;
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2883 if (res == 0)
Al Viro2830ba72011-06-20 19:16:29 -04002884 res = generic_permission(inode, mask);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002887EXPORT_SYMBOL_GPL(nfs_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889/*
2890 * Local variables:
2891 * version-control: t
2892 * kept-new-versions: 5
2893 * End:
2894 */