blob: e37ffddd79c242ded2aa590432b3cfdf9b9d261d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/dir.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs directory handling functions
7 *
8 * 10 Apr 1996 Added silly rename for unlink --okir
9 * 28 Sep 1996 Improved directory cache --okir
10 * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
11 * Re-implemented silly rename for unlink, newly implemented
12 * silly rename for nfs_rename() following the suggestions
13 * of Olaf Kirch (okir) found in this file.
14 * Following Linus comments on my original hack, this version
15 * depends only on the dcache stuff and doesn't touch the inode
16 * layer (iput() and friends).
17 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
18 */
19
20#include <linux/time.h>
21#include <linux/errno.h>
22#include <linux/stat.h>
23#include <linux/fcntl.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/sunrpc/clnt.h>
29#include <linux/nfs_fs.h>
30#include <linux/nfs_mount.h>
31#include <linux/pagemap.h>
Chuck Lever873101b2006-08-22 20:06:23 -040032#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/namei.h>
David Howells54ceac42006-08-22 20:06:13 -040034#include <linux/mount.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Trond Myklebust4ce79712005-06-22 17:16:21 +000037#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050039#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080040#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040041#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* #define NFS_DEBUG_VERBOSE 1 */
44
45static int nfs_opendir(struct inode *, struct file *);
46static int nfs_readdir(struct file *, void *, filldir_t);
47static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
48static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
49static int nfs_mkdir(struct inode *, struct dentry *, int);
50static int nfs_rmdir(struct inode *, struct dentry *);
51static int nfs_unlink(struct inode *, struct dentry *);
52static int nfs_symlink(struct inode *, struct dentry *, const char *);
53static int nfs_link(struct dentry *, struct inode *, struct dentry *);
54static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
55static int nfs_rename(struct inode *, struct dentry *,
56 struct inode *, struct dentry *);
Christoph Hellwig7ea80852010-05-26 17:53:25 +020057static int nfs_fsync_dir(struct file *, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000058static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080060const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000061 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .read = generic_read_dir,
63 .readdir = nfs_readdir,
64 .open = nfs_opendir,
65 .release = nfs_release,
66 .fsync = nfs_fsync_dir,
67};
68
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080069const struct inode_operations nfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .create = nfs_create,
71 .lookup = nfs_lookup,
72 .link = nfs_link,
73 .unlink = nfs_unlink,
74 .symlink = nfs_symlink,
75 .mkdir = nfs_mkdir,
76 .rmdir = nfs_rmdir,
77 .mknod = nfs_mknod,
78 .rename = nfs_rename,
79 .permission = nfs_permission,
80 .getattr = nfs_getattr,
81 .setattr = nfs_setattr,
82};
83
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000084#ifdef CONFIG_NFS_V3
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080085const struct inode_operations nfs3_dir_inode_operations = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000086 .create = nfs_create,
87 .lookup = nfs_lookup,
88 .link = nfs_link,
89 .unlink = nfs_unlink,
90 .symlink = nfs_symlink,
91 .mkdir = nfs_mkdir,
92 .rmdir = nfs_rmdir,
93 .mknod = nfs_mknod,
94 .rename = nfs_rename,
95 .permission = nfs_permission,
96 .getattr = nfs_getattr,
97 .setattr = nfs_setattr,
98 .listxattr = nfs3_listxattr,
99 .getxattr = nfs3_getxattr,
100 .setxattr = nfs3_setxattr,
101 .removexattr = nfs3_removexattr,
102};
103#endif /* CONFIG_NFS_V3 */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#ifdef CONFIG_NFS_V4
106
107static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400108static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800109const struct inode_operations nfs4_dir_inode_operations = {
Trond Myklebustc0204fd2010-09-17 10:56:51 -0400110 .create = nfs_open_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .lookup = nfs_atomic_lookup,
112 .link = nfs_link,
113 .unlink = nfs_unlink,
114 .symlink = nfs_symlink,
115 .mkdir = nfs_mkdir,
116 .rmdir = nfs_rmdir,
117 .mknod = nfs_mknod,
118 .rename = nfs_rename,
119 .permission = nfs_permission,
120 .getattr = nfs_getattr,
121 .setattr = nfs_setattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +0000122 .getxattr = nfs4_getxattr,
123 .setxattr = nfs4_setxattr,
124 .listxattr = nfs4_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127#endif /* CONFIG_NFS_V4 */
128
129/*
130 * Open file
131 */
132static int
133nfs_opendir(struct inode *inode, struct file *filp)
134{
Carsten Otte7451c4f2006-04-19 13:06:37 -0400135 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Chuck Lever6da24bc2008-06-11 17:55:58 -0400137 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400138 filp->f_path.dentry->d_parent->d_name.name,
139 filp->f_path.dentry->d_name.name);
140
141 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* Call generic open code in order to cache credentials */
Carsten Otte7451c4f2006-04-19 13:06:37 -0400144 res = nfs_open(inode, filp);
Neil Brownf5a73672010-08-10 10:20:05 -0400145 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
146 /* This is a mountpoint, so d_revalidate will never
147 * have been called, so we need to refresh the
148 * inode (for close-open consistency) ourselves.
149 */
150 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return res;
153}
154
Al Viro0dbb4c62006-10-19 23:28:49 -0700155typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156typedef struct {
157 struct file *file;
158 struct page *page;
159 unsigned long page_index;
Al Viro0dbb4c62006-10-19 23:28:49 -0700160 __be32 *ptr;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000161 u64 *dir_cookie;
162 loff_t current_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct nfs_entry *entry;
164 decode_dirent_t decode;
165 int plus;
Neil Brown1f4eab72007-04-16 09:35:27 +1000166 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400167 unsigned long gencount;
Neil Brown1f4eab72007-04-16 09:35:27 +1000168 int timestamp_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169} nfs_readdir_descriptor_t;
170
171/* Now we cache directories properly, by stuffing the dirent
172 * data directly in the page cache.
173 *
174 * Inode invalidation due to refresh etc. takes care of
175 * _everything_, no sloppy entry flushing logic, no extraneous
176 * copying, network direct to page cache, the way it was meant
177 * to be.
178 *
179 * NOTE: Dirent information verification is done always by the
180 * page-in of the RPC reply, nowhere else, this simplies
181 * things substantially.
182 */
183static
184int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
185{
186 struct file *file = desc->file;
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800187 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct rpc_cred *cred = nfs_file_cred(file);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400189 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int error;
191
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500192 dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700193 __func__, (long long)desc->entry->cookie,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500194 page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 again:
197 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400198 gencount = nfs_inc_attr_generation_counter();
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800199 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 NFS_SERVER(inode)->dtsize, desc->plus);
201 if (error < 0) {
202 /* We requested READDIRPLUS, but the server doesn't grok it */
203 if (error == -ENOTSUPP && desc->plus) {
204 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
Benny Halevy3a10c302008-01-23 08:58:59 +0200205 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 desc->plus = 0;
207 goto again;
208 }
209 goto error;
210 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000211 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400212 desc->gencount = gencount;
Neil Brown1f4eab72007-04-16 09:35:27 +1000213 desc->timestamp_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Ensure consistent page alignment of the data.
216 * Note: assumes we have exclusive access to this mapping either
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800217 * through inode->i_mutex or some other mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400219 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700220 /* Should never happen */
221 nfs_zap_mapping(inode, inode->i_mapping);
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unlock_page(page);
224 return 0;
225 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EIO;
228}
229
230static inline
231int dir_decode(nfs_readdir_descriptor_t *desc)
232{
Al Viro0dbb4c62006-10-19 23:28:49 -0700233 __be32 *p = desc->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 p = desc->decode(p, desc->entry, desc->plus);
235 if (IS_ERR(p))
236 return PTR_ERR(p);
237 desc->ptr = p;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400238 if (desc->timestamp_valid) {
Neil Brown1f4eab72007-04-16 09:35:27 +1000239 desc->entry->fattr->time_start = desc->timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400240 desc->entry->fattr->gencount = desc->gencount;
241 } else
Neil Brown1f4eab72007-04-16 09:35:27 +1000242 desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
246static inline
247void dir_page_release(nfs_readdir_descriptor_t *desc)
248{
249 kunmap(desc->page);
250 page_cache_release(desc->page);
251 desc->page = NULL;
252 desc->ptr = NULL;
253}
254
255/*
256 * Given a pointer to a buffer that has already been filled by a call
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000257 * to readdir, find the next entry with cookie '*desc->dir_cookie'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
259 * If the end of the buffer has been reached, return -EAGAIN, if not,
260 * return the offset within the buffer of the next entry to be
261 * read.
262 */
263static inline
Olivier Galibert00a92642005-06-22 17:16:29 +0000264int find_dirent(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct nfs_entry *entry = desc->entry;
267 int loop_count = 0,
268 status;
269
270 while((status = dir_decode(desc)) == 0) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500271 dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700272 __func__, (unsigned long long)entry->cookie);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000273 if (entry->prev_cookie == *desc->dir_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 if (loop_count++ > 200) {
276 loop_count = 0;
277 schedule();
278 }
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return status;
281}
282
283/*
Olivier Galibert00a92642005-06-22 17:16:29 +0000284 * Given a pointer to a buffer that has already been filled by a call
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000285 * to readdir, find the entry at offset 'desc->file->f_pos'.
Olivier Galibert00a92642005-06-22 17:16:29 +0000286 *
287 * If the end of the buffer has been reached, return -EAGAIN, if not,
288 * return the offset within the buffer of the next entry to be
289 * read.
290 */
291static inline
292int find_dirent_index(nfs_readdir_descriptor_t *desc)
293{
294 struct nfs_entry *entry = desc->entry;
295 int loop_count = 0,
296 status;
297
298 for(;;) {
299 status = dir_decode(desc);
300 if (status)
301 break;
302
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500303 dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
304 (unsigned long long)entry->cookie, desc->current_index);
Olivier Galibert00a92642005-06-22 17:16:29 +0000305
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000306 if (desc->file->f_pos == desc->current_index) {
307 *desc->dir_cookie = entry->cookie;
Olivier Galibert00a92642005-06-22 17:16:29 +0000308 break;
309 }
310 desc->current_index++;
311 if (loop_count++ > 200) {
312 loop_count = 0;
313 schedule();
314 }
315 }
Olivier Galibert00a92642005-06-22 17:16:29 +0000316 return status;
317}
318
319/*
320 * Find the given page, and call find_dirent() or find_dirent_index in
321 * order to try to return the next entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
323static inline
324int find_dirent_page(nfs_readdir_descriptor_t *desc)
325{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800326 struct inode *inode = desc->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct page *page;
328 int status;
329
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500330 dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700331 __func__, desc->page_index,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500332 (long long) *desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Neil Brown1f4eab72007-04-16 09:35:27 +1000334 /* If we find the page in the page_cache, we cannot be sure
335 * how fresh the data is, so we will ignore readdir_plus attributes.
336 */
337 desc->timestamp_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 page = read_cache_page(inode->i_mapping, desc->page_index,
339 (filler_t *)nfs_readdir_filler, desc);
340 if (IS_ERR(page)) {
341 status = PTR_ERR(page);
342 goto out;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* NOTE: Someone else may have changed the READDIRPLUS flag */
346 desc->page = page;
347 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000348 if (*desc->dir_cookie != 0)
Olivier Galibert00a92642005-06-22 17:16:29 +0000349 status = find_dirent(desc);
350 else
351 status = find_dirent_index(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (status < 0)
353 dir_page_release(desc);
354 out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700355 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
360 * Recurse through the page cache pages, and return a
361 * filled nfs_entry structure of the next directory entry if possible.
362 *
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000363 * The target for the search is '*desc->dir_cookie' if non-0,
364 * 'desc->file->f_pos' otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366static inline
367int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
368{
369 int loop_count = 0;
370 int res;
371
Olivier Galibert00a92642005-06-22 17:16:29 +0000372 /* Always search-by-index from the beginning of the cache */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000373 if (*desc->dir_cookie == 0) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500374 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
375 (long long)desc->file->f_pos);
Olivier Galibert00a92642005-06-22 17:16:29 +0000376 desc->page_index = 0;
377 desc->entry->cookie = desc->entry->prev_cookie = 0;
378 desc->entry->eof = 0;
379 desc->current_index = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000380 } else
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500381 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
382 (unsigned long long)*desc->dir_cookie);
Olivier Galibert00a92642005-06-22 17:16:29 +0000383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (;;) {
385 res = find_dirent_page(desc);
386 if (res != -EAGAIN)
387 break;
388 /* Align to beginning of next page */
389 desc->page_index ++;
390 if (loop_count++ > 200) {
391 loop_count = 0;
392 schedule();
393 }
394 }
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500395
Harvey Harrison3110ff82008-05-02 13:42:44 -0700396 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return res;
398}
399
400static inline unsigned int dt_type(struct inode *inode)
401{
402 return (inode->i_mode >> 12) & 15;
403}
404
405static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
406
407/*
408 * Once we've found the start of the dirent within a page: fill 'er up...
409 */
410static
411int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
412 filldir_t filldir)
413{
414 struct file *file = desc->file;
415 struct nfs_entry *entry = desc->entry;
416 struct dentry *dentry = NULL;
Peter Staubach4e769b92007-08-03 15:07:10 -0400417 u64 fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int loop_count = 0,
419 res;
420
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500421 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
422 (unsigned long long)entry->cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 for(;;) {
425 unsigned d_type = DT_UNKNOWN;
426 /* Note: entry->prev_cookie contains the cookie for
427 * retrieving the current dirent on the server */
Peter Staubach4e769b92007-08-03 15:07:10 -0400428 fileid = entry->ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Get a dentry if we have one */
431 if (dentry != NULL)
432 dput(dentry);
433 dentry = nfs_readdir_lookup(desc);
434
435 /* Use readdirplus info */
436 if (dentry != NULL && dentry->d_inode != NULL) {
437 d_type = dt_type(dentry->d_inode);
Peter Staubach4e769b92007-08-03 15:07:10 -0400438 fileid = NFS_FILEID(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 res = filldir(dirent, entry->name, entry->len,
Trond Myklebustf43bf0b2007-10-09 12:01:04 -0400442 file->f_pos, nfs_compat_user_ino64(fileid),
443 d_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (res < 0)
445 break;
Olivier Galibert00a92642005-06-22 17:16:29 +0000446 file->f_pos++;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000447 *desc->dir_cookie = entry->cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (dir_decode(desc) != 0) {
449 desc->page_index ++;
450 break;
451 }
452 if (loop_count++ > 200) {
453 loop_count = 0;
454 schedule();
455 }
456 }
457 dir_page_release(desc);
458 if (dentry != NULL)
459 dput(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500460 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
461 (unsigned long long)*desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return res;
463}
464
465/*
466 * If we cannot find a cookie in our cache, we suspect that this is
467 * because it points to a deleted file, so we ask the server to return
468 * whatever it thinks is the next entry. We then feed this to filldir.
469 * If all goes well, we should then be able to find our way round the
470 * cache on the next call to readdir_search_pagecache();
471 *
472 * NOTE: we cannot add the anonymous page to the pagecache because
473 * the data it contains might not be page aligned. Besides,
474 * we should already have a complete representation of the
475 * directory in the page cache by the time we get here.
476 */
477static inline
478int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
479 filldir_t filldir)
480{
481 struct file *file = desc->file;
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800482 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct rpc_cred *cred = nfs_file_cred(file);
484 struct page *page = NULL;
485 int status;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400486 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500488 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
489 (unsigned long long)*desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 page = alloc_page(GFP_HIGHUSER);
492 if (!page) {
493 status = -ENOMEM;
494 goto out;
495 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000496 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400497 gencount = nfs_inc_attr_generation_counter();
Jeff Layton25606652008-02-12 06:49:01 -0500498 status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred,
499 *desc->dir_cookie, page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 NFS_SERVER(inode)->dtsize,
501 desc->plus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 desc->page = page;
503 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
Jeff Layton25606652008-02-12 06:49:01 -0500504 if (status >= 0) {
Neil Brown1f4eab72007-04-16 09:35:27 +1000505 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400506 desc->gencount = gencount;
Neil Brown1f4eab72007-04-16 09:35:27 +1000507 desc->timestamp_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if ((status = dir_decode(desc)) == 0)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000509 desc->entry->prev_cookie = *desc->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else
511 status = -EIO;
512 if (status < 0)
513 goto out_release;
514
515 status = nfs_do_filldir(desc, dirent, filldir);
516
517 /* Reset read descriptor so it searches the page cache from
518 * the start upon the next call to readdir_search_pagecache() */
519 desc->page_index = 0;
520 desc->entry->cookie = desc->entry->prev_cookie = 0;
521 desc->entry->eof = 0;
522 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500523 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700524 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return status;
526 out_release:
527 dir_page_release(desc);
528 goto out;
529}
530
Olivier Galibert00a92642005-06-22 17:16:29 +0000531/* The file offset position represents the dirent entry number. A
532 last cookie cache takes care of the common case of reading the
533 whole directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
536{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800537 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct inode *inode = dentry->d_inode;
539 nfs_readdir_descriptor_t my_desc,
540 *desc = &my_desc;
541 struct nfs_entry my_entry;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400542 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Chuck Lever6da24bc2008-06-11 17:55:58 -0400544 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500545 dentry->d_parent->d_name.name, dentry->d_name.name,
546 (long long)filp->f_pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500547 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
Olivier Galibert00a92642005-06-22 17:16:29 +0000550 * filp->f_pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000551 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000552 * to either find the entry with the appropriate number or
553 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555 memset(desc, 0, sizeof(*desc));
556
557 desc->file = filp;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400558 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 desc->decode = NFS_PROTO(inode)->decode_dirent;
560 desc->plus = NFS_USE_READDIRPLUS(inode);
561
562 my_entry.cookie = my_entry.prev_cookie = 0;
563 my_entry.eof = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400564 my_entry.fh = nfs_alloc_fhandle();
565 my_entry.fattr = nfs_alloc_fattr();
566 if (my_entry.fh == NULL || my_entry.fattr == NULL)
567 goto out_alloc_failed;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 desc->entry = &my_entry;
570
Trond Myklebust565277f2007-10-15 18:17:53 -0400571 nfs_block_sillyrename(dentry);
Trond Myklebust1cda7072010-02-19 17:03:30 -0800572 res = nfs_revalidate_mapping(inode, filp->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500573 if (res < 0)
574 goto out;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 while(!desc->entry->eof) {
577 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (res == -EBADCOOKIE) {
580 /* This means either end of directory */
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000581 if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Or that the server has 'lost' a cookie */
583 res = uncached_readdir(desc, dirent, filldir);
584 if (res >= 0)
585 continue;
586 }
587 res = 0;
588 break;
589 }
590 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200591 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 nfs_zap_caches(inode);
593 desc->plus = 0;
594 desc->entry->eof = 0;
595 continue;
596 }
597 if (res < 0)
598 break;
599
600 res = nfs_do_filldir(desc, dirent, filldir);
601 if (res < 0) {
602 res = 0;
603 break;
604 }
605 }
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500606out:
Trond Myklebust565277f2007-10-15 18:17:53 -0400607 nfs_unblock_sillyrename(dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500608 if (res > 0)
609 res = 0;
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400610out_alloc_failed:
611 nfs_free_fattr(my_entry.fattr);
612 nfs_free_fhandle(my_entry.fh);
613 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500614 dentry->d_parent->d_name.name, dentry->d_name.name,
615 res);
616 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Trond Myklebust10afec92007-05-14 17:16:04 -0400619static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000620{
Chuck Leverb84e06c2008-06-11 17:55:34 -0400621 struct dentry *dentry = filp->f_path.dentry;
622 struct inode *inode = dentry->d_inode;
623
Chuck Lever6da24bc2008-06-11 17:55:58 -0400624 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
Chuck Leverb84e06c2008-06-11 17:55:34 -0400625 dentry->d_parent->d_name.name,
626 dentry->d_name.name,
627 offset, origin);
628
629 mutex_lock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000630 switch (origin) {
631 case 1:
632 offset += filp->f_pos;
633 case 0:
634 if (offset >= 0)
635 break;
636 default:
637 offset = -EINVAL;
638 goto out;
639 }
640 if (offset != filp->f_pos) {
641 filp->f_pos = offset;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400642 nfs_file_open_context(filp)->dir_cookie = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000643 }
644out:
Chuck Leverb84e06c2008-06-11 17:55:34 -0400645 mutex_unlock(&inode->i_mutex);
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000646 return offset;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * All directory operations under NFS are synchronous, so fsync()
651 * is a dummy operation.
652 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200653static int nfs_fsync_dir(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200655 struct dentry *dentry = filp->f_path.dentry;
656
Chuck Lever6da24bc2008-06-11 17:55:58 -0400657 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500658 dentry->d_parent->d_name.name, dentry->d_name.name,
659 datasync);
660
Chuck Lever54917782008-05-27 16:29:07 -0400661 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663}
664
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400665/**
666 * nfs_force_lookup_revalidate - Mark the directory as having changed
667 * @dir - pointer to directory inode
668 *
669 * This forces the revalidation code in nfs_lookup_revalidate() to do a
670 * full lookup on all child dentries of 'dir' whenever a change occurs
671 * on the server that might have invalidated our dcache.
672 *
673 * The caller should be holding dir->i_lock
674 */
675void nfs_force_lookup_revalidate(struct inode *dir)
676{
Trond Myklebust011935a2008-10-14 19:24:50 -0400677 NFS_I(dir)->cache_change_attribute++;
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * A check for whether or not the parent directory has changed.
682 * In the case it has, we assume that the dentries are untrustworthy
683 * and may need to be looked up again.
684 */
Trond Myklebustc79ba782007-01-31 08:16:24 -0500685static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 if (IS_ROOT(dentry))
688 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400689 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
690 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -0400691 if (!nfs_verify_change_attribute(dir, dentry->d_time))
692 return 0;
693 /* Revalidate nfsi->cache_change_attribute before we declare a match */
694 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
695 return 0;
696 if (!nfs_verify_change_attribute(dir, dentry->d_time))
697 return 0;
698 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400702 * Return the intent data that applies to this particular path component
703 *
704 * Note that the current set of intents only apply to the very last
705 * component of the path.
706 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
707 */
708static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
709{
710 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
711 return 0;
712 return nd->flags & mask;
713}
714
715/*
Trond Myklebusta12802c2007-10-02 19:13:04 -0400716 * Use intent information to check whether or not we're going to do
717 * an O_EXCL create using this path component.
718 */
719static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
720{
721 if (NFS_PROTO(dir)->version == 2)
722 return 0;
Al Viro35165862008-08-05 03:00:49 -0400723 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
Trond Myklebusta12802c2007-10-02 19:13:04 -0400724}
725
726/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400727 * Inode and filehandle revalidation for lookups.
728 *
729 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
730 * or if the intent information indicates that we're about to open this
731 * particular file and the "nocto" mount flag is not set.
732 *
733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static inline
735int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
736{
737 struct nfs_server *server = NFS_SERVER(inode);
738
Trond Myklebust4e99a1f2008-03-06 12:34:59 -0500739 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (nd != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* VFS wants an on-the-wire revalidation */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400743 if (nd->flags & LOOKUP_REVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto out_force;
745 /* This is an open(2) */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400746 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
Trond Myklebust4e0641a2006-07-05 13:05:13 -0400747 !(server->flags & NFS_MOUNT_NOCTO) &&
748 (S_ISREG(inode->i_mode) ||
749 S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto out_force;
Trond Myklebust4f48af42007-10-02 23:13:32 -0400751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 return nfs_revalidate_inode(server, inode);
754out_force:
755 return __nfs_revalidate_inode(server, inode);
756}
757
758/*
759 * We judge how long we want to trust negative
760 * dentries by looking at the parent inode mtime.
761 *
762 * If parent mtime has changed, we revalidate, else we wait for a
763 * period corresponding to the parent's attribute cache timeout value.
764 */
765static inline
766int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
767 struct nameidata *nd)
768{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* Don't revalidate a negative dentry if we're creating a new file */
Trond Myklebust1d6757f2005-06-07 18:37:01 -0400770 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -0400772 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
773 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return !nfs_check_verifier(dir, dentry);
775}
776
777/*
778 * This is called every time the dcache has a lookup hit,
779 * and we should check whether we can really trust that
780 * lookup.
781 *
782 * NOTE! The hit can be a negative hit too, don't assume
783 * we have an inode!
784 *
785 * If the parent directory is seen to have changed, we throw out the
786 * cached dentry and do a new lookup.
787 */
788static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
789{
790 struct inode *dir;
791 struct inode *inode;
792 struct dentry *parent;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400793 struct nfs_fh *fhandle = NULL;
794 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 dir = parent->d_inode;
Chuck Lever91d5b472006-03-20 13:44:14 -0500799 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 inode = dentry->d_inode;
801
802 if (!inode) {
803 if (nfs_neg_need_reval(dir, dentry, nd))
804 goto out_bad;
805 goto out_valid;
806 }
807
808 if (is_bad_inode(inode)) {
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500809 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700810 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500811 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto out_bad;
813 }
814
Trond Myklebust15860ab2008-12-23 15:21:54 -0500815 if (nfs_have_delegation(inode, FMODE_READ))
816 goto out_set_verifier;
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Force a full look up iff the parent directory has changed */
Trond Myklebusta12802c2007-10-02 19:13:04 -0400819 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (nfs_lookup_verify_inode(inode, nd))
821 goto out_zap_parent;
822 goto out_valid;
823 }
824
825 if (NFS_STALE(inode))
826 goto out_bad;
827
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400828 error = -ENOMEM;
829 fhandle = nfs_alloc_fhandle();
830 fattr = nfs_alloc_fattr();
831 if (fhandle == NULL || fattr == NULL)
832 goto out_error;
833
834 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (error)
836 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400837 if (nfs_compare_fh(NFS_FH(inode), fhandle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto out_bad;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400839 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto out_bad;
841
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400842 nfs_free_fattr(fattr);
843 nfs_free_fhandle(fhandle);
Trond Myklebust15860ab2008-12-23 15:21:54 -0500844out_set_verifier:
Trond Myklebustcf8ba452007-10-01 13:46:53 -0400845 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500848 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700849 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500850 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 1;
852out_zap_parent:
853 nfs_zap_caches(dir);
854 out_bad:
Trond Myklebusta1643a92007-09-29 17:25:43 -0400855 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (inode && S_ISDIR(inode->i_mode)) {
857 /* Purge readdir caches. */
858 nfs_zap_caches(inode);
859 /* If we have submounts, don't unhash ! */
860 if (have_submounts(dentry))
861 goto out_valid;
Al Virod9e80b72010-04-29 03:10:43 +0100862 if (dentry->d_flags & DCACHE_DISCONNECTED)
863 goto out_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 shrink_dcache_parent(dentry);
865 }
866 d_drop(dentry);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400867 nfs_free_fattr(fattr);
868 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 dput(parent);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500870 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700871 __func__, dentry->d_parent->d_name.name,
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500872 dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400874out_error:
875 nfs_free_fattr(fattr);
876 nfs_free_fhandle(fhandle);
877 dput(parent);
878 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
879 __func__, dentry->d_parent->d_name.name,
880 dentry->d_name.name, error);
881 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884/*
885 * This is called from dput() when d_count is going to 0.
886 */
887static int nfs_dentry_delete(struct dentry *dentry)
888{
889 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
890 dentry->d_parent->d_name.name, dentry->d_name.name,
891 dentry->d_flags);
892
Trond Myklebust77f11192008-01-28 19:43:19 -0500893 /* Unhash any dentry with a stale inode */
894 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
895 return 1;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
898 /* Unhash it, so that ->d_iput() would be called */
899 return 1;
900 }
901 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
902 /* Unhash it, so that ancestors of killed async unlink
903 * files will be cleaned up during umount */
904 return 1;
905 }
906 return 0;
907
908}
909
Trond Myklebust1b83d702008-06-11 15:44:04 -0400910static void nfs_drop_nlink(struct inode *inode)
911{
912 spin_lock(&inode->i_lock);
913 if (inode->i_nlink > 0)
914 drop_nlink(inode);
915 spin_unlock(&inode->i_lock);
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/*
919 * Called when the dentry loses inode.
920 * We use it to clean up silly-renamed files.
921 */
922static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
923{
Neil Brown83672d32007-02-26 12:48:25 +1100924 if (S_ISDIR(inode->i_mode))
925 /* drop any readdir cache as it could easily be old */
926 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700929 drop_nlink(inode);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400930 nfs_complete_unlink(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 iput(inode);
933}
934
Al Virof786aa92009-02-20 05:51:22 +0000935const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 .d_revalidate = nfs_lookup_revalidate,
937 .d_delete = nfs_dentry_delete,
938 .d_iput = nfs_dentry_iput,
939};
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
942{
943 struct dentry *res;
Trond Myklebust565277f2007-10-15 18:17:53 -0400944 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400946 struct nfs_fh *fhandle = NULL;
947 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
951 dentry->d_parent->d_name.name, dentry->d_name.name);
Chuck Lever91d5b472006-03-20 13:44:14 -0500952 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 res = ERR_PTR(-ENAMETOOLONG);
955 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
956 goto out;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
959
Trond Myklebustfd684072006-09-05 12:27:44 -0400960 /*
961 * If we're doing an exclusive create, optimize away the lookup
962 * but don't hash the dentry.
963 */
964 if (nfs_is_exclusive_create(dir, nd)) {
965 d_instantiate(dentry, NULL);
966 res = NULL;
Trond Myklebustfc0f6842008-06-11 15:44:20 -0400967 goto out;
Trond Myklebustfd684072006-09-05 12:27:44 -0400968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400970 res = ERR_PTR(-ENOMEM);
971 fhandle = nfs_alloc_fhandle();
972 fattr = nfs_alloc_fattr();
973 if (fhandle == NULL || fattr == NULL)
974 goto out;
975
Trond Myklebust565277f2007-10-15 18:17:53 -0400976 parent = dentry->d_parent;
977 /* Protect against concurrent sillydeletes */
978 nfs_block_sillyrename(parent);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400979 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (error == -ENOENT)
981 goto no_entry;
982 if (error < 0) {
983 res = ERR_PTR(error);
Trond Myklebust565277f2007-10-15 18:17:53 -0400984 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400986 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500987 res = (struct dentry *)inode;
988 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -0400989 goto out_unblock_sillyrename;
David Howells54ceac42006-08-22 20:06:13 -0400990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991no_entry:
David Howells54ceac42006-08-22 20:06:13 -0400992 res = d_materialise_unique(dentry, inode);
Trond Myklebust9eaef272006-10-21 10:24:20 -0700993 if (res != NULL) {
994 if (IS_ERR(res))
Trond Myklebust565277f2007-10-15 18:17:53 -0400995 goto out_unblock_sillyrename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -0700997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -0400999out_unblock_sillyrename:
1000 nfs_unblock_sillyrename(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001002 nfs_free_fattr(fattr);
1003 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return res;
1005}
1006
1007#ifdef CONFIG_NFS_V4
1008static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1009
Al Virof786aa92009-02-20 05:51:22 +00001010const struct dentry_operations nfs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 .d_revalidate = nfs_open_revalidate,
1012 .d_delete = nfs_dentry_delete,
1013 .d_iput = nfs_dentry_iput,
1014};
1015
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001016/*
1017 * Use intent information to determine whether we need to substitute
1018 * the NFSv4-style stateful OPEN for the LOOKUP call
1019 */
Trond Myklebust5584c302008-12-23 15:21:54 -05001020static int is_atomic_open(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001022 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024 /* NFS does not (yet) have a stateful open for directories */
1025 if (nd->flags & LOOKUP_DIRECTORY)
1026 return 0;
1027 /* Are we trying to write to a read only partition? */
Dave Hansen2c463e92008-02-15 14:37:56 -08001028 if (__mnt_is_readonly(nd->path.mnt) &&
1029 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
1031 return 1;
1032}
1033
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001034static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1035{
1036 struct path path = {
1037 .mnt = nd->path.mnt,
1038 .dentry = dentry,
1039 };
1040 struct nfs_open_context *ctx;
1041 struct rpc_cred *cred;
1042 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1043
1044 cred = rpc_lookup_cred();
1045 if (IS_ERR(cred))
1046 return ERR_CAST(cred);
1047 ctx = alloc_nfs_open_context(&path, cred, fmode);
1048 put_rpccred(cred);
1049 if (ctx == NULL)
1050 return ERR_PTR(-ENOMEM);
1051 return ctx;
1052}
1053
1054static int do_open(struct inode *inode, struct file *filp)
1055{
1056 nfs_fscache_set_inode_cookie(inode, filp);
1057 return 0;
1058}
1059
1060static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1061{
1062 struct file *filp;
1063 int ret = 0;
1064
1065 /* If the open_intent is for execute, we have an extra check to make */
1066 if (ctx->mode & FMODE_EXEC) {
1067 ret = nfs_may_open(ctx->path.dentry->d_inode,
1068 ctx->cred,
1069 nd->intent.open.flags);
1070 if (ret < 0)
1071 goto out;
1072 }
1073 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1074 if (IS_ERR(filp))
1075 ret = PTR_ERR(filp);
1076 else
1077 nfs_file_set_open_context(filp, ctx);
1078out:
1079 put_nfs_open_context(ctx);
1080 return ret;
1081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1084{
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001085 struct nfs_open_context *ctx;
1086 struct iattr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct dentry *res = NULL;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001088 struct inode *inode;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001089 int open_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001091 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1092 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* Check that we are indeed trying to open this file */
Trond Myklebust5584c302008-12-23 15:21:54 -05001095 if (!is_atomic_open(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto no_open;
1097
1098 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1099 res = ERR_PTR(-ENAMETOOLONG);
1100 goto out;
1101 }
1102 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1103
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001104 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1105 * the dentry. */
Al Viro35165862008-08-05 03:00:49 -04001106 if (nd->flags & LOOKUP_EXCL) {
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04001107 d_instantiate(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001108 goto out;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001111 ctx = nameidata_to_nfs_open_context(dentry, nd);
1112 res = ERR_CAST(ctx);
1113 if (IS_ERR(ctx))
1114 goto out;
1115
1116 open_flags = nd->intent.open.flags;
1117 if (nd->flags & LOOKUP_CREATE) {
1118 attr.ia_mode = nd->intent.open.create_mode;
1119 attr.ia_valid = ATTR_MODE;
1120 if (!IS_POSIXACL(dir))
1121 attr.ia_mode &= ~current_umask();
1122 } else {
1123 open_flags &= ~O_EXCL;
1124 attr.ia_valid = 0;
1125 BUG_ON(open_flags & O_CREAT);
1126 }
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* Open the file on the server */
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001129 nfs_block_sillyrename(dentry->d_parent);
1130 inode = nfs4_atomic_open(dir, ctx, open_flags, &attr);
1131 if (IS_ERR(inode)) {
1132 nfs_unblock_sillyrename(dentry->d_parent);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001133 put_nfs_open_context(ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001134 switch (PTR_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /* Make a negative dentry */
1136 case -ENOENT:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001137 d_add(dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001138 res = NULL;
1139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* This turned out not to be a regular file */
Trond Myklebust80e60632010-03-25 13:51:05 -04001141 case -EISDIR:
Trond Myklebust6f926b52005-10-18 14:20:18 -07001142 case -ENOTDIR:
1143 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 case -ELOOP:
1145 if (!(nd->intent.open.flags & O_NOFOLLOW))
1146 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* case -EINVAL: */
1148 default:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001149 res = ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 goto out;
1151 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001152 }
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001153 res = d_add_unique(dentry, inode);
1154 if (res != NULL) {
1155 dput(ctx->path.dentry);
1156 ctx->path.dentry = dget(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 dentry = res;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001158 }
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001159 nfs_intent_set_file(nd, ctx);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001160 nfs_unblock_sillyrename(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161out:
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001162 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return res;
1164no_open:
1165 return nfs_lookup(dir, dentry, nd);
1166}
1167
1168static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1169{
1170 struct dentry *parent = NULL;
1171 struct inode *inode = dentry->d_inode;
1172 struct inode *dir;
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001173 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int openflags, ret = 0;
1175
Trond Myklebust1f063d22010-04-22 15:35:55 -04001176 if (!is_atomic_open(nd) || d_mountpoint(dentry))
Trond Myklebust5584c302008-12-23 15:21:54 -05001177 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 parent = dget_parent(dentry);
1179 dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* We can't create new files in nfs_open_revalidate(), so we
1181 * optimize away revalidation of negative dentries.
1182 */
Trond Myklebust216d5d02007-10-01 20:10:12 -04001183 if (inode == NULL) {
1184 if (!nfs_neg_need_reval(dir, dentry, nd))
1185 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto out;
Trond Myklebust216d5d02007-10-01 20:10:12 -04001187 }
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* NFS only supports OPEN on regular files */
1190 if (!S_ISREG(inode->i_mode))
Trond Myklebust5584c302008-12-23 15:21:54 -05001191 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 openflags = nd->intent.open.flags;
1193 /* We cannot do exclusive creation on a positive dentry */
1194 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
Trond Myklebust5584c302008-12-23 15:21:54 -05001195 goto no_open_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* We can't create new files, or truncate existing ones here */
Trond Myklebust0a377cf2010-08-18 09:25:42 -04001197 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001199 ctx = nameidata_to_nfs_open_context(dentry, nd);
1200 ret = PTR_ERR(ctx);
1201 if (IS_ERR(ctx))
1202 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001204 * Note: we're not holding inode->i_mutex and so may be racing with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * operations that change the directory. We therefore save the
1206 * change attribute *before* we do the RPC call.
1207 */
Trond Myklebust535918f2010-09-17 10:56:51 -04001208 inode = nfs4_atomic_open(dir, ctx, openflags, NULL);
1209 if (IS_ERR(inode)) {
1210 ret = PTR_ERR(inode);
1211 switch (ret) {
1212 case -EPERM:
1213 case -EACCES:
1214 case -EDQUOT:
1215 case -ENOSPC:
1216 case -EROFS:
1217 goto out_put_ctx;
1218 default:
1219 goto out_drop;
1220 }
1221 }
1222 iput(inode);
1223 if (inode == dentry->d_inode) {
1224 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustb8d4cad2010-09-17 10:56:51 -04001225 nfs_intent_set_file(nd, ctx);
Trond Myklebust535918f2010-09-17 10:56:51 -04001226 } else
1227 goto out_drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228out:
1229 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return ret;
Trond Myklebust535918f2010-09-17 10:56:51 -04001231out_drop:
1232 d_drop(dentry);
1233 ret = 0;
1234out_put_ctx:
1235 put_nfs_open_context(ctx);
1236 goto out;
1237
Trond Myklebust5584c302008-12-23 15:21:54 -05001238no_open_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 dput(parent);
Trond Myklebust5584c302008-12-23 15:21:54 -05001240no_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return nfs_lookup_revalidate(dentry, nd);
1242}
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001243
1244static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1245 struct nameidata *nd)
1246{
1247 struct nfs_open_context *ctx = NULL;
1248 struct iattr attr;
1249 int error;
1250 int open_flags = 0;
1251
1252 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1253 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1254
1255 attr.ia_mode = mode;
1256 attr.ia_valid = ATTR_MODE;
1257
1258 if ((nd->flags & LOOKUP_CREATE) != 0) {
1259 open_flags = nd->intent.open.flags;
1260
1261 ctx = nameidata_to_nfs_open_context(dentry, nd);
1262 error = PTR_ERR(ctx);
1263 if (IS_ERR(ctx))
1264 goto out_err;
1265 }
1266
1267 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1268 if (error != 0)
1269 goto out_put_ctx;
1270 if (ctx != NULL)
1271 nfs_intent_set_file(nd, ctx);
1272 return 0;
1273out_put_ctx:
1274 if (ctx != NULL)
1275 put_nfs_open_context(ctx);
1276out_err:
1277 d_drop(dentry);
1278 return error;
1279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#endif /* CONFIG_NFSV4 */
1282
1283static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
1284{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -08001285 struct dentry *parent = desc->file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct inode *dir = parent->d_inode;
1287 struct nfs_entry *entry = desc->entry;
1288 struct dentry *dentry, *alias;
1289 struct qstr name = {
1290 .name = entry->name,
1291 .len = entry->len,
1292 };
1293 struct inode *inode;
Trond Myklebust57fa76f2007-09-30 18:01:13 -04001294 unsigned long verf = nfs_save_change_attribute(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 switch (name.len) {
1297 case 2:
1298 if (name.name[0] == '.' && name.name[1] == '.')
1299 return dget_parent(parent);
1300 break;
1301 case 1:
1302 if (name.name[0] == '.')
1303 return dget(parent);
1304 }
Trond Myklebust57fa76f2007-09-30 18:01:13 -04001305
1306 spin_lock(&dir->i_lock);
1307 if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
1308 spin_unlock(&dir->i_lock);
1309 return NULL;
1310 }
1311 spin_unlock(&dir->i_lock);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 name.hash = full_name_hash(name.name, name.len);
1314 dentry = d_lookup(parent, &name);
Trond Myklebustdf1d5d22007-01-15 13:56:29 -05001315 if (dentry != NULL) {
Trond Myklebustef75c792007-01-16 10:09:44 -05001316 /* Is this a positive dentry that matches the readdir info? */
1317 if (dentry->d_inode != NULL &&
1318 (NFS_FILEID(dentry->d_inode) == entry->ino ||
1319 d_mountpoint(dentry))) {
1320 if (!desc->plus || entry->fh->size == 0)
1321 return dentry;
1322 if (nfs_compare_fh(NFS_FH(dentry->d_inode),
1323 entry->fh) == 0)
1324 goto out_renew;
1325 }
Trond Myklebustdf1d5d22007-01-15 13:56:29 -05001326 /* No, so d_drop to allow one to be created */
1327 d_drop(dentry);
1328 dput(dentry);
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
1331 return NULL;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001332 if (name.len > NFS_SERVER(dir)->namelen)
1333 return NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001334 /* Note: caller is already holding the dir->i_mutex! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 dentry = d_alloc(parent, &name);
1336 if (dentry == NULL)
1337 return NULL;
1338 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1339 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001340 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 dput(dentry);
1342 return NULL;
1343 }
David Howells54ceac42006-08-22 20:06:13 -04001344
1345 alias = d_materialise_unique(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (alias != NULL) {
1347 dput(dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001348 if (IS_ERR(alias))
1349 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 dentry = alias;
1351 }
David Howells54ceac42006-08-22 20:06:13 -04001352
Trond Myklebustc79ba782007-01-31 08:16:24 -05001353out_renew:
Trond Myklebust57fa76f2007-09-30 18:01:13 -04001354 nfs_set_verifier(dentry, verf);
Trond Myklebustc79ba782007-01-31 08:16:24 -05001355 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358/*
1359 * Code common to create, mkdir, and mknod.
1360 */
1361int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1362 struct nfs_fattr *fattr)
1363{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001364 struct dentry *parent = dget_parent(dentry);
1365 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct inode *inode;
1367 int error = -EACCES;
1368
Trond Myklebustfab728e2007-09-29 17:41:33 -04001369 d_drop(dentry);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* We may have been initialized further down */
1372 if (dentry->d_inode)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (fhandle->size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1376 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001377 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001379 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1381 struct nfs_server *server = NFS_SB(dentry->d_sb);
David Howells8fa5c002006-08-22 20:06:12 -04001382 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001384 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001387 error = PTR_ERR(inode);
1388 if (IS_ERR(inode))
Trond Myklebustfab728e2007-09-29 17:41:33 -04001389 goto out_error;
1390 d_add(dentry, inode);
1391out:
1392 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 0;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001394out_error:
1395 nfs_mark_for_revalidate(dir);
1396 dput(parent);
1397 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400/*
1401 * Following a failed create operation, we drop the dentry rather
1402 * than retain a negative dentry. This avoids a problem in the event
1403 * that the operation succeeded on the server, but an error in the
1404 * reply path made it appear to have failed.
1405 */
1406static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1407 struct nameidata *nd)
1408{
1409 struct iattr attr;
1410 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001412 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1413 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 attr.ia_mode = mode;
1416 attr.ia_valid = ATTR_MODE;
1417
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001418 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (error != 0)
1420 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return 0;
1422out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 d_drop(dentry);
1424 return error;
1425}
1426
1427/*
1428 * See comments for nfs_proc_create regarding failed operations.
1429 */
1430static int
1431nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1432{
1433 struct iattr attr;
1434 int status;
1435
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001436 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1437 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 if (!new_valid_dev(rdev))
1440 return -EINVAL;
1441
1442 attr.ia_mode = mode;
1443 attr.ia_valid = ATTR_MODE;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (status != 0)
1447 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return 0;
1449out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 d_drop(dentry);
1451 return status;
1452}
1453
1454/*
1455 * See comments for nfs_proc_create regarding failed operations.
1456 */
1457static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1458{
1459 struct iattr attr;
1460 int error;
1461
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001462 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1463 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 attr.ia_valid = ATTR_MODE;
1466 attr.ia_mode = mode | S_IFDIR;
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (error != 0)
1470 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 0;
1472out_err:
1473 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return error;
1475}
1476
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001477static void nfs_dentry_handle_enoent(struct dentry *dentry)
1478{
1479 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1480 d_delete(dentry);
1481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1484{
1485 int error;
1486
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001487 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1488 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1491 /* Ensure the VFS deletes this inode */
1492 if (error == 0 && dentry->d_inode != NULL)
Dave Hansence71ec32006-09-30 23:29:06 -07001493 clear_nlink(dentry->d_inode);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001494 else if (error == -ENOENT)
1495 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 return error;
1498}
1499
1500static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
1501{
1502 static unsigned int sillycounter;
Peter Staubach4e769b92007-08-03 15:07:10 -04001503 const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 const int countersize = sizeof(sillycounter)*2;
Peter Staubach4e769b92007-08-03 15:07:10 -04001505 const int slen = sizeof(".nfs")+fileidsize+countersize-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 char silly[slen+1];
1507 struct qstr qsilly;
1508 struct dentry *sdentry;
1509 int error = -EIO;
1510
1511 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
1512 dentry->d_parent->d_name.name, dentry->d_name.name,
1513 atomic_read(&dentry->d_count));
Chuck Lever91d5b472006-03-20 13:44:14 -05001514 nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * We don't allow a dentry to be silly-renamed twice.
1518 */
1519 error = -EBUSY;
1520 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1521 goto out;
1522
Peter Staubach4e769b92007-08-03 15:07:10 -04001523 sprintf(silly, ".nfs%*.*Lx",
1524 fileidsize, fileidsize,
1525 (unsigned long long)NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Trond Myklebust34ea81882005-11-04 15:35:02 -05001527 /* Return delegation in anticipation of the rename */
1528 nfs_inode_return_delegation(dentry->d_inode);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 sdentry = NULL;
1531 do {
1532 char *suffix = silly + slen - countersize;
1533
1534 dput(sdentry);
1535 sillycounter++;
1536 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
1537
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001538 dfprintk(VFS, "NFS: trying to rename %s to %s\n",
1539 dentry->d_name.name, silly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
1542 /*
1543 * N.B. Better to return EBUSY here ... it could be
1544 * dangerous to delete the file while it's in use.
1545 */
1546 if (IS_ERR(sdentry))
1547 goto out;
1548 } while(sdentry->d_inode != NULL); /* need negative lookup */
1549
1550 qsilly.name = silly;
1551 qsilly.len = strlen(silly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1554 dir, &qsilly);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001555 nfs_mark_for_revalidate(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else
1557 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1558 dir, &qsilly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1561 d_move(dentry, sdentry);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001562 error = nfs_async_unlink(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /* If we return 0 we don't unlink */
1564 }
1565 dput(sdentry);
1566out:
1567 return error;
1568}
1569
1570/*
1571 * Remove a file after making sure there are no pending writes,
1572 * and after checking that the file has only one user.
1573 *
1574 * We invalidate the attribute cache and free the inode prior to the operation
1575 * to avoid possible races if the server reuses the inode.
1576 */
1577static int nfs_safe_remove(struct dentry *dentry)
1578{
1579 struct inode *dir = dentry->d_parent->d_inode;
1580 struct inode *inode = dentry->d_inode;
1581 int error = -EBUSY;
1582
1583 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1584 dentry->d_parent->d_name.name, dentry->d_name.name);
1585
1586 /* If the dentry was sillyrenamed, we simply call d_delete() */
1587 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1588 error = 0;
1589 goto out;
1590 }
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (inode != NULL) {
Trond Myklebustcae7a072005-10-18 14:20:19 -07001593 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1595 /* The VFS may want to delete this inode */
1596 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04001597 nfs_drop_nlink(inode);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001598 nfs_mark_for_revalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 } else
1600 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001601 if (error == -ENOENT)
1602 nfs_dentry_handle_enoent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603out:
1604 return error;
1605}
1606
1607/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1608 * belongs to an active ".nfs..." file and we return -EBUSY.
1609 *
1610 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1611 */
1612static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1613{
1614 int error;
1615 int need_rehash = 0;
1616
1617 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1618 dir->i_ino, dentry->d_name.name);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 spin_lock(&dcache_lock);
1621 spin_lock(&dentry->d_lock);
1622 if (atomic_read(&dentry->d_count) > 1) {
1623 spin_unlock(&dentry->d_lock);
1624 spin_unlock(&dcache_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05001625 /* Start asynchronous writeout of the inode */
1626 write_inode_now(dentry->d_inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 error = nfs_sillyrename(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return error;
1629 }
1630 if (!d_unhashed(dentry)) {
1631 __d_drop(dentry);
1632 need_rehash = 1;
1633 }
1634 spin_unlock(&dentry->d_lock);
1635 spin_unlock(&dcache_lock);
1636 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001637 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1639 } else if (need_rehash)
1640 d_rehash(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return error;
1642}
1643
Chuck Lever873101b2006-08-22 20:06:23 -04001644/*
1645 * To create a symbolic link, most file systems instantiate a new inode,
1646 * add a page to it containing the path, then write it out to the disk
1647 * using prepare_write/commit_write.
1648 *
1649 * Unfortunately the NFS client can't create the in-core inode first
1650 * because it needs a file handle to create an in-core inode (see
1651 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1652 * symlink request has completed on the server.
1653 *
1654 * So instead we allocate a raw page, copy the symname into it, then do
1655 * the SYMLINK request with the page as the buffer. If it succeeds, we
1656 * now have a new file handle and can instantiate an in-core NFS inode
1657 * and move the raw page into its mapping.
1658 */
1659static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Chuck Lever873101b2006-08-22 20:06:23 -04001661 struct pagevec lru_pvec;
1662 struct page *page;
1663 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04001665 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int error;
1667
1668 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1669 dir->i_ino, dentry->d_name.name, symname);
1670
Chuck Lever873101b2006-08-22 20:06:23 -04001671 if (pathlen > PAGE_SIZE)
1672 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Chuck Lever873101b2006-08-22 20:06:23 -04001674 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1675 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jeff Layton83d93f22007-06-07 09:58:08 -04001677 page = alloc_page(GFP_HIGHUSER);
Trond Myklebust76566992008-06-11 15:44:22 -04001678 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04001679 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04001680
1681 kaddr = kmap_atomic(page, KM_USER0);
1682 memcpy(kaddr, symname, pathlen);
1683 if (pathlen < PAGE_SIZE)
1684 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1685 kunmap_atomic(kaddr, KM_USER0);
1686
Chuck Lever94a6d752006-08-22 20:06:23 -04001687 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Chuck Lever873101b2006-08-22 20:06:23 -04001688 if (error != 0) {
1689 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1690 dir->i_sb->s_id, dir->i_ino,
1691 dentry->d_name.name, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04001693 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04001694 return error;
1695 }
1696
1697 /*
1698 * No big deal if we can't add this page to the page cache here.
1699 * READLINK will get the missing page from the server if needed.
1700 */
1701 pagevec_init(&lru_pvec, 0);
1702 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1703 GFP_KERNEL)) {
Chuck Lever39cf8a12006-10-19 23:28:41 -07001704 pagevec_add(&lru_pvec, page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001705 pagevec_lru_add_file(&lru_pvec);
Chuck Lever873101b2006-08-22 20:06:23 -04001706 SetPageUptodate(page);
1707 unlock_page(page);
1708 } else
1709 __free_page(page);
1710
Chuck Lever873101b2006-08-22 20:06:23 -04001711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714static int
1715nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1716{
1717 struct inode *inode = old_dentry->d_inode;
1718 int error;
1719
1720 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1721 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1722 dentry->d_parent->d_name.name, dentry->d_name.name);
1723
Trond Myklebust9a3936a2009-10-26 08:09:46 -04001724 nfs_inode_return_delegation(inode);
1725
Trond Myklebust9697d232007-10-02 21:58:05 -04001726 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04001728 if (error == 0) {
1729 atomic_inc(&inode->i_count);
Trond Myklebust9697d232007-10-02 21:58:05 -04001730 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return error;
1733}
1734
1735/*
1736 * RENAME
1737 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1738 * different file handle for the same inode after a rename (e.g. when
1739 * moving to a different directory). A fail-safe method to do so would
1740 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1741 * rename the old file using the sillyrename stuff. This way, the original
1742 * file in old_dir will go away when the last process iput()s the inode.
1743 *
1744 * FIXED.
1745 *
1746 * It actually works quite well. One needs to have the possibility for
1747 * at least one ".nfs..." file in each directory the file ever gets
1748 * moved or linked to which happens automagically with the new
1749 * implementation that only depends on the dcache stuff instead of
1750 * using the inode layer
1751 *
1752 * Unfortunately, things are a little more complicated than indicated
1753 * above. For a cross-directory move, we want to make sure we can get
1754 * rid of the old inode after the operation. This means there must be
1755 * no pending writes (if it's a file), and the use count must be 1.
1756 * If these conditions are met, we can drop the dentries before doing
1757 * the rename.
1758 */
1759static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1760 struct inode *new_dir, struct dentry *new_dentry)
1761{
1762 struct inode *old_inode = old_dentry->d_inode;
1763 struct inode *new_inode = new_dentry->d_inode;
1764 struct dentry *dentry = NULL, *rehash = NULL;
1765 int error = -EBUSY;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1768 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1769 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1770 atomic_read(&new_dentry->d_count));
1771
1772 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05001773 * For non-directories, check whether the target is busy and if so,
1774 * make a copy of the dentry and then do a silly-rename. If the
1775 * silly-rename succeeds, the copied dentry is hashed and becomes
1776 * the new target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Miklos Szeredi27226102009-12-03 15:58:56 -05001778 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1779 /*
1780 * To prevent any new references to the target during the
1781 * rename, we unhash the dentry in advance.
1782 */
1783 if (!d_unhashed(new_dentry)) {
1784 d_drop(new_dentry);
1785 rehash = new_dentry;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Miklos Szeredi27226102009-12-03 15:58:56 -05001788 if (atomic_read(&new_dentry->d_count) > 2) {
1789 int err;
1790
1791 /* copy the target dentry's name */
1792 dentry = d_alloc(new_dentry->d_parent,
1793 &new_dentry->d_name);
1794 if (!dentry)
1795 goto out;
1796
1797 /* silly-rename the existing target ... */
1798 err = nfs_sillyrename(new_dir, new_dentry);
Miklos Szeredi24e93022009-12-03 15:58:56 -05001799 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05001800 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001801
1802 new_dentry = dentry;
OGAWA Hirofumi56335932010-01-06 18:48:26 -05001803 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05001804 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05001805 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Trond Myklebustcae7a072005-10-18 14:20:19 -07001808 nfs_inode_return_delegation(old_inode);
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001809 if (new_inode != NULL)
Trond Myklebust24174112006-01-03 09:55:33 +01001810 nfs_inode_return_delegation(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1813 new_dir, &new_dentry->d_name);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -05001814 nfs_mark_for_revalidate(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815out:
1816 if (rehash)
1817 d_rehash(rehash);
1818 if (!error) {
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04001819 if (new_inode != NULL)
1820 nfs_drop_nlink(new_inode);
Mark Fasheh349457c2006-09-08 14:22:21 -07001821 d_move(old_dentry, new_dentry);
Chuck Lever8fb559f2007-09-24 15:40:16 -04001822 nfs_set_verifier(new_dentry,
1823 nfs_save_change_attribute(new_dir));
Trond Myklebustd45b9d82008-01-28 19:43:18 -05001824 } else if (error == -ENOENT)
1825 nfs_dentry_handle_enoent(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /* new dentry created? */
1828 if (dentry)
1829 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return error;
1831}
1832
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001833static DEFINE_SPINLOCK(nfs_access_lru_lock);
1834static LIST_HEAD(nfs_access_lru_list);
1835static atomic_long_t nfs_access_nr_entries;
1836
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001837static void nfs_access_free_entry(struct nfs_access_entry *entry)
1838{
1839 put_rpccred(entry->cred);
1840 kfree(entry);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001841 smp_mb__before_atomic_dec();
1842 atomic_long_dec(&nfs_access_nr_entries);
1843 smp_mb__after_atomic_dec();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001844}
1845
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001846static void nfs_access_free_list(struct list_head *head)
1847{
1848 struct nfs_access_entry *cache;
1849
1850 while (!list_empty(head)) {
1851 cache = list_entry(head->next, struct nfs_access_entry, lru);
1852 list_del(&cache->lru);
1853 nfs_access_free_entry(cache);
1854 }
1855}
1856
Dave Chinner7f8275d2010-07-19 14:56:17 +10001857int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebust979df722006-07-25 11:28:19 -04001858{
1859 LIST_HEAD(head);
1860 struct nfs_inode *nfsi;
1861 struct nfs_access_entry *cache;
1862
Trond Myklebust61d5eb22010-05-13 12:51:06 -04001863 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1864 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001865
Trond Myklebusta50f7952007-06-05 19:23:43 -04001866 spin_lock(&nfs_access_lru_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04001867 list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
1868 struct inode *inode;
1869
1870 if (nr_to_scan-- == 0)
1871 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001872 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04001873 spin_lock(&inode->i_lock);
1874 if (list_empty(&nfsi->access_cache_entry_lru))
1875 goto remove_lru_entry;
1876 cache = list_entry(nfsi->access_cache_entry_lru.next,
1877 struct nfs_access_entry, lru);
1878 list_move(&cache->lru, &head);
1879 rb_erase(&cache->rb_node, &nfsi->access_cache);
1880 if (!list_empty(&nfsi->access_cache_entry_lru))
1881 list_move_tail(&nfsi->access_cache_inode_lru,
1882 &nfs_access_lru_list);
1883 else {
1884remove_lru_entry:
1885 list_del_init(&nfsi->access_cache_inode_lru);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001886 smp_mb__before_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001887 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04001888 smp_mb__after_clear_bit();
Trond Myklebust979df722006-07-25 11:28:19 -04001889 }
Trond Myklebust59844a92010-05-26 08:42:24 -04001890 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04001891 }
1892 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001893 nfs_access_free_list(&head);
Trond Myklebust979df722006-07-25 11:28:19 -04001894 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1895}
1896
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001897static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001898{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001899 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001900 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001901 struct nfs_access_entry *entry;
1902
1903 /* Unhook entries from the cache */
1904 while ((n = rb_first(root_node)) != NULL) {
1905 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1906 rb_erase(n, root_node);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001907 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001908 }
1909 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001910}
1911
1912void nfs_access_zap_cache(struct inode *inode)
1913{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001914 LIST_HEAD(head);
1915
1916 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
1917 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001918 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001919 spin_lock(&nfs_access_lru_lock);
1920 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001921 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001922
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001923 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001924 __nfs_access_zap_cache(NFS_I(inode), &head);
1925 spin_unlock(&inode->i_lock);
1926 spin_unlock(&nfs_access_lru_lock);
1927 nfs_access_free_list(&head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001928}
1929
1930static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
1931{
1932 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
1933 struct nfs_access_entry *entry;
1934
1935 while (n != NULL) {
1936 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1937
1938 if (cred < entry->cred)
1939 n = n->rb_left;
1940 else if (cred > entry->cred)
1941 n = n->rb_right;
1942 else
1943 return entry;
1944 }
1945 return NULL;
1946}
1947
Trond Myklebustaf22f942007-08-10 17:45:10 -04001948static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Chuck Lever55296802005-08-18 11:24:09 -07001950 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001951 struct nfs_access_entry *cache;
1952 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001954 spin_lock(&inode->i_lock);
1955 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
1956 goto out_zap;
1957 cache = nfs_access_search_rbtree(inode, cred);
1958 if (cache == NULL)
1959 goto out;
Trond Myklebustb4d23142010-03-10 15:21:44 -05001960 if (!nfs_have_delegated_attributes(inode) &&
Peter Staubach64672d52008-12-23 15:21:56 -05001961 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001962 goto out_stale;
1963 res->jiffies = cache->jiffies;
1964 res->cred = cache->cred;
1965 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001966 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001967 err = 0;
1968out:
1969 spin_unlock(&inode->i_lock);
1970 return err;
1971out_stale:
1972 rb_erase(&cache->rb_node, &nfsi->access_cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001973 list_del(&cache->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001974 spin_unlock(&inode->i_lock);
1975 nfs_access_free_entry(cache);
1976 return -ENOENT;
1977out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04001978 spin_unlock(&inode->i_lock);
1979 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001980 return -ENOENT;
1981}
1982
1983static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
1984{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04001985 struct nfs_inode *nfsi = NFS_I(inode);
1986 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04001987 struct rb_node **p = &root_node->rb_node;
1988 struct rb_node *parent = NULL;
1989 struct nfs_access_entry *entry;
1990
1991 spin_lock(&inode->i_lock);
1992 while (*p != NULL) {
1993 parent = *p;
1994 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
1995
1996 if (set->cred < entry->cred)
1997 p = &parent->rb_left;
1998 else if (set->cred > entry->cred)
1999 p = &parent->rb_right;
2000 else
2001 goto found;
2002 }
2003 rb_link_node(&set->rb_node, parent, p);
2004 rb_insert_color(&set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002005 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002006 spin_unlock(&inode->i_lock);
2007 return;
2008found:
2009 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002010 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2011 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002012 spin_unlock(&inode->i_lock);
2013 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Trond Myklebustaf22f942007-08-10 17:45:10 -04002016static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002018 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2019 if (cache == NULL)
2020 return;
2021 RB_CLEAR_NODE(&cache->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 cache->jiffies = set->jiffies;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002023 cache->cred = get_rpccred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002025
2026 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002027
2028 /* Update accounting */
2029 smp_mb__before_atomic_inc();
2030 atomic_long_inc(&nfs_access_nr_entries);
2031 smp_mb__after_atomic_inc();
2032
2033 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002034 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002035 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002036 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2037 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2038 &nfs_access_lru_list);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002039 spin_unlock(&nfs_access_lru_lock);
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2044{
2045 struct nfs_access_entry cache;
2046 int status;
2047
2048 status = nfs_access_get_cached(inode, cred, &cache);
2049 if (status == 0)
2050 goto out;
2051
2052 /* Be clever: ask server to check for all possible rights */
2053 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2054 cache.cred = cred;
2055 cache.jiffies = jiffies;
2056 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002057 if (status != 0) {
2058 if (status == -ESTALE) {
2059 nfs_zap_caches(inode);
2060 if (!S_ISDIR(inode->i_mode))
2061 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return status;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 nfs_access_add_cache(inode, &cache);
2066out:
Al Viroe6305c42008-07-15 21:03:57 -04002067 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return 0;
2069 return -EACCES;
2070}
2071
Trond Myklebustaf22f942007-08-10 17:45:10 -04002072static int nfs_open_permission_mask(int openflags)
2073{
2074 int mask = 0;
2075
2076 if (openflags & FMODE_READ)
2077 mask |= MAY_READ;
2078 if (openflags & FMODE_WRITE)
2079 mask |= MAY_WRITE;
2080 if (openflags & FMODE_EXEC)
2081 mask |= MAY_EXEC;
2082 return mask;
2083}
2084
2085int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2086{
2087 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2088}
2089
Al Viroe6305c42008-07-15 21:03:57 -04002090int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct rpc_cred *cred;
2093 int res = 0;
2094
Chuck Lever91d5b472006-03-20 13:44:14 -05002095 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2096
Al Viroe6305c42008-07-15 21:03:57 -04002097 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto out;
2099 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002100 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto force_lookup;
2102
2103 switch (inode->i_mode & S_IFMT) {
2104 case S_IFLNK:
2105 goto out;
2106 case S_IFREG:
2107 /* NFSv4 has atomic_open... */
2108 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
Frank Filz7ee2cb72009-05-18 17:41:40 -04002109 && (mask & MAY_OPEN)
2110 && !(mask & MAY_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 goto out;
2112 break;
2113 case S_IFDIR:
2114 /*
2115 * Optimize away all write operations, since the server
2116 * will check permissions when we perform the op.
2117 */
2118 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2119 goto out;
2120 }
2121
2122force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (!NFS_PROTO(inode)->access)
2124 goto out_notsup;
2125
Trond Myklebust98a8e322008-03-12 12:25:28 -04002126 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (!IS_ERR(cred)) {
2128 res = nfs_do_access(inode, cred, mask);
2129 put_rpccred(cred);
2130 } else
2131 res = PTR_ERR(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132out:
Miklos Szeredif696a362008-07-31 13:41:58 +02002133 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2134 res = -EACCES;
2135
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002136 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2137 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return res;
2139out_notsup:
2140 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2141 if (res == 0)
2142 res = generic_permission(inode, mask, NULL);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/*
2147 * Local variables:
2148 * version-control: t
2149 * kept-new-versions: 5
2150 * End:
2151 */