blob: f7248145c333e05089b5af28cdf5e1b715c29d5a [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
Fabian Frederick0795bf82017-05-03 20:52:21 +0200450 array = kmap(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 }
Fabian Frederick0795bf82017-05-03 20:52:21 +0200462 kunmap(desc->page);
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;
762 struct nfs_cache_array *array;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500763 int status = -ENOMEM;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400764 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400765
766 entry.prev_cookie = 0;
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500767 entry.cookie = nfs_readdir_page_last_cookie(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400768 entry.eof = 0;
769 entry.fh = nfs_alloc_fhandle();
770 entry.fattr = nfs_alloc_fattr();
Chuck Lever573c4e12010-12-14 14:58:11 +0000771 entry.server = NFS_SERVER(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400772 if (entry.fh == NULL || entry.fattr == NULL)
773 goto out;
774
David Quigley14c43f72013-05-22 12:50:43 -0400775 entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
776 if (IS_ERR(entry.label)) {
777 status = PTR_ERR(entry.label);
778 goto out;
779 }
780
Fabian Frederick0795bf82017-05-03 20:52:21 +0200781 array = kmap(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400782
Anna Schumakerc7e96682015-07-13 14:01:25 -0400783 status = nfs_readdir_alloc_pages(pages, array_size);
Trond Myklebust66502392011-01-08 17:45:38 -0500784 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400785 goto out_release_array;
786 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500787 unsigned int pglen;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400788 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400789
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400790 if (status < 0)
791 break;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500792
Trond Myklebustac396122010-11-15 20:26:22 -0500793 pglen = status;
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500794 if (pglen == 0) {
795 nfs_readdir_page_set_eof(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500796 break;
797 }
Trond Myklebust972bcdf2020-11-01 17:15:43 -0500798
799 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
800 } while (!status && !nfs_readdir_array_is_full(array));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400801
Anna Schumakerc7e96682015-07-13 14:01:25 -0400802 nfs_readdir_free_pages(pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400803out_release_array:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200804 kunmap(page);
David Quigley14c43f72013-05-22 12:50:43 -0400805 nfs4_label_free(entry.label);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400806out:
807 nfs_free_fattr(entry.fattr);
808 nfs_free_fhandle(entry.fh);
809 return status;
810}
811
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500812static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300814 put_page(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500818static void
819nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500821 unlock_page(desc->page);
822 nfs_readdir_page_put(desc);
823}
824
825static struct page *
826nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
827{
828 return nfs_readdir_page_get_locked(desc->file->f_mapping,
829 desc->page_index,
830 desc->last_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400834 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Trond Myklebust114de382020-02-02 17:53:54 -0500835 * and locks the page to prevent removal from the page cache.
Olivier Galibert00a92642005-06-22 17:16:29 +0000836 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400837static
Trond Myklebust114de382020-02-02 17:53:54 -0500838int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000839{
Dai Ngo227823d2020-01-22 20:45:39 -0500840 struct inode *inode = file_inode(desc->file);
841 struct nfs_inode *nfsi = NFS_I(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400842 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000843
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500844 desc->page = nfs_readdir_page_get_cached(desc);
845 if (!desc->page)
846 return -ENOMEM;
847 if (nfs_readdir_page_needs_filling(desc->page)) {
848 res = nfs_readdir_xdr_to_array(desc, desc->page, inode);
849 if (res < 0)
850 goto error;
Trond Myklebust114de382020-02-02 17:53:54 -0500851 }
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500852 res = nfs_readdir_search_array(desc);
853 if (res == 0) {
854 nfsi->page_index = desc->page_index;
855 return 0;
856 }
Trond Myklebust114de382020-02-02 17:53:54 -0500857error:
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500858 nfs_readdir_page_unlock_and_put_cached(desc);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400859 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000860}
861
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400862/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static inline
864int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
865{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500866 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000867
Trond Myklebust0aded702010-11-30 21:56:32 -0500868 if (desc->page_index == 0) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500869 desc->current_index = 0;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500870 desc->prev_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500871 desc->last_cookie = 0;
872 }
Trond Myklebust47c716c2010-12-07 12:44:56 -0500873 do {
Trond Myklebust114de382020-02-02 17:53:54 -0500874 res = find_and_lock_cache_page(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500875 } while (res == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return res;
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/*
880 * Once we've found the start of the dirent within a page: fill 'er up...
881 */
882static
Al Viro23db8622013-05-17 16:34:50 -0400883int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400886 int i = 0;
887 int res = 0;
888 struct nfs_cache_array *array = NULL;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400889
Fabian Frederick0795bf82017-05-03 20:52:21 +0200890 array = kmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400891 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500892 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Trond Myklebustece0b422010-11-20 13:55:33 -0500894 ent = &array->array[i];
Al Viro23db8622013-05-17 16:34:50 -0400895 if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
896 nfs_compat_user_ino64(ent->ino), ent->d_type)) {
Thomas Meyer6089dd02017-10-07 16:02:21 +0200897 desc->eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500899 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400900 if (i < (array->size-1))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500901 desc->dir_cookie = array->array[i+1].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400902 else
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500903 desc->dir_cookie = array->last_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500904 if (nfs_readdir_use_cookie(file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500905 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500906 else
907 desc->ctx->pos++;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500908 if (desc->duped != 0)
909 desc->duped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500911 if (array->page_is_eof)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200912 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400913
Fabian Frederick0795bf82017-05-03 20:52:21 +0200914 kunmap(desc->page);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500915 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500916 (unsigned long long)desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return res;
918}
919
920/*
921 * If we cannot find a cookie in our cache, we suspect that this is
922 * because it points to a deleted file, so we ask the server to return
923 * whatever it thinks is the next entry. We then feed this to filldir.
924 * If all goes well, we should then be able to find our way round the
925 * cache on the next call to readdir_search_pagecache();
926 *
927 * NOTE: we cannot add the anonymous page to the pagecache because
928 * the data it contains might not be page aligned. Besides,
929 * we should already have a complete representation of the
930 * directory in the page cache by the time we get here.
931 */
932static inline
Al Viro23db8622013-05-17 16:34:50 -0400933int uncached_readdir(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct page *page = NULL;
936 int status;
Al Viro496ad9a2013-01-23 17:07:38 -0500937 struct inode *inode = file_inode(desc->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500939 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500940 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 page = alloc_page(GFP_HIGHUSER);
943 if (!page) {
944 status = -ENOMEM;
945 goto out;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500948 desc->page_index = 0;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500949 desc->last_cookie = desc->dir_cookie;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500950 desc->page = page;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500951 desc->duped = 0;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500952
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500953 nfs_readdir_page_init_array(page, desc->dir_cookie);
Trond Myklebust85f86072010-11-20 13:24:49 -0500954 status = nfs_readdir_xdr_to_array(desc, page, inode);
955 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400956 goto out_release;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400957
Al Viro23db8622013-05-17 16:34:50 -0400958 status = nfs_do_filldir(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Trond Myklebust114de382020-02-02 17:53:54 -0500960 out_release:
961 nfs_readdir_clear_array(desc->page);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -0500962 nfs_readdir_page_put(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500964 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700965 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Olivier Galibert00a92642005-06-22 17:16:29 +0000969/* The file offset position represents the dirent entry number. A
970 last cookie cache takes care of the common case of reading the
971 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Al Viro23db8622013-05-17 16:34:50 -0400973static int nfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Miklos Szeredibe62a1a2016-03-26 16:14:39 -0400975 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +0000976 struct inode *inode = d_inode(dentry);
Al Viro23db8622013-05-17 16:34:50 -0400977 struct nfs_open_dir_context *dir_ctx = file->private_data;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500978 nfs_readdir_descriptor_t my_desc = {
979 .file = file,
980 .ctx = ctx,
Trond Myklebust59e356a2020-02-03 14:49:33 -0500981 .plus = nfs_use_readdirplus(inode, ctx),
982 },
983 *desc = &my_desc;
Scott Mayhew07b5ce82013-07-05 17:49:31 -0400984 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Al Viro6de14722013-09-16 10:53:17 -0400986 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
987 file, (long long)ctx->pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500988 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
Al Viro23db8622013-05-17 16:34:50 -0400991 * ctx->pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000992 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000993 * to either find the entry with the appropriate number or
994 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Trond Myklebust79f687a32016-11-19 10:54:55 -0500996 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
Scott Mayhew07b5ce82013-07-05 17:49:31 -0400997 res = nfs_revalidate_mapping(inode, file->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500998 if (res < 0)
999 goto out;
1000
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001001 spin_lock(&file->f_lock);
1002 desc->dir_cookie = dir_ctx->dir_cookie;
1003 desc->dup_cookie = dir_ctx->dup_cookie;
1004 desc->duped = dir_ctx->duped;
1005 desc->attr_gencount = dir_ctx->attr_gencount;
1006 spin_unlock(&file->f_lock);
1007
Trond Myklebust47c716c2010-12-07 12:44:56 -05001008 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +00001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -05001012 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* This means either end of directory */
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001014 if (desc->dir_cookie && !desc->eof) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* Or that the server has 'lost' a cookie */
Al Viro23db8622013-05-17 16:34:50 -04001016 res = uncached_readdir(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -05001017 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 continue;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021 }
1022 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +02001023 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -04001025 desc->page_index = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001026 desc->plus = false;
1027 desc->eof = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 continue;
1029 }
1030 if (res < 0)
1031 break;
1032
Al Viro23db8622013-05-17 16:34:50 -04001033 res = nfs_do_filldir(desc);
Trond Myklebust1f1d4aa42020-11-01 12:34:43 -05001034 nfs_readdir_page_unlock_and_put_cached(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -05001035 if (res < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
Trond Myklebust47c716c2010-12-07 12:44:56 -05001037 } while (!desc->eof);
Trond Myklebust2e7a4642020-11-01 09:56:18 -05001038
1039 spin_lock(&file->f_lock);
1040 dir_ctx->dir_cookie = desc->dir_cookie;
1041 dir_ctx->dup_cookie = desc->dup_cookie;
1042 dir_ctx->duped = desc->duped;
1043 dir_ctx->attr_gencount = desc->attr_gencount;
1044 spin_unlock(&file->f_lock);
1045
Trond Myklebustfccca7f2008-01-26 17:37:47 -05001046out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001047 if (res > 0)
1048 res = 0;
Al Viro6de14722013-09-16 10:53:17 -04001049 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001050 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Andrew Morton965c8e52012-12-17 15:59:39 -08001053static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001054{
Bryan Schumaker480c2002011-03-23 14:48:29 -04001055 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Chuck Leverb84e06c2008-06-11 17:55:34 -04001056
Al Viro6de14722013-09-16 10:53:17 -04001057 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1058 filp, offset, whence);
Chuck Leverb84e06c2008-06-11 17:55:34 -04001059
Andrew Morton965c8e52012-12-17 15:59:39 -08001060 switch (whence) {
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001061 default:
1062 return -EINVAL;
1063 case SEEK_SET:
1064 if (offset < 0)
1065 return -EINVAL;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001066 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001067 break;
1068 case SEEK_CUR:
1069 if (offset == 0)
1070 return filp->f_pos;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001071 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001072 offset += filp->f_pos;
1073 if (offset < 0) {
Trond Myklebust83f2c452020-10-30 17:57:29 -04001074 spin_unlock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001075 return -EINVAL;
1076 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001077 }
1078 if (offset != filp->f_pos) {
1079 filp->f_pos = offset;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001080 if (nfs_readdir_use_cookie(filp))
1081 dir_ctx->dir_cookie = offset;
1082 else
1083 dir_ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -04001084 dir_ctx->duped = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001085 }
Trond Myklebust83f2c452020-10-30 17:57:29 -04001086 spin_unlock(&filp->f_lock);
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001087 return offset;
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090/*
1091 * All directory operations under NFS are synchronous, so fsync()
1092 * is a dummy operation.
1093 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001094static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1095 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Al Viro6de14722013-09-16 10:53:17 -04001097 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001098
Trond Myklebust11decaf2020-10-30 17:57:30 -04001099 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return 0;
1101}
1102
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001103/**
1104 * nfs_force_lookup_revalidate - Mark the directory as having changed
Trond Myklebust302fad72019-02-18 13:32:38 -05001105 * @dir: pointer to directory inode
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001106 *
1107 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1108 * full lookup on all child dentries of 'dir' whenever a change occurs
1109 * on the server that might have invalidated our dcache.
1110 *
Trond Myklebustefeda802020-02-05 09:01:54 -05001111 * Note that we reserve bit '0' as a tag to let us know when a dentry
1112 * was revalidated while holding a delegation on its inode.
1113 *
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001114 * The caller should be holding dir->i_lock
1115 */
1116void nfs_force_lookup_revalidate(struct inode *dir)
1117{
Trond Myklebustefeda802020-02-05 09:01:54 -05001118 NFS_I(dir)->cache_change_attribute += 2;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001119}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001120EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001121
Trond Myklebustefeda802020-02-05 09:01:54 -05001122/**
1123 * nfs_verify_change_attribute - Detects NFS remote directory changes
1124 * @dir: pointer to parent directory inode
1125 * @verf: previously saved change attribute
1126 *
1127 * Return "false" if the verifiers doesn't match the change attribute.
1128 * This would usually indicate that the directory contents have changed on
1129 * the server, and that any dentries need revalidating.
1130 */
1131static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1132{
1133 return (verf & ~1UL) == nfs_save_change_attribute(dir);
1134}
1135
1136static void nfs_set_verifier_delegated(unsigned long *verf)
1137{
1138 *verf |= 1UL;
1139}
1140
1141#if IS_ENABLED(CONFIG_NFS_V4)
1142static void nfs_unset_verifier_delegated(unsigned long *verf)
1143{
1144 *verf &= ~1UL;
1145}
1146#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1147
1148static bool nfs_test_verifier_delegated(unsigned long verf)
1149{
1150 return verf & 1;
1151}
1152
1153static bool nfs_verifier_is_delegated(struct dentry *dentry)
1154{
1155 return nfs_test_verifier_delegated(dentry->d_time);
1156}
1157
1158static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1159{
1160 struct inode *inode = d_inode(dentry);
1161
1162 if (!nfs_verifier_is_delegated(dentry) &&
1163 !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1164 goto out;
1165 if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1166 nfs_set_verifier_delegated(&verf);
1167out:
1168 dentry->d_time = verf;
1169}
1170
1171/**
1172 * nfs_set_verifier - save a parent directory verifier in the dentry
1173 * @dentry: pointer to dentry
1174 * @verf: verifier to save
1175 *
1176 * Saves the parent directory verifier in @dentry. If the inode has
1177 * a delegation, we also tag the dentry as having been revalidated
1178 * while holding a delegation so that we know we don't have to
1179 * look it up again after a directory change.
1180 */
1181void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1182{
1183
1184 spin_lock(&dentry->d_lock);
1185 nfs_set_verifier_locked(dentry, verf);
1186 spin_unlock(&dentry->d_lock);
1187}
1188EXPORT_SYMBOL_GPL(nfs_set_verifier);
1189
1190#if IS_ENABLED(CONFIG_NFS_V4)
1191/**
1192 * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1193 * @inode: pointer to inode
1194 *
1195 * Iterates through the dentries in the inode alias list and clears
1196 * the tag used to indicate that the dentry has been revalidated
1197 * while holding a delegation.
1198 * This function is intended for use when the delegation is being
1199 * returned or revoked.
1200 */
1201void nfs_clear_verifier_delegated(struct inode *inode)
1202{
1203 struct dentry *alias;
1204
1205 if (!inode)
1206 return;
1207 spin_lock(&inode->i_lock);
1208 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1209 spin_lock(&alias->d_lock);
1210 nfs_unset_verifier_delegated(&alias->d_time);
1211 spin_unlock(&alias->d_lock);
1212 }
1213 spin_unlock(&inode->i_lock);
1214}
1215EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1216#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/*
1219 * A check for whether or not the parent directory has changed.
1220 * In the case it has, we assume that the dentries are untrustworthy
1221 * and may need to be looked up again.
NeilBrown912a1082014-07-14 11:28:20 +10001222 * If rcu_walk prevents us from performing a full check, return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
NeilBrown912a1082014-07-14 11:28:20 +10001224static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1225 int rcu_walk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 if (IS_ROOT(dentry))
1228 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001229 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1230 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001231 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1232 return 0;
1233 /* Revalidate nfsi->cache_change_attribute before we declare a match */
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001234 if (nfs_mapping_need_revalidate_inode(dir)) {
1235 if (rcu_walk)
1236 return 0;
1237 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1238 return 0;
1239 }
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001240 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1241 return 0;
1242 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/*
Trond Myklebusta12802c2007-10-02 19:13:04 -04001246 * Use intent information to check whether or not we're going to do
1247 * an O_EXCL create using this path component.
1248 */
Al Virofa3c56b2012-06-10 15:36:40 -04001249static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
Trond Myklebusta12802c2007-10-02 19:13:04 -04001250{
1251 if (NFS_PROTO(dir)->version == 2)
1252 return 0;
Al Virofa3c56b2012-06-10 15:36:40 -04001253 return flags & LOOKUP_EXCL;
Trond Myklebusta12802c2007-10-02 19:13:04 -04001254}
1255
1256/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001257 * Inode and filehandle revalidation for lookups.
1258 *
1259 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1260 * or if the intent information indicates that we're about to open this
1261 * particular file and the "nocto" mount flag is not set.
1262 *
1263 */
Trond Myklebust65a0c142012-12-14 17:51:40 -05001264static
Al Virofa3c56b2012-06-10 15:36:40 -04001265int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust65a0c142012-12-14 17:51:40 -05001268 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
David Howells36d43a42011-01-14 18:45:42 +00001270 if (IS_AUTOMOUNT(inode))
Trond Myklebust4e99a1f2008-03-06 12:34:59 -05001271 return 0;
Trond Myklebust47921922018-05-10 10:08:36 -04001272
1273 if (flags & LOOKUP_OPEN) {
1274 switch (inode->i_mode & S_IFMT) {
1275 case S_IFREG:
1276 /* A NFSv4 OPEN will revalidate later */
1277 if (server->caps & NFS_CAP_ATOMIC_OPEN)
1278 goto out;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001279 fallthrough;
Trond Myklebust47921922018-05-10 10:08:36 -04001280 case S_IFDIR:
1281 if (server->flags & NFS_MOUNT_NOCTO)
1282 break;
1283 /* NFS close-to-open cache consistency validation */
1284 goto out_force;
1285 }
1286 }
1287
Al Virofacc3532012-06-10 15:33:51 -04001288 /* VFS wants an on-the-wire revalidation */
Al Virofa3c56b2012-06-10 15:36:40 -04001289 if (flags & LOOKUP_REVAL)
Al Virofacc3532012-06-10 15:33:51 -04001290 goto out_force;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001291out:
Lance Sheltona61246c2018-07-16 13:05:36 -04001292 return (inode->i_nlink == 0) ? -ESTALE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293out_force:
NeilBrown1fa1e382014-07-14 11:28:20 +10001294 if (flags & LOOKUP_RCU)
1295 return -ECHILD;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001296 ret = __nfs_revalidate_inode(server, inode);
1297 if (ret != 0)
1298 return ret;
1299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302/*
1303 * We judge how long we want to trust negative
1304 * dentries by looking at the parent inode mtime.
1305 *
1306 * If parent mtime has changed, we revalidate, else we wait for a
1307 * period corresponding to the parent's attribute cache timeout value.
NeilBrown912a1082014-07-14 11:28:20 +10001308 *
1309 * If LOOKUP_RCU prevents us from performing a full check, return 1
1310 * suggesting a reval is needed.
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001311 *
1312 * Note that when creating a new file, or looking up a rename target,
1313 * then it shouldn't be necessary to revalidate a negative dentry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
1315static inline
1316int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
Al Virofa3c56b2012-06-10 15:36:40 -04001317 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001319 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001321 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1322 return 1;
NeilBrown912a1082014-07-14 11:28:20 +10001323 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001326static int
1327nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1328 struct inode *inode, int error)
1329{
1330 switch (error) {
1331 case 1:
1332 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
1333 __func__, dentry);
1334 return 1;
1335 case 0:
1336 nfs_mark_for_revalidate(dir);
1337 if (inode && S_ISDIR(inode->i_mode)) {
1338 /* Purge readdir caches. */
1339 nfs_zap_caches(inode);
1340 /*
1341 * We can't d_drop the root of a disconnected tree:
1342 * its d_hash is on the s_anon list and d_drop() would hide
1343 * it from shrink_dcache_for_unmount(), leading to busy
1344 * inodes on unmount and further oopses.
1345 */
1346 if (IS_ROOT(dentry))
1347 return 1;
1348 }
1349 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
1350 __func__, dentry);
1351 return 0;
1352 }
1353 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
1354 __func__, dentry, error);
1355 return error;
1356}
1357
1358static int
1359nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1360 unsigned int flags)
1361{
1362 int ret = 1;
1363 if (nfs_neg_need_reval(dir, dentry, flags)) {
1364 if (flags & LOOKUP_RCU)
1365 return -ECHILD;
1366 ret = 0;
1367 }
1368 return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1369}
1370
1371static int
1372nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1373 struct inode *inode)
1374{
1375 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1376 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1377}
1378
1379static int
1380nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
1381 struct inode *inode)
1382{
1383 struct nfs_fh *fhandle;
1384 struct nfs_fattr *fattr;
1385 struct nfs4_label *label;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001386 unsigned long dir_verifier;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001387 int ret;
1388
1389 ret = -ENOMEM;
1390 fhandle = nfs_alloc_fhandle();
1391 fattr = nfs_alloc_fattr();
1392 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1393 if (fhandle == NULL || fattr == NULL || IS_ERR(label))
1394 goto out;
1395
Trond Myklebusta1147b82020-02-05 09:01:52 -05001396 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001397 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001398 if (ret < 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001399 switch (ret) {
1400 case -ESTALE:
1401 case -ENOENT:
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001402 ret = 0;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001403 break;
1404 case -ETIMEDOUT:
1405 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1406 ret = 1;
1407 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001408 goto out;
1409 }
1410 ret = 0;
1411 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1412 goto out;
1413 if (nfs_refresh_inode(inode, fattr) < 0)
1414 goto out;
1415
1416 nfs_setsecurity(inode, fattr, label);
Trond Myklebusta1147b82020-02-05 09:01:52 -05001417 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001418
1419 /* set a readdirplus hint that we had a cache miss */
1420 nfs_force_use_readdirplus(dir);
1421 ret = 1;
1422out:
1423 nfs_free_fattr(fattr);
1424 nfs_free_fhandle(fhandle);
1425 nfs4_label_free(label);
1426 return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/*
1430 * This is called every time the dcache has a lookup hit,
1431 * and we should check whether we can really trust that
1432 * lookup.
1433 *
1434 * NOTE! The hit can be a negative hit too, don't assume
1435 * we have an inode!
1436 *
1437 * If the parent directory is seen to have changed, we throw out the
1438 * cached dentry and do a new lookup.
1439 */
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001440static int
1441nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1442 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Chuck Lever91d5b472006-03-20 13:44:14 -05001447 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
David Howells2b0143b2015-03-17 22:25:59 +00001448 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001450 if (!inode)
1451 return nfs_lookup_revalidate_negative(dir, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001454 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1455 __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto out_bad;
1457 }
1458
Trond Myklebustefeda802020-02-05 09:01:54 -05001459 if (nfs_verifier_is_delegated(dentry))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001460 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* Force a full look up iff the parent directory has changed */
Trond Myklebust73dd6842018-05-10 10:13:09 -04001463 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
NeilBrown912a1082014-07-14 11:28:20 +10001464 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
NeilBrowncc896842017-07-05 12:22:20 +10001465 error = nfs_lookup_verify_inode(inode, flags);
1466 if (error) {
NeilBrowncc896842017-07-05 12:22:20 +10001467 if (error == -ESTALE)
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001468 nfs_zap_caches(dir);
1469 goto out_bad;
NeilBrown1fa1e382014-07-14 11:28:20 +10001470 }
Trond Myklebust63519fb2016-11-19 11:21:54 -05001471 nfs_advise_use_readdirplus(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto out_valid;
1473 }
1474
NeilBrown912a1082014-07-14 11:28:20 +10001475 if (flags & LOOKUP_RCU)
1476 return -ECHILD;
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (NFS_STALE(inode))
1479 goto out_bad;
1480
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001481 trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001482 error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001483 trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001484 return error;
1485out_valid:
1486 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1487out_bad:
1488 if (flags & LOOKUP_RCU)
1489 return -ECHILD;
1490 return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001493static int
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001494__nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1495 int (*reval)(struct inode *, struct dentry *, unsigned int))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001496{
1497 struct dentry *parent;
1498 struct inode *dir;
1499 int ret;
David Quigleyaa9c2662013-05-22 12:50:44 -04001500
NeilBrownd51ac1a2014-07-14 11:28:20 +10001501 if (flags & LOOKUP_RCU) {
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001502 parent = READ_ONCE(dentry->d_parent);
1503 dir = d_inode_rcu(parent);
1504 if (!dir)
1505 return -ECHILD;
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001506 ret = reval(dir, dentry, flags);
Mark Rutland6aa7de02017-10-23 14:07:29 -07001507 if (parent != READ_ONCE(dentry->d_parent))
NeilBrownd51ac1a2014-07-14 11:28:20 +10001508 return -ECHILD;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001509 } else {
1510 parent = dget_parent(dentry);
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001511 ret = reval(d_inode(parent), dentry, flags);
NeilBrownd51ac1a2014-07-14 11:28:20 +10001512 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001514 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001517static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1518{
1519 return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
David Howells2b0143b2015-03-17 22:25:59 +00001523 * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001524 * when we don't really care about the dentry name. This is called when a
1525 * pathwalk ends on a dentry that was not found via a normal lookup in the
1526 * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1527 *
1528 * In this situation, we just want to verify that the inode itself is OK
1529 * since the dentry might have changed on the server.
1530 */
1531static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1532{
David Howells2b0143b2015-03-17 22:25:59 +00001533 struct inode *inode = d_inode(dentry);
Trond Myklebust9cdd1d32016-12-16 18:04:47 -05001534 int error = 0;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001535
1536 /*
1537 * I believe we can only get a negative dentry here in the case of a
1538 * procfs-style symlink. Just assume it's correct for now, but we may
1539 * eventually need to do something more here.
1540 */
1541 if (!inode) {
Al Viro6de14722013-09-16 10:53:17 -04001542 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1543 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001544 return 1;
1545 }
1546
1547 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001548 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1549 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001550 return 0;
1551 }
1552
NeilBrownb6887412017-08-25 17:34:41 +10001553 error = nfs_lookup_verify_inode(inode, flags);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001554 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1555 __func__, inode->i_ino, error ? "invalid" : "valid");
1556 return !error;
1557}
1558
1559/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * This is called from dput() when d_count is going to 0.
1561 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001562static int nfs_dentry_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Al Viro6de14722013-09-16 10:53:17 -04001564 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1565 dentry, dentry->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Trond Myklebust77f11192008-01-28 19:43:19 -05001567 /* Unhash any dentry with a stale inode */
David Howells2b0143b2015-03-17 22:25:59 +00001568 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
Trond Myklebust77f11192008-01-28 19:43:19 -05001569 return 1;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1572 /* Unhash it, so that ->d_iput() would be called */
1573 return 1;
1574 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001575 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /* Unhash it, so that ancestors of killed async unlink
1577 * files will be cleaned up during umount */
1578 return 1;
1579 }
1580 return 0;
1581
1582}
1583
Trond Myklebust1f018452012-12-14 16:38:46 -05001584/* Ensure that we revalidate inode->i_nlink */
Trond Myklebust1b83d702008-06-11 15:44:04 -04001585static void nfs_drop_nlink(struct inode *inode)
1586{
1587 spin_lock(&inode->i_lock);
Trond Myklebust1f018452012-12-14 16:38:46 -05001588 /* drop the inode if we're reasonably sure this is the last link */
Trond Myklebust59a707b2018-04-08 18:11:18 -04001589 if (inode->i_nlink > 0)
1590 drop_nlink(inode);
1591 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust16e14372018-03-20 16:53:31 -04001592 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
1593 | NFS_INO_INVALID_CTIME
Trond Myklebust59a707b2018-04-08 18:11:18 -04001594 | NFS_INO_INVALID_OTHER
1595 | NFS_INO_REVAL_FORCED;
Trond Myklebust1b83d702008-06-11 15:44:04 -04001596 spin_unlock(&inode->i_lock);
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/*
1600 * Called when the dentry loses inode.
1601 * We use it to clean up silly-renamed files.
1602 */
1603static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1604{
Neil Brown83672d32007-02-26 12:48:25 +11001605 if (S_ISDIR(inode->i_mode))
1606 /* drop any readdir cache as it could easily be old */
1607 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001610 nfs_complete_unlink(dentry, inode);
Trond Myklebust1f018452012-12-14 16:38:46 -05001611 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 iput(inode);
1614}
1615
Al Virob1942c52011-03-16 05:44:14 -04001616static void nfs_d_release(struct dentry *dentry)
1617{
1618 /* free cached devname value, if it survived that far */
1619 if (unlikely(dentry->d_fsdata)) {
1620 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1621 WARN_ON(1);
1622 else
1623 kfree(dentry->d_fsdata);
1624 }
1625}
1626
Al Virof786aa92009-02-20 05:51:22 +00001627const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 .d_revalidate = nfs_lookup_revalidate,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001629 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 .d_delete = nfs_dentry_delete,
1631 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001632 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001633 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634};
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001635EXPORT_SYMBOL_GPL(nfs_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Bryan Schumaker597d9282012-07-16 16:39:10 -04001637struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 struct dentry *res;
1640 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001641 struct nfs_fh *fhandle = NULL;
1642 struct nfs_fattr *fattr = NULL;
David Quigley1775fd32013-05-22 12:50:42 -04001643 struct nfs4_label *label = NULL;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001644 unsigned long dir_verifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Al Viro6de14722013-09-16 10:53:17 -04001647 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
Chuck Lever91d5b472006-03-20 13:44:14 -05001648 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Al Viro130f9ab2016-03-07 22:40:43 -05001650 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1651 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Trond Myklebustfd684072006-09-05 12:27:44 -04001653 /*
1654 * If we're doing an exclusive create, optimize away the lookup
1655 * but don't hash the dentry.
1656 */
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001657 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
Al Viro130f9ab2016-03-07 22:40:43 -05001658 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001660 res = ERR_PTR(-ENOMEM);
1661 fhandle = nfs_alloc_fhandle();
1662 fattr = nfs_alloc_fattr();
1663 if (fhandle == NULL || fattr == NULL)
1664 goto out;
1665
David Quigley14c43f72013-05-22 12:50:43 -04001666 label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
1667 if (IS_ERR(label))
1668 goto out;
1669
Trond Myklebusta1147b82020-02-05 09:01:52 -05001670 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001671 trace_nfs_lookup_enter(dir, dentry, flags);
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001672 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (error == -ENOENT)
1674 goto no_entry;
1675 if (error < 0) {
1676 res = ERR_PTR(error);
Al Virobf130912016-05-29 15:14:14 -04001677 goto out_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
David Quigley1775fd32013-05-22 12:50:42 -04001679 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
Namhyung Kimbf0c84f2010-12-28 17:02:46 +00001680 res = ERR_CAST(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001681 if (IS_ERR(res))
Al Virobf130912016-05-29 15:14:14 -04001682 goto out_label;
David Howells54ceac42006-08-22 20:06:13 -04001683
Trond Myklebust63519fb2016-11-19 11:21:54 -05001684 /* Notify readdir to use READDIRPLUS */
1685 nfs_force_use_readdirplus(dir);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687no_entry:
Al Viro41d28bc2014-10-12 22:24:21 -04001688 res = d_splice_alias(inode, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001689 if (res != NULL) {
1690 if (IS_ERR(res))
Al Virobf130912016-05-29 15:14:14 -04001691 goto out_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001693 }
Trond Myklebusta1147b82020-02-05 09:01:52 -05001694 nfs_set_verifier(dentry, dir_verifier);
Al Virobf130912016-05-29 15:14:14 -04001695out_label:
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001696 trace_nfs_lookup_exit(dir, dentry, flags, error);
David Quigley14c43f72013-05-22 12:50:43 -04001697 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001699 nfs_free_fattr(fattr);
1700 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return res;
1702}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001703EXPORT_SYMBOL_GPL(nfs_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001705#if IS_ENABLED(CONFIG_NFS_V4)
Al Viro0b728e12012-06-10 16:03:43 -04001706static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Al Virof786aa92009-02-20 05:51:22 +00001708const struct dentry_operations nfs4_dentry_operations = {
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001709 .d_revalidate = nfs4_lookup_revalidate,
NeilBrownb6887412017-08-25 17:34:41 +10001710 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 .d_delete = nfs_dentry_delete,
1712 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001713 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001714 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715};
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001716EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Al Viro8a5e9292011-06-25 19:15:54 -04001718static fmode_t flags_to_mode(int flags)
1719{
1720 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1721 if ((flags & O_ACCMODE) != O_WRONLY)
1722 res |= FMODE_READ;
1723 if ((flags & O_ACCMODE) != O_RDONLY)
1724 res |= FMODE_WRITE;
1725 return res;
1726}
1727
NeilBrown532d4de2016-10-13 15:26:47 +11001728static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001729{
NeilBrown532d4de2016-10-13 15:26:47 +11001730 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001731}
1732
1733static int do_open(struct inode *inode, struct file *filp)
1734{
David Howellsf1fe29b2013-09-27 11:20:03 +01001735 nfs_fscache_open_file(inode, filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001736 return 0;
1737}
1738
Al Virod9585272012-06-22 12:39:14 +04001739static int nfs_finish_open(struct nfs_open_context *ctx,
1740 struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -04001741 struct file *file, unsigned open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001742{
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001743 int err;
1744
Al Virobe12af32018-06-08 11:44:56 -04001745 err = finish_open(file, dentry, do_open);
Al Viro30d90492012-06-22 12:40:19 +04001746 if (err)
Al Virod9585272012-06-22 12:39:14 +04001747 goto out;
NeilBrowneaa2b822017-07-03 15:27:26 +10001748 if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1749 nfs_file_set_open_context(file, ctx);
1750 else
Trond Myklebust98214212019-08-09 12:15:07 -04001751 err = -EOPENSTALE;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001752out:
Al Virod9585272012-06-22 12:39:14 +04001753 return err;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001754}
1755
Bryan Schumaker73a79702012-07-16 16:39:12 -04001756int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1757 struct file *file, unsigned open_flags,
Al Viro44907d72018-06-08 13:32:02 -04001758 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Al Viroc94c0952016-07-05 09:49:21 -04001760 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001761 struct nfs_open_context *ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001762 struct dentry *res;
1763 struct iattr attr = { .ia_valid = ATTR_OPEN };
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001764 struct inode *inode;
Trond Myklebust1472b832013-08-20 11:59:41 -04001765 unsigned int lookup_flags = 0;
Al Viroc94c0952016-07-05 09:49:21 -04001766 bool switched = false;
Al Viro73a09dd2018-06-08 13:22:02 -04001767 int created = 0;
Trond Myklebust898f6352010-10-23 11:24:25 -04001768 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001770 /* Expect a negative dentry */
David Howells2b0143b2015-03-17 22:25:59 +00001771 BUG_ON(d_inode(dentry));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001772
Niels de Vos1e8968c2013-12-17 18:20:16 +01001773 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001774 dir->i_sb->s_id, dir->i_ino, dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001775
Jeff Layton9597c132013-08-02 11:39:32 -04001776 err = nfs_check_flags(open_flags);
1777 if (err)
1778 return err;
1779
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001780 /* NFS only supports OPEN on regular files */
1781 if ((open_flags & O_DIRECTORY)) {
Al Viro00699ad2016-07-05 09:44:53 -04001782 if (!d_in_lookup(dentry)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001783 /*
1784 * Hashed negative dentry with O_DIRECTORY: dentry was
1785 * revalidated and is fine, no need to perform lookup
1786 * again
1787 */
Al Virod9585272012-06-22 12:39:14 +04001788 return -ENOENT;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001789 }
Trond Myklebust1472b832013-08-20 11:59:41 -04001790 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001794 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
Al Virod9585272012-06-22 12:39:14 +04001795 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001797 if (open_flags & O_CREAT) {
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001798 struct nfs_server *server = NFS_SERVER(dir);
1799
1800 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1801 mode &= ~current_umask();
1802
Trond Myklebust536e43d2012-01-17 22:04:26 -05001803 attr.ia_valid |= ATTR_MODE;
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001804 attr.ia_mode = mode;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001805 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001806 if (open_flags & O_TRUNC) {
1807 attr.ia_valid |= ATTR_SIZE;
1808 attr.ia_size = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001809 }
1810
Al Viroc94c0952016-07-05 09:49:21 -04001811 if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1812 d_drop(dentry);
1813 switched = true;
1814 dentry = d_alloc_parallel(dentry->d_parent,
1815 &dentry->d_name, &wq);
1816 if (IS_ERR(dentry))
1817 return PTR_ERR(dentry);
1818 if (unlikely(!d_in_lookup(dentry)))
1819 return finish_no_open(file, dentry);
1820 }
1821
NeilBrown532d4de2016-10-13 15:26:47 +11001822 ctx = create_nfs_open_context(dentry, open_flags, file);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001823 err = PTR_ERR(ctx);
1824 if (IS_ERR(ctx))
Al Virod9585272012-06-22 12:39:14 +04001825 goto out;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001826
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001827 trace_nfs_atomic_open_enter(dir, ctx, open_flags);
Al Viro73a09dd2018-06-08 13:22:02 -04001828 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
1829 if (created)
1830 file->f_mode |= FMODE_CREATED;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001831 if (IS_ERR(inode)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001832 err = PTR_ERR(inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001833 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001834 put_nfs_open_context(ctx);
Al Virod20cb712016-06-20 13:14:36 -04001835 d_drop(dentry);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001836 switch (err) {
1837 case -ENOENT:
Peng Tao774d9512017-06-29 06:34:50 -07001838 d_splice_alias(NULL, dentry);
Trond Myklebust809fd142014-10-23 19:33:14 +03001839 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001840 break;
1841 case -EISDIR:
1842 case -ENOTDIR:
1843 goto no_open;
1844 case -ELOOP:
1845 if (!(open_flags & O_NOFOLLOW))
Trond Myklebust6f926b52005-10-18 14:20:18 -07001846 goto no_open;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 /* case -EINVAL: */
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001849 default:
1850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Al Virod9585272012-06-22 12:39:14 +04001852 goto out;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001853 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001854
Al Virob452a452018-06-08 13:06:28 -04001855 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001856 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001857 put_nfs_open_context(ctx);
Al Virod9585272012-06-22 12:39:14 +04001858out:
Al Viroc94c0952016-07-05 09:49:21 -04001859 if (unlikely(switched)) {
1860 d_lookup_done(dentry);
1861 dput(dentry);
1862 }
Al Virod9585272012-06-22 12:39:14 +04001863 return err;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865no_open:
Trond Myklebust1472b832013-08-20 11:59:41 -04001866 res = nfs_lookup(dir, dentry, lookup_flags);
Al Viroc94c0952016-07-05 09:49:21 -04001867 if (switched) {
1868 d_lookup_done(dentry);
1869 if (!res)
1870 res = dentry;
1871 else
1872 dput(dentry);
1873 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001874 if (IS_ERR(res))
Al Viroc94c0952016-07-05 09:49:21 -04001875 return PTR_ERR(res);
Al Viroe45198a2012-06-10 06:48:09 -04001876 return finish_no_open(file, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001878EXPORT_SYMBOL_GPL(nfs_atomic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001880static int
1881nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1882 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Nick Piggin657e94b2011-01-14 02:48:39 +00001884 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Al Virofa3c56b2012-06-10 15:36:40 -04001886 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001887 goto full_reval;
Miklos Szeredieda72af2012-06-05 15:10:21 +02001888 if (d_mountpoint(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001889 goto full_reval;
Trond Myklebust2b484292010-09-17 10:56:51 -04001890
David Howells2b0143b2015-03-17 22:25:59 +00001891 inode = d_inode(dentry);
Trond Myklebust2b484292010-09-17 10:56:51 -04001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* We can't create new files in nfs_open_revalidate(), so we
1894 * optimize away revalidation of negative dentries.
1895 */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001896 if (inode == NULL)
1897 goto full_reval;
NeilBrown49317a72014-07-14 11:28:20 +10001898
Trond Myklebustefeda802020-02-05 09:01:54 -05001899 if (nfs_verifier_is_delegated(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001900 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust216d5d062007-10-01 20:10:12 -04001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* NFS only supports OPEN on regular files */
1903 if (!S_ISREG(inode->i_mode))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001904 goto full_reval;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* We cannot do exclusive creation on a positive dentry */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001907 if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1908 goto reval_dentry;
1909
1910 /* Check if the directory changed */
1911 if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1912 goto reval_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001914 /* Let f_op->open() actually open (and revalidate) the file */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001915 return 1;
1916reval_dentry:
1917 if (flags & LOOKUP_RCU)
1918 return -ECHILD;
zhangliguang42f72cf2019-02-12 09:38:33 +08001919 return nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust536e43d2012-01-17 22:04:26 -05001920
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001921full_reval:
1922 return nfs_do_lookup_revalidate(dir, dentry, flags);
1923}
Trond Myklebust535918f2010-09-17 10:56:51 -04001924
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001925static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1926{
1927 return __nfs_lookup_revalidate(dentry, flags,
1928 nfs4_do_lookup_revalidate);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001929}
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931#endif /* CONFIG_NFSV4 */
1932
Benjamin Coddington406cd912019-09-13 08:29:02 -04001933struct dentry *
1934nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04001935 struct nfs_fattr *fattr,
1936 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001938 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001939 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct inode *inode;
Al Virob0c6108e2018-05-16 10:55:01 -04001941 struct dentry *d;
Benjamin Coddington406cd912019-09-13 08:29:02 -04001942 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Trond Myklebustfab728e2007-09-29 17:41:33 -04001944 d_drop(dentry);
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (fhandle->size == 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001947 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001949 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001951 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1953 struct nfs_server *server = NFS_SB(dentry->d_sb);
Trond Myklebusta841b542018-04-07 13:50:59 -04001954 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1955 fattr, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001957 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
David Quigley1775fd32013-05-22 12:50:42 -04001959 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
Al Virob0c6108e2018-05-16 10:55:01 -04001960 d = d_splice_alias(inode, dentry);
Trond Myklebustfab728e2007-09-29 17:41:33 -04001961out:
1962 dput(parent);
Benjamin Coddington406cd912019-09-13 08:29:02 -04001963 return d;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001964out_error:
1965 nfs_mark_for_revalidate(dir);
Benjamin Coddington406cd912019-09-13 08:29:02 -04001966 d = ERR_PTR(error);
1967 goto out;
1968}
1969EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1970
1971/*
1972 * Code common to create, mkdir, and mknod.
1973 */
1974int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1975 struct nfs_fattr *fattr,
1976 struct nfs4_label *label)
1977{
1978 struct dentry *d;
1979
1980 d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1981 if (IS_ERR(d))
1982 return PTR_ERR(d);
1983
1984 /* Callers don't care */
1985 dput(d);
1986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001988EXPORT_SYMBOL_GPL(nfs_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990/*
1991 * Following a failed create operation, we drop the dentry rather
1992 * than retain a negative dentry. This avoids a problem in the event
1993 * that the operation succeeded on the server, but an error in the
1994 * reply path made it appear to have failed.
1995 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04001996int nfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04001997 umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 struct iattr attr;
Al Viroebfc3b42012-06-10 18:05:36 -04002000 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Niels de Vos1e8968c2013-12-17 18:20:16 +01002003 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002004 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 attr.ia_mode = mode;
2007 attr.ia_valid = ATTR_MODE;
2008
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002009 trace_nfs_create_enter(dir, dentry, open_flags);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002010 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04002011 trace_nfs_create_exit(dir, dentry, open_flags, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (error != 0)
2013 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 d_drop(dentry);
2017 return error;
2018}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002019EXPORT_SYMBOL_GPL(nfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021/*
2022 * See comments for nfs_proc_create regarding failed operations.
2023 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002024int
Al Viro1a67aaf2011-07-26 01:52:52 -04002025nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct iattr attr;
2028 int status;
2029
Niels de Vos1e8968c2013-12-17 18:20:16 +01002030 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002031 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 attr.ia_mode = mode;
2034 attr.ia_valid = ATTR_MODE;
2035
Trond Myklebust1ca42382013-08-21 12:36:04 -04002036 trace_nfs_mknod_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002038 trace_nfs_mknod_exit(dir, dentry, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (status != 0)
2040 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return 0;
2042out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 d_drop(dentry);
2044 return status;
2045}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002046EXPORT_SYMBOL_GPL(nfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048/*
2049 * See comments for nfs_proc_create regarding failed operations.
2050 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002051int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 struct iattr attr;
2054 int error;
2055
Niels de Vos1e8968c2013-12-17 18:20:16 +01002056 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002057 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 attr.ia_valid = ATTR_MODE;
2060 attr.ia_mode = mode | S_IFDIR;
2061
Trond Myklebust1ca42382013-08-21 12:36:04 -04002062 trace_nfs_mkdir_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002064 trace_nfs_mkdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (error != 0)
2066 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
2068out_err:
2069 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return error;
2071}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002072EXPORT_SYMBOL_GPL(nfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002074static void nfs_dentry_handle_enoent(struct dentry *dentry)
2075{
Al Virodc3f4192015-05-18 10:10:34 -04002076 if (simple_positive(dentry))
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002077 d_delete(dentry);
2078}
2079
Bryan Schumaker597d9282012-07-16 16:39:10 -04002080int nfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 int error;
2083
Niels de Vos1e8968c2013-12-17 18:20:16 +01002084 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002085 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Trond Myklebust1ca42382013-08-21 12:36:04 -04002087 trace_nfs_rmdir_enter(dir, dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002088 if (d_really_is_positive(dentry)) {
Al Viro884be172016-04-28 23:56:31 -04002089 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002090 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2091 /* Ensure the VFS deletes this inode */
2092 switch (error) {
2093 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00002094 clear_nlink(d_inode(dentry));
Trond Myklebustba6c0592013-08-30 12:24:25 -04002095 break;
2096 case -ENOENT:
2097 nfs_dentry_handle_enoent(dentry);
2098 }
Al Viro884be172016-04-28 23:56:31 -04002099 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002100 } else
2101 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002102 trace_nfs_rmdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 return error;
2105}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002106EXPORT_SYMBOL_GPL(nfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/*
2109 * Remove a file after making sure there are no pending writes,
2110 * and after checking that the file has only one user.
2111 *
2112 * We invalidate the attribute cache and free the inode prior to the operation
2113 * to avoid possible races if the server reuses the inode.
2114 */
2115static int nfs_safe_remove(struct dentry *dentry)
2116{
David Howells2b0143b2015-03-17 22:25:59 +00002117 struct inode *dir = d_inode(dentry->d_parent);
2118 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int error = -EBUSY;
2120
Al Viro6de14722013-09-16 10:53:17 -04002121 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 /* If the dentry was sillyrenamed, we simply call d_delete() */
2124 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2125 error = 0;
2126 goto out;
2127 }
2128
Trond Myklebust1ca42382013-08-21 12:36:04 -04002129 trace_nfs_remove_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (inode != NULL) {
Trond Myklebust912678d2018-03-20 16:43:15 -04002131 error = NFS_PROTO(dir)->remove(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04002133 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 } else
Trond Myklebust912678d2018-03-20 16:43:15 -04002135 error = NFS_PROTO(dir)->remove(dir, dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002136 if (error == -ENOENT)
2137 nfs_dentry_handle_enoent(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002138 trace_nfs_remove_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139out:
2140 return error;
2141}
2142
2143/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
2144 * belongs to an active ".nfs..." file and we return -EBUSY.
2145 *
2146 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
2147 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002148int nfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 int error;
2151 int need_rehash = 0;
2152
Niels de Vos1e8968c2013-12-17 18:20:16 +01002153 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002154 dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Trond Myklebust1ca42382013-08-21 12:36:04 -04002156 trace_nfs_unlink_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 spin_lock(&dentry->d_lock);
Al Viro84d08fa2013-07-05 18:59:33 +04002158 if (d_count(dentry) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 spin_unlock(&dentry->d_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05002160 /* Start asynchronous writeout of the inode */
David Howells2b0143b2015-03-17 22:25:59 +00002161 write_inode_now(d_inode(dentry), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 error = nfs_sillyrename(dir, dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165 if (!d_unhashed(dentry)) {
2166 __d_drop(dentry);
2167 need_rehash = 1;
2168 }
2169 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002171 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2173 } else if (need_rehash)
2174 d_rehash(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002175out:
2176 trace_nfs_unlink_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return error;
2178}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002179EXPORT_SYMBOL_GPL(nfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Chuck Lever873101b2006-08-22 20:06:23 -04002181/*
2182 * To create a symbolic link, most file systems instantiate a new inode,
2183 * add a page to it containing the path, then write it out to the disk
2184 * using prepare_write/commit_write.
2185 *
2186 * Unfortunately the NFS client can't create the in-core inode first
2187 * because it needs a file handle to create an in-core inode (see
2188 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
2189 * symlink request has completed on the server.
2190 *
2191 * So instead we allocate a raw page, copy the symname into it, then do
2192 * the SYMLINK request with the page as the buffer. If it succeeds, we
2193 * now have a new file handle and can instantiate an in-core NFS inode
2194 * and move the raw page into its mapping.
2195 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002196int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Chuck Lever873101b2006-08-22 20:06:23 -04002198 struct page *page;
2199 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04002201 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 int error;
2203
Niels de Vos1e8968c2013-12-17 18:20:16 +01002204 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002205 dir->i_ino, dentry, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Chuck Lever873101b2006-08-22 20:06:23 -04002207 if (pathlen > PAGE_SIZE)
2208 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Chuck Lever873101b2006-08-22 20:06:23 -04002210 attr.ia_mode = S_IFLNK | S_IRWXUGO;
2211 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Al Viroe8ecde22016-01-14 17:52:59 -05002213 page = alloc_page(GFP_USER);
Trond Myklebust76566992008-06-11 15:44:22 -04002214 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04002215 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04002216
Al Viroe8ecde22016-01-14 17:52:59 -05002217 kaddr = page_address(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002218 memcpy(kaddr, symname, pathlen);
2219 if (pathlen < PAGE_SIZE)
2220 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
Chuck Lever873101b2006-08-22 20:06:23 -04002221
Trond Myklebust1ca42382013-08-21 12:36:04 -04002222 trace_nfs_symlink_enter(dir, dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -04002223 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002224 trace_nfs_symlink_exit(dir, dentry, error);
Chuck Lever873101b2006-08-22 20:06:23 -04002225 if (error != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01002226 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
Chuck Lever873101b2006-08-22 20:06:23 -04002227 dir->i_sb->s_id, dir->i_ino,
Al Viro6de14722013-09-16 10:53:17 -04002228 dentry, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04002230 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002231 return error;
2232 }
2233
2234 /*
2235 * No big deal if we can't add this page to the page cache here.
2236 * READLINK will get the missing page from the server if needed.
2237 */
David Howells2b0143b2015-03-17 22:25:59 +00002238 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
Chuck Lever873101b2006-08-22 20:06:23 -04002239 GFP_KERNEL)) {
Chuck Lever873101b2006-08-22 20:06:23 -04002240 SetPageUptodate(page);
2241 unlock_page(page);
Rafael Aquinia0b54add2014-02-10 14:25:48 -08002242 /*
2243 * add_to_page_cache_lru() grabs an extra page refcount.
2244 * Drop it here to avoid leaking this page later.
2245 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002246 put_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002247 } else
2248 __free_page(page);
2249
Chuck Lever873101b2006-08-22 20:06:23 -04002250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002252EXPORT_SYMBOL_GPL(nfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Bryan Schumaker597d9282012-07-16 16:39:10 -04002254int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2256{
David Howells2b0143b2015-03-17 22:25:59 +00002257 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 int error;
2259
Al Viro6de14722013-09-16 10:53:17 -04002260 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2261 old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Trond Myklebust1fd10852013-08-21 13:54:44 -04002263 trace_nfs_link_enter(inode, dir, dentry);
Trond Myklebust9697d232007-10-02 21:58:05 -04002264 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04002266 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04002267 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04002268 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04002269 }
Trond Myklebust1fd10852013-08-21 13:54:44 -04002270 trace_nfs_link_exit(inode, dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return error;
2272}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002273EXPORT_SYMBOL_GPL(nfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275/*
2276 * RENAME
2277 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2278 * different file handle for the same inode after a rename (e.g. when
2279 * moving to a different directory). A fail-safe method to do so would
2280 * be to look up old_dir/old_name, create a link to new_dir/new_name and
2281 * rename the old file using the sillyrename stuff. This way, the original
2282 * file in old_dir will go away when the last process iput()s the inode.
2283 *
2284 * FIXED.
2285 *
2286 * It actually works quite well. One needs to have the possibility for
2287 * at least one ".nfs..." file in each directory the file ever gets
2288 * moved or linked to which happens automagically with the new
2289 * implementation that only depends on the dcache stuff instead of
2290 * using the inode layer
2291 *
2292 * Unfortunately, things are a little more complicated than indicated
2293 * above. For a cross-directory move, we want to make sure we can get
2294 * rid of the old inode after the operation. This means there must be
2295 * no pending writes (if it's a file), and the use count must be 1.
2296 * If these conditions are met, we can drop the dentries before doing
2297 * the rename.
2298 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002299int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002300 struct inode *new_dir, struct dentry *new_dentry,
2301 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
David Howells2b0143b2015-03-17 22:25:59 +00002303 struct inode *old_inode = d_inode(old_dentry);
2304 struct inode *new_inode = d_inode(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002305 struct dentry *dentry = NULL, *rehash = NULL;
Jeff Layton80a491f2014-03-17 07:06:56 -04002306 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 int error = -EBUSY;
2308
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002309 if (flags)
2310 return -EINVAL;
2311
Al Viro6de14722013-09-16 10:53:17 -04002312 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2313 old_dentry, new_dentry,
Al Viro84d08fa2013-07-05 18:59:33 +04002314 d_count(new_dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Trond Myklebust70ded202013-08-21 12:08:45 -04002316 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05002318 * For non-directories, check whether the target is busy and if so,
2319 * make a copy of the dentry and then do a silly-rename. If the
2320 * silly-rename succeeds, the copied dentry is hashed and becomes
2321 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 */
Miklos Szeredi27226102009-12-03 15:58:56 -05002323 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2324 /*
2325 * To prevent any new references to the target during the
2326 * rename, we unhash the dentry in advance.
2327 */
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002328 if (!d_unhashed(new_dentry)) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002329 d_drop(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002330 rehash = new_dentry;
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Al Viro84d08fa2013-07-05 18:59:33 +04002333 if (d_count(new_dentry) > 2) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002334 int err;
2335
2336 /* copy the target dentry's name */
2337 dentry = d_alloc(new_dentry->d_parent,
2338 &new_dentry->d_name);
2339 if (!dentry)
2340 goto out;
2341
2342 /* silly-rename the existing target ... */
2343 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05002344 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05002345 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002346
2347 new_dentry = dentry;
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002348 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002349 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05002350 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002353 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
Jeff Layton80a491f2014-03-17 07:06:56 -04002354 if (IS_ERR(task)) {
2355 error = PTR_ERR(task);
2356 goto out;
2357 }
2358
2359 error = rpc_wait_for_completion_task(task);
Benjamin Coddington818a8db2017-06-16 11:13:00 -04002360 if (error != 0) {
2361 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2362 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2363 smp_wmb();
2364 } else
Jeff Layton80a491f2014-03-17 07:06:56 -04002365 error = task->tk_status;
2366 rpc_put_task(task);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002367 /* Ensure the inode attributes are revalidated */
2368 if (error == 0) {
2369 spin_lock(&old_inode->i_lock);
2370 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2371 NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
2372 | NFS_INO_INVALID_CTIME
2373 | NFS_INO_REVAL_FORCED;
2374 spin_unlock(&old_inode->i_lock);
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376out:
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002377 if (rehash)
2378 d_rehash(rehash);
Trond Myklebust70ded202013-08-21 12:08:45 -04002379 trace_nfs_rename_exit(old_dir, old_dentry,
2380 new_dir, new_dentry, error);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002381 if (!error) {
2382 if (new_inode != NULL)
2383 nfs_drop_nlink(new_inode);
2384 /*
2385 * The d_move() should be here instead of in an async RPC completion
2386 * handler because we need the proper locks to move the dentry. If
2387 * we're interrupted by a signal, the async RPC completion handler
2388 * should mark the directories for revalidation.
2389 */
2390 d_move(old_dentry, new_dentry);
Trond Myklebustd8032242017-11-06 15:28:04 -05002391 nfs_set_verifier(old_dentry,
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002392 nfs_save_change_attribute(new_dir));
2393 } else if (error == -ENOENT)
2394 nfs_dentry_handle_enoent(old_dentry);
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /* new dentry created? */
2397 if (dentry)
2398 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return error;
2400}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002401EXPORT_SYMBOL_GPL(nfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002403static DEFINE_SPINLOCK(nfs_access_lru_lock);
2404static LIST_HEAD(nfs_access_lru_list);
2405static atomic_long_t nfs_access_nr_entries;
2406
Trond Myklebusta8b373e2020-02-08 09:14:11 -05002407static unsigned long nfs_access_max_cachesize = 4*1024*1024;
Trond Myklebust3a505842014-07-21 13:53:48 -04002408module_param(nfs_access_max_cachesize, ulong, 0644);
2409MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2410
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002411static void nfs_access_free_entry(struct nfs_access_entry *entry)
2412{
NeilBrownb68572e2018-12-03 11:30:30 +11002413 put_cred(entry->cred);
NeilBrownf682a392014-07-14 11:28:20 +10002414 kfree_rcu(entry, rcu_head);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002415 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002416 atomic_long_dec(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002417 smp_mb__after_atomic();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002418}
2419
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002420static void nfs_access_free_list(struct list_head *head)
2421{
2422 struct nfs_access_entry *cache;
2423
2424 while (!list_empty(head)) {
2425 cache = list_entry(head->next, struct nfs_access_entry, lru);
2426 list_del(&cache->lru);
2427 nfs_access_free_entry(cache);
2428 }
2429}
2430
Trond Myklebust3a505842014-07-21 13:53:48 -04002431static unsigned long
2432nfs_do_access_cache_scan(unsigned int nr_to_scan)
Trond Myklebust979df722006-07-25 11:28:19 -04002433{
2434 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002435 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04002436 struct nfs_access_entry *cache;
Dave Chinner1ab6c492013-08-28 10:18:09 +10002437 long freed = 0;
Trond Myklebust979df722006-07-25 11:28:19 -04002438
Trond Myklebusta50f7952007-06-05 19:23:43 -04002439 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002440 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04002441 struct inode *inode;
2442
2443 if (nr_to_scan-- == 0)
2444 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002445 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04002446 spin_lock(&inode->i_lock);
2447 if (list_empty(&nfsi->access_cache_entry_lru))
2448 goto remove_lru_entry;
2449 cache = list_entry(nfsi->access_cache_entry_lru.next,
2450 struct nfs_access_entry, lru);
2451 list_move(&cache->lru, &head);
2452 rb_erase(&cache->rb_node, &nfsi->access_cache);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002453 freed++;
Trond Myklebust979df722006-07-25 11:28:19 -04002454 if (!list_empty(&nfsi->access_cache_entry_lru))
2455 list_move_tail(&nfsi->access_cache_inode_lru,
2456 &nfs_access_lru_list);
2457 else {
2458remove_lru_entry:
2459 list_del_init(&nfsi->access_cache_inode_lru);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002460 smp_mb__before_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002461 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002462 smp_mb__after_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002463 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002464 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002465 }
2466 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002467 nfs_access_free_list(&head);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002468 return freed;
2469}
2470
2471unsigned long
Trond Myklebust3a505842014-07-21 13:53:48 -04002472nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2473{
2474 int nr_to_scan = sc->nr_to_scan;
2475 gfp_t gfp_mask = sc->gfp_mask;
2476
2477 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2478 return SHRINK_STOP;
2479 return nfs_do_access_cache_scan(nr_to_scan);
2480}
2481
2482
2483unsigned long
Dave Chinner1ab6c492013-08-28 10:18:09 +10002484nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2485{
Glauber Costa55f841c2013-08-28 10:17:53 +10002486 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
Trond Myklebust979df722006-07-25 11:28:19 -04002487}
2488
Trond Myklebust3a505842014-07-21 13:53:48 -04002489static void
2490nfs_access_cache_enforce_limit(void)
2491{
2492 long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2493 unsigned long diff;
2494 unsigned int nr_to_scan;
2495
2496 if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2497 return;
2498 nr_to_scan = 100;
2499 diff = nr_entries - nfs_access_max_cachesize;
2500 if (diff < nr_to_scan)
2501 nr_to_scan = diff;
2502 nfs_do_access_cache_scan(nr_to_scan);
2503}
2504
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002505static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002506{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002507 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002508 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002509 struct nfs_access_entry *entry;
2510
2511 /* Unhook entries from the cache */
2512 while ((n = rb_first(root_node)) != NULL) {
2513 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2514 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002515 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002516 }
2517 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002518}
2519
2520void nfs_access_zap_cache(struct inode *inode)
2521{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002522 LIST_HEAD(head);
2523
2524 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2525 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002526 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002527 spin_lock(&nfs_access_lru_lock);
2528 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002529 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002530
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002531 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002532 __nfs_access_zap_cache(NFS_I(inode), &head);
2533 spin_unlock(&inode->i_lock);
2534 spin_unlock(&nfs_access_lru_lock);
2535 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002536}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04002537EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002538
NeilBrownb68572e2018-12-03 11:30:30 +11002539static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002540{
2541 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002542
2543 while (n != NULL) {
NeilBrownb68572e2018-12-03 11:30:30 +11002544 struct nfs_access_entry *entry =
2545 rb_entry(n, struct nfs_access_entry, rb_node);
2546 int cmp = cred_fscmp(cred, entry->cred);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002547
NeilBrownb68572e2018-12-03 11:30:30 +11002548 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002549 n = n->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002550 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002551 n = n->rb_right;
2552 else
2553 return entry;
2554 }
2555 return NULL;
2556}
2557
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002558static 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 -07002559{
Chuck Lever55296802005-08-18 11:24:09 -07002560 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002561 struct nfs_access_entry *cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002562 bool retry = true;
2563 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002565 spin_lock(&inode->i_lock);
Trond Myklebust57b69182016-06-03 17:07:19 -04002566 for(;;) {
2567 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2568 goto out_zap;
2569 cache = nfs_access_search_rbtree(inode, cred);
2570 err = -ENOENT;
2571 if (cache == NULL)
2572 goto out;
2573 /* Found an entry, is our attribute cache valid? */
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002574 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
Trond Myklebust57b69182016-06-03 17:07:19 -04002575 break;
Trond Myklebust5c965db2020-01-06 15:39:36 -05002576 if (!retry)
2577 break;
Trond Myklebust57b69182016-06-03 17:07:19 -04002578 err = -ECHILD;
2579 if (!may_block)
2580 goto out;
Trond Myklebust57b69182016-06-03 17:07:19 -04002581 spin_unlock(&inode->i_lock);
2582 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2583 if (err)
2584 return err;
2585 spin_lock(&inode->i_lock);
2586 retry = false;
2587 }
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002588 res->cred = cache->cred;
2589 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002590 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002591 err = 0;
2592out:
2593 spin_unlock(&inode->i_lock);
2594 return err;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002595out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002596 spin_unlock(&inode->i_lock);
2597 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002598 return -ENOENT;
2599}
2600
NeilBrownb68572e2018-12-03 11:30:30 +11002601static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
NeilBrownf682a392014-07-14 11:28:20 +10002602{
2603 /* Only check the most recently returned cache entry,
2604 * but do it without locking.
2605 */
2606 struct nfs_inode *nfsi = NFS_I(inode);
2607 struct nfs_access_entry *cache;
2608 int err = -ECHILD;
2609 struct list_head *lh;
2610
2611 rcu_read_lock();
2612 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2613 goto out;
Madhuparna Bhowmik9f01eb52019-12-10 11:16:39 +05302614 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
NeilBrownf682a392014-07-14 11:28:20 +10002615 cache = list_entry(lh, struct nfs_access_entry, lru);
2616 if (lh == &nfsi->access_cache_entry_lru ||
Trond Myklebust9a206de2020-01-26 17:31:14 -05002617 cred_fscmp(cred, cache->cred) != 0)
NeilBrownf682a392014-07-14 11:28:20 +10002618 cache = NULL;
2619 if (cache == NULL)
2620 goto out;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002621 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
NeilBrownf682a392014-07-14 11:28:20 +10002622 goto out;
NeilBrownf682a392014-07-14 11:28:20 +10002623 res->cred = cache->cred;
2624 res->mask = cache->mask;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002625 err = 0;
NeilBrownf682a392014-07-14 11:28:20 +10002626out:
2627 rcu_read_unlock();
2628 return err;
2629}
2630
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002631int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2632nfs_access_entry *res, bool may_block)
2633{
2634 int status;
2635
2636 status = nfs_access_get_cached_rcu(inode, cred, res);
2637 if (status != 0)
2638 status = nfs_access_get_cached_locked(inode, cred, res,
2639 may_block);
2640
2641 return status;
2642}
2643EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2644
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002645static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2646{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002647 struct nfs_inode *nfsi = NFS_I(inode);
2648 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002649 struct rb_node **p = &root_node->rb_node;
2650 struct rb_node *parent = NULL;
2651 struct nfs_access_entry *entry;
NeilBrownb68572e2018-12-03 11:30:30 +11002652 int cmp;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002653
2654 spin_lock(&inode->i_lock);
2655 while (*p != NULL) {
2656 parent = *p;
2657 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
NeilBrownb68572e2018-12-03 11:30:30 +11002658 cmp = cred_fscmp(set->cred, entry->cred);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002659
NeilBrownb68572e2018-12-03 11:30:30 +11002660 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002661 p = &parent->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002662 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002663 p = &parent->rb_right;
2664 else
2665 goto found;
2666 }
2667 rb_link_node(&set->rb_node, parent, p);
2668 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002669 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002670 spin_unlock(&inode->i_lock);
2671 return;
2672found:
2673 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002674 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2675 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002676 spin_unlock(&inode->i_lock);
2677 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002680void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002682 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2683 if (cache == NULL)
2684 return;
2685 RB_CLEAR_NODE(&cache->rb_node);
NeilBrownb68572e2018-12-03 11:30:30 +11002686 cache->cred = get_cred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002688
NeilBrownf682a392014-07-14 11:28:20 +10002689 /* The above field assignments must be visible
2690 * before this item appears on the lru. We cannot easily
2691 * use rcu_assign_pointer, so just force the memory barrier.
2692 */
2693 smp_wmb();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002694 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002695
2696 /* Update accounting */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002697 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002698 atomic_long_inc(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002699 smp_mb__after_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002700
2701 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002702 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002703 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002704 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2705 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2706 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002707 spin_unlock(&nfs_access_lru_lock);
2708 }
Trond Myklebust3a505842014-07-21 13:53:48 -04002709 nfs_access_cache_enforce_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002711EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2712
Anna Schumaker3c181822017-07-26 10:14:55 -04002713#define NFS_MAY_READ (NFS_ACCESS_READ)
2714#define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
2715 NFS_ACCESS_EXTEND | \
2716 NFS_ACCESS_DELETE)
2717#define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
2718 NFS_ACCESS_EXTEND)
Trond Myklebustecbb9032017-07-11 17:54:35 -04002719#define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
Anna Schumaker3c181822017-07-26 10:14:55 -04002720#define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
2721#define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002722static int
Trond Myklebustecbb9032017-07-11 17:54:35 -04002723nfs_access_calc_mask(u32 access_result, umode_t umode)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002724{
2725 int mask = 0;
2726
2727 if (access_result & NFS_MAY_READ)
2728 mask |= MAY_READ;
Trond Myklebustecbb9032017-07-11 17:54:35 -04002729 if (S_ISDIR(umode)) {
2730 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
2731 mask |= MAY_WRITE;
2732 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
2733 mask |= MAY_EXEC;
2734 } else if (S_ISREG(umode)) {
2735 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2736 mask |= MAY_WRITE;
2737 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
2738 mask |= MAY_EXEC;
2739 } else if (access_result & NFS_MAY_WRITE)
2740 mask |= MAY_WRITE;
Trond Myklebust15d4b732017-07-11 17:54:32 -04002741 return mask;
2742}
2743
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002744void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2745{
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002746 entry->mask = access_result;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002747}
2748EXPORT_SYMBOL_GPL(nfs_access_set_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
NeilBrownb68572e2018-12-03 11:30:30 +11002750static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 struct nfs_access_entry cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002753 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
Trond Myklebuste8194b72020-01-06 15:25:12 -05002754 int cache_mask = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 int status;
2756
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002757 trace_nfs_access_enter(inode);
2758
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002759 status = nfs_access_get_cached(inode, cred, &cache, may_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (status == 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002761 goto out_cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
NeilBrownf3324a22014-07-14 11:28:20 +10002763 status = -ECHILD;
Trond Myklebust57b69182016-06-03 17:07:19 -04002764 if (!may_block)
NeilBrownf3324a22014-07-14 11:28:20 +10002765 goto out;
2766
Anna Schumaker1750d922017-07-26 12:00:21 -04002767 /*
2768 * Determine which access bits we want to ask for...
2769 */
2770 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
Frank van der Linden72832a22020-06-23 22:38:58 +00002771 if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
2772 cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
2773 NFS_ACCESS_XALIST;
2774 }
Anna Schumaker1750d922017-07-26 12:00:21 -04002775 if (S_ISDIR(inode->i_mode))
2776 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
2777 else
2778 cache.mask |= NFS_ACCESS_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 cache.cred = cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002781 if (status != 0) {
2782 if (status == -ESTALE) {
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002783 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002784 nfs_set_inode_stale(inode);
2785 else
2786 nfs_zap_caches(inode);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002787 }
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002788 goto out;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 nfs_access_add_cache(inode, &cache);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002791out_cached:
Trond Myklebustecbb9032017-07-11 17:54:35 -04002792 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002793 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002794 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795out:
Trond Myklebuste8194b72020-01-06 15:25:12 -05002796 trace_nfs_access_exit(inode, mask, cache_mask, status);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002797 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
Trond Myklebustaf22f942007-08-10 17:45:10 -04002800static int nfs_open_permission_mask(int openflags)
2801{
2802 int mask = 0;
2803
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002804 if (openflags & __FMODE_EXEC) {
2805 /* ONLY check exec rights */
2806 mask = MAY_EXEC;
2807 } else {
2808 if ((openflags & O_ACCMODE) != O_WRONLY)
2809 mask |= MAY_READ;
2810 if ((openflags & O_ACCMODE) != O_RDONLY)
2811 mask |= MAY_WRITE;
2812 }
2813
Trond Myklebustaf22f942007-08-10 17:45:10 -04002814 return mask;
2815}
2816
NeilBrownb68572e2018-12-03 11:30:30 +11002817int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
Trond Myklebustaf22f942007-08-10 17:45:10 -04002818{
2819 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2820}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002821EXPORT_SYMBOL_GPL(nfs_may_open);
Trond Myklebustaf22f942007-08-10 17:45:10 -04002822
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002823static int nfs_execute_ok(struct inode *inode, int mask)
2824{
2825 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002826 int ret = 0;
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002827
Trond Myklebust38258272018-07-24 14:27:11 -04002828 if (S_ISDIR(inode->i_mode))
2829 return 0;
Trond Myklebustcf834022018-07-20 16:19:35 -04002830 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002831 if (mask & MAY_NOT_BLOCK)
2832 return -ECHILD;
2833 ret = __nfs_revalidate_inode(server, inode);
2834 }
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002835 if (ret == 0 && !execute_ok(inode))
2836 ret = -EACCES;
2837 return ret;
2838}
2839
Al Viro10556cb22011-06-20 19:28:19 -04002840int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
NeilBrownb68572e2018-12-03 11:30:30 +11002842 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 int res = 0;
2844
Chuck Lever91d5b472006-03-20 13:44:14 -05002845 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2846
Al Viroe6305c42008-07-15 21:03:57 -04002847 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 goto out;
2849 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002850 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 goto force_lookup;
2852
2853 switch (inode->i_mode & S_IFMT) {
2854 case S_IFLNK:
2855 goto out;
2856 case S_IFREG:
Trond Myklebust762674f2015-12-26 21:54:58 -05002857 if ((mask & MAY_OPEN) &&
2858 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 break;
2861 case S_IFDIR:
2862 /*
2863 * Optimize away all write operations, since the server
2864 * will check permissions when we perform the op.
2865 */
2866 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2867 goto out;
2868 }
2869
2870force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (!NFS_PROTO(inode)->access)
2872 goto out_notsup;
2873
Zhouyi Zhoueb095c12020-03-06 03:45:26 +00002874 res = nfs_do_access(inode, cred, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875out:
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002876 if (!res && (mask & MAY_EXEC))
2877 res = nfs_execute_ok(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +02002878
Niels de Vos1e8968c2013-12-17 18:20:16 +01002879 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002880 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return res;
2882out_notsup:
NeilBrownd51ac1a2014-07-14 11:28:20 +10002883 if (mask & MAY_NOT_BLOCK)
2884 return -ECHILD;
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2887 if (res == 0)
Al Viro2830ba72011-06-20 19:16:29 -04002888 res = generic_permission(inode, mask);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002889 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002891EXPORT_SYMBOL_GPL(nfs_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893/*
2894 * Local variables:
2895 * version-control: t
2896 * kept-new-versions: 5
2897 * End:
2898 */