dir.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*
  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. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/namei.h>
  33. #include "nfs4_fs.h"
  34. #include "delegation.h"
  35. #define NFS_PARANOIA 1
  36. /* #define NFS_DEBUG_VERBOSE 1 */
  37. static int nfs_opendir(struct inode *, struct file *);
  38. static int nfs_readdir(struct file *, void *, filldir_t);
  39. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  40. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  41. static int nfs_mkdir(struct inode *, struct dentry *, int);
  42. static int nfs_rmdir(struct inode *, struct dentry *);
  43. static int nfs_unlink(struct inode *, struct dentry *);
  44. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  45. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  46. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  47. static int nfs_rename(struct inode *, struct dentry *,
  48. struct inode *, struct dentry *);
  49. static int nfs_fsync_dir(struct file *, struct dentry *, int);
  50. struct file_operations nfs_dir_operations = {
  51. .read = generic_read_dir,
  52. .readdir = nfs_readdir,
  53. .open = nfs_opendir,
  54. .release = nfs_release,
  55. .fsync = nfs_fsync_dir,
  56. };
  57. struct inode_operations nfs_dir_inode_operations = {
  58. .create = nfs_create,
  59. .lookup = nfs_lookup,
  60. .link = nfs_link,
  61. .unlink = nfs_unlink,
  62. .symlink = nfs_symlink,
  63. .mkdir = nfs_mkdir,
  64. .rmdir = nfs_rmdir,
  65. .mknod = nfs_mknod,
  66. .rename = nfs_rename,
  67. .permission = nfs_permission,
  68. .getattr = nfs_getattr,
  69. .setattr = nfs_setattr,
  70. };
  71. #ifdef CONFIG_NFS_V4
  72. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  73. struct inode_operations nfs4_dir_inode_operations = {
  74. .create = nfs_create,
  75. .lookup = nfs_atomic_lookup,
  76. .link = nfs_link,
  77. .unlink = nfs_unlink,
  78. .symlink = nfs_symlink,
  79. .mkdir = nfs_mkdir,
  80. .rmdir = nfs_rmdir,
  81. .mknod = nfs_mknod,
  82. .rename = nfs_rename,
  83. .permission = nfs_permission,
  84. .getattr = nfs_getattr,
  85. .setattr = nfs_setattr,
  86. .getxattr = nfs4_getxattr,
  87. .setxattr = nfs4_setxattr,
  88. .listxattr = nfs4_listxattr,
  89. };
  90. #endif /* CONFIG_NFS_V4 */
  91. /*
  92. * Open file
  93. */
  94. static int
  95. nfs_opendir(struct inode *inode, struct file *filp)
  96. {
  97. int res = 0;
  98. lock_kernel();
  99. /* Call generic open code in order to cache credentials */
  100. if (!res)
  101. res = nfs_open(inode, filp);
  102. unlock_kernel();
  103. return res;
  104. }
  105. typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
  106. typedef struct {
  107. struct file *file;
  108. struct page *page;
  109. unsigned long page_index;
  110. u32 *ptr;
  111. u64 target;
  112. struct nfs_entry *entry;
  113. decode_dirent_t decode;
  114. int plus;
  115. int error;
  116. } nfs_readdir_descriptor_t;
  117. /* Now we cache directories properly, by stuffing the dirent
  118. * data directly in the page cache.
  119. *
  120. * Inode invalidation due to refresh etc. takes care of
  121. * _everything_, no sloppy entry flushing logic, no extraneous
  122. * copying, network direct to page cache, the way it was meant
  123. * to be.
  124. *
  125. * NOTE: Dirent information verification is done always by the
  126. * page-in of the RPC reply, nowhere else, this simplies
  127. * things substantially.
  128. */
  129. static
  130. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  131. {
  132. struct file *file = desc->file;
  133. struct inode *inode = file->f_dentry->d_inode;
  134. struct rpc_cred *cred = nfs_file_cred(file);
  135. unsigned long timestamp;
  136. int error;
  137. dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
  138. again:
  139. timestamp = jiffies;
  140. error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
  141. NFS_SERVER(inode)->dtsize, desc->plus);
  142. if (error < 0) {
  143. /* We requested READDIRPLUS, but the server doesn't grok it */
  144. if (error == -ENOTSUPP && desc->plus) {
  145. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  146. NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
  147. desc->plus = 0;
  148. goto again;
  149. }
  150. goto error;
  151. }
  152. SetPageUptodate(page);
  153. NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
  154. /* Ensure consistent page alignment of the data.
  155. * Note: assumes we have exclusive access to this mapping either
  156. * through inode->i_sem or some other mechanism.
  157. */
  158. if (page->index == 0)
  159. invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1);
  160. unlock_page(page);
  161. return 0;
  162. error:
  163. SetPageError(page);
  164. unlock_page(page);
  165. nfs_zap_caches(inode);
  166. desc->error = error;
  167. return -EIO;
  168. }
  169. static inline
  170. int dir_decode(nfs_readdir_descriptor_t *desc)
  171. {
  172. u32 *p = desc->ptr;
  173. p = desc->decode(p, desc->entry, desc->plus);
  174. if (IS_ERR(p))
  175. return PTR_ERR(p);
  176. desc->ptr = p;
  177. return 0;
  178. }
  179. static inline
  180. void dir_page_release(nfs_readdir_descriptor_t *desc)
  181. {
  182. kunmap(desc->page);
  183. page_cache_release(desc->page);
  184. desc->page = NULL;
  185. desc->ptr = NULL;
  186. }
  187. /*
  188. * Given a pointer to a buffer that has already been filled by a call
  189. * to readdir, find the next entry.
  190. *
  191. * If the end of the buffer has been reached, return -EAGAIN, if not,
  192. * return the offset within the buffer of the next entry to be
  193. * read.
  194. */
  195. static inline
  196. int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
  197. {
  198. struct nfs_entry *entry = desc->entry;
  199. int loop_count = 0,
  200. status;
  201. while((status = dir_decode(desc)) == 0) {
  202. dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
  203. if (entry->prev_cookie == desc->target)
  204. break;
  205. if (loop_count++ > 200) {
  206. loop_count = 0;
  207. schedule();
  208. }
  209. }
  210. dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
  211. return status;
  212. }
  213. /*
  214. * Find the given page, and call find_dirent() in order to try to
  215. * return the next entry.
  216. */
  217. static inline
  218. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  219. {
  220. struct inode *inode = desc->file->f_dentry->d_inode;
  221. struct page *page;
  222. int status;
  223. dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
  224. page = read_cache_page(inode->i_mapping, desc->page_index,
  225. (filler_t *)nfs_readdir_filler, desc);
  226. if (IS_ERR(page)) {
  227. status = PTR_ERR(page);
  228. goto out;
  229. }
  230. if (!PageUptodate(page))
  231. goto read_error;
  232. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  233. desc->page = page;
  234. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  235. status = find_dirent(desc, page);
  236. if (status < 0)
  237. dir_page_release(desc);
  238. out:
  239. dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
  240. return status;
  241. read_error:
  242. page_cache_release(page);
  243. return -EIO;
  244. }
  245. /*
  246. * Recurse through the page cache pages, and return a
  247. * filled nfs_entry structure of the next directory entry if possible.
  248. *
  249. * The target for the search is 'desc->target'.
  250. */
  251. static inline
  252. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  253. {
  254. int loop_count = 0;
  255. int res;
  256. dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
  257. for (;;) {
  258. res = find_dirent_page(desc);
  259. if (res != -EAGAIN)
  260. break;
  261. /* Align to beginning of next page */
  262. desc->page_index ++;
  263. if (loop_count++ > 200) {
  264. loop_count = 0;
  265. schedule();
  266. }
  267. }
  268. dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
  269. return res;
  270. }
  271. static inline unsigned int dt_type(struct inode *inode)
  272. {
  273. return (inode->i_mode >> 12) & 15;
  274. }
  275. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  276. /*
  277. * Once we've found the start of the dirent within a page: fill 'er up...
  278. */
  279. static
  280. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  281. filldir_t filldir)
  282. {
  283. struct file *file = desc->file;
  284. struct nfs_entry *entry = desc->entry;
  285. struct dentry *dentry = NULL;
  286. unsigned long fileid;
  287. int loop_count = 0,
  288. res;
  289. dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
  290. for(;;) {
  291. unsigned d_type = DT_UNKNOWN;
  292. /* Note: entry->prev_cookie contains the cookie for
  293. * retrieving the current dirent on the server */
  294. fileid = nfs_fileid_to_ino_t(entry->ino);
  295. /* Get a dentry if we have one */
  296. if (dentry != NULL)
  297. dput(dentry);
  298. dentry = nfs_readdir_lookup(desc);
  299. /* Use readdirplus info */
  300. if (dentry != NULL && dentry->d_inode != NULL) {
  301. d_type = dt_type(dentry->d_inode);
  302. fileid = dentry->d_inode->i_ino;
  303. }
  304. res = filldir(dirent, entry->name, entry->len,
  305. entry->prev_cookie, fileid, d_type);
  306. if (res < 0)
  307. break;
  308. file->f_pos = desc->target = entry->cookie;
  309. if (dir_decode(desc) != 0) {
  310. desc->page_index ++;
  311. break;
  312. }
  313. if (loop_count++ > 200) {
  314. loop_count = 0;
  315. schedule();
  316. }
  317. }
  318. dir_page_release(desc);
  319. if (dentry != NULL)
  320. dput(dentry);
  321. dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
  322. return res;
  323. }
  324. /*
  325. * If we cannot find a cookie in our cache, we suspect that this is
  326. * because it points to a deleted file, so we ask the server to return
  327. * whatever it thinks is the next entry. We then feed this to filldir.
  328. * If all goes well, we should then be able to find our way round the
  329. * cache on the next call to readdir_search_pagecache();
  330. *
  331. * NOTE: we cannot add the anonymous page to the pagecache because
  332. * the data it contains might not be page aligned. Besides,
  333. * we should already have a complete representation of the
  334. * directory in the page cache by the time we get here.
  335. */
  336. static inline
  337. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  338. filldir_t filldir)
  339. {
  340. struct file *file = desc->file;
  341. struct inode *inode = file->f_dentry->d_inode;
  342. struct rpc_cred *cred = nfs_file_cred(file);
  343. struct page *page = NULL;
  344. int status;
  345. dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
  346. page = alloc_page(GFP_HIGHUSER);
  347. if (!page) {
  348. status = -ENOMEM;
  349. goto out;
  350. }
  351. desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target,
  352. page,
  353. NFS_SERVER(inode)->dtsize,
  354. desc->plus);
  355. NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
  356. desc->page = page;
  357. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  358. if (desc->error >= 0) {
  359. if ((status = dir_decode(desc)) == 0)
  360. desc->entry->prev_cookie = desc->target;
  361. } else
  362. status = -EIO;
  363. if (status < 0)
  364. goto out_release;
  365. status = nfs_do_filldir(desc, dirent, filldir);
  366. /* Reset read descriptor so it searches the page cache from
  367. * the start upon the next call to readdir_search_pagecache() */
  368. desc->page_index = 0;
  369. desc->entry->cookie = desc->entry->prev_cookie = 0;
  370. desc->entry->eof = 0;
  371. out:
  372. dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
  373. return status;
  374. out_release:
  375. dir_page_release(desc);
  376. goto out;
  377. }
  378. /* The file offset position is now represented as a true offset into the
  379. * page cache as is the case in most of the other filesystems.
  380. */
  381. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  382. {
  383. struct dentry *dentry = filp->f_dentry;
  384. struct inode *inode = dentry->d_inode;
  385. nfs_readdir_descriptor_t my_desc,
  386. *desc = &my_desc;
  387. struct nfs_entry my_entry;
  388. struct nfs_fh fh;
  389. struct nfs_fattr fattr;
  390. long res;
  391. lock_kernel();
  392. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  393. if (res < 0) {
  394. unlock_kernel();
  395. return res;
  396. }
  397. /*
  398. * filp->f_pos points to the file offset in the page cache.
  399. * but if the cache has meanwhile been zapped, we need to
  400. * read from the last dirent to revalidate f_pos
  401. * itself.
  402. */
  403. memset(desc, 0, sizeof(*desc));
  404. desc->file = filp;
  405. desc->target = filp->f_pos;
  406. desc->decode = NFS_PROTO(inode)->decode_dirent;
  407. desc->plus = NFS_USE_READDIRPLUS(inode);
  408. my_entry.cookie = my_entry.prev_cookie = 0;
  409. my_entry.eof = 0;
  410. my_entry.fh = &fh;
  411. my_entry.fattr = &fattr;
  412. desc->entry = &my_entry;
  413. while(!desc->entry->eof) {
  414. res = readdir_search_pagecache(desc);
  415. if (res == -EBADCOOKIE) {
  416. /* This means either end of directory */
  417. if (desc->entry->cookie != desc->target) {
  418. /* Or that the server has 'lost' a cookie */
  419. res = uncached_readdir(desc, dirent, filldir);
  420. if (res >= 0)
  421. continue;
  422. }
  423. res = 0;
  424. break;
  425. }
  426. if (res == -ETOOSMALL && desc->plus) {
  427. NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
  428. nfs_zap_caches(inode);
  429. desc->plus = 0;
  430. desc->entry->eof = 0;
  431. continue;
  432. }
  433. if (res < 0)
  434. break;
  435. res = nfs_do_filldir(desc, dirent, filldir);
  436. if (res < 0) {
  437. res = 0;
  438. break;
  439. }
  440. }
  441. unlock_kernel();
  442. if (desc->error < 0)
  443. return desc->error;
  444. if (res < 0)
  445. return res;
  446. return 0;
  447. }
  448. /*
  449. * All directory operations under NFS are synchronous, so fsync()
  450. * is a dummy operation.
  451. */
  452. int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
  453. {
  454. return 0;
  455. }
  456. /*
  457. * A check for whether or not the parent directory has changed.
  458. * In the case it has, we assume that the dentries are untrustworthy
  459. * and may need to be looked up again.
  460. */
  461. static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  462. {
  463. if (IS_ROOT(dentry))
  464. return 1;
  465. if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0
  466. || nfs_attribute_timeout(dir))
  467. return 0;
  468. return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
  469. }
  470. static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
  471. {
  472. dentry->d_fsdata = (void *)verf;
  473. }
  474. /*
  475. * Whenever an NFS operation succeeds, we know that the dentry
  476. * is valid, so we update the revalidation timestamp.
  477. */
  478. static inline void nfs_renew_times(struct dentry * dentry)
  479. {
  480. dentry->d_time = jiffies;
  481. }
  482. /*
  483. * Return the intent data that applies to this particular path component
  484. *
  485. * Note that the current set of intents only apply to the very last
  486. * component of the path.
  487. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  488. */
  489. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  490. {
  491. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  492. return 0;
  493. return nd->flags & mask;
  494. }
  495. /*
  496. * Inode and filehandle revalidation for lookups.
  497. *
  498. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  499. * or if the intent information indicates that we're about to open this
  500. * particular file and the "nocto" mount flag is not set.
  501. *
  502. */
  503. static inline
  504. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  505. {
  506. struct nfs_server *server = NFS_SERVER(inode);
  507. if (nd != NULL) {
  508. /* VFS wants an on-the-wire revalidation */
  509. if (nd->flags & LOOKUP_REVAL)
  510. goto out_force;
  511. /* This is an open(2) */
  512. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  513. !(server->flags & NFS_MOUNT_NOCTO))
  514. goto out_force;
  515. }
  516. return nfs_revalidate_inode(server, inode);
  517. out_force:
  518. return __nfs_revalidate_inode(server, inode);
  519. }
  520. /*
  521. * We judge how long we want to trust negative
  522. * dentries by looking at the parent inode mtime.
  523. *
  524. * If parent mtime has changed, we revalidate, else we wait for a
  525. * period corresponding to the parent's attribute cache timeout value.
  526. */
  527. static inline
  528. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  529. struct nameidata *nd)
  530. {
  531. /* Don't revalidate a negative dentry if we're creating a new file */
  532. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  533. return 0;
  534. return !nfs_check_verifier(dir, dentry);
  535. }
  536. /*
  537. * This is called every time the dcache has a lookup hit,
  538. * and we should check whether we can really trust that
  539. * lookup.
  540. *
  541. * NOTE! The hit can be a negative hit too, don't assume
  542. * we have an inode!
  543. *
  544. * If the parent directory is seen to have changed, we throw out the
  545. * cached dentry and do a new lookup.
  546. */
  547. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  548. {
  549. struct inode *dir;
  550. struct inode *inode;
  551. struct dentry *parent;
  552. int error;
  553. struct nfs_fh fhandle;
  554. struct nfs_fattr fattr;
  555. unsigned long verifier;
  556. parent = dget_parent(dentry);
  557. lock_kernel();
  558. dir = parent->d_inode;
  559. inode = dentry->d_inode;
  560. if (!inode) {
  561. if (nfs_neg_need_reval(dir, dentry, nd))
  562. goto out_bad;
  563. goto out_valid;
  564. }
  565. if (is_bad_inode(inode)) {
  566. dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
  567. dentry->d_parent->d_name.name, dentry->d_name.name);
  568. goto out_bad;
  569. }
  570. /* Revalidate parent directory attribute cache */
  571. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  572. goto out_zap_parent;
  573. /* Force a full look up iff the parent directory has changed */
  574. if (nfs_check_verifier(dir, dentry)) {
  575. if (nfs_lookup_verify_inode(inode, nd))
  576. goto out_zap_parent;
  577. goto out_valid;
  578. }
  579. if (NFS_STALE(inode))
  580. goto out_bad;
  581. verifier = nfs_save_change_attribute(dir);
  582. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  583. if (error)
  584. goto out_bad;
  585. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  586. goto out_bad;
  587. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  588. goto out_bad;
  589. nfs_renew_times(dentry);
  590. nfs_set_verifier(dentry, verifier);
  591. out_valid:
  592. unlock_kernel();
  593. dput(parent);
  594. return 1;
  595. out_zap_parent:
  596. nfs_zap_caches(dir);
  597. out_bad:
  598. NFS_CACHEINV(dir);
  599. if (inode && S_ISDIR(inode->i_mode)) {
  600. /* Purge readdir caches. */
  601. nfs_zap_caches(inode);
  602. /* If we have submounts, don't unhash ! */
  603. if (have_submounts(dentry))
  604. goto out_valid;
  605. shrink_dcache_parent(dentry);
  606. }
  607. d_drop(dentry);
  608. unlock_kernel();
  609. dput(parent);
  610. return 0;
  611. }
  612. /*
  613. * This is called from dput() when d_count is going to 0.
  614. */
  615. static int nfs_dentry_delete(struct dentry *dentry)
  616. {
  617. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  618. dentry->d_parent->d_name.name, dentry->d_name.name,
  619. dentry->d_flags);
  620. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  621. /* Unhash it, so that ->d_iput() would be called */
  622. return 1;
  623. }
  624. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  625. /* Unhash it, so that ancestors of killed async unlink
  626. * files will be cleaned up during umount */
  627. return 1;
  628. }
  629. return 0;
  630. }
  631. /*
  632. * Called when the dentry loses inode.
  633. * We use it to clean up silly-renamed files.
  634. */
  635. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  636. {
  637. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  638. lock_kernel();
  639. inode->i_nlink--;
  640. nfs_complete_unlink(dentry);
  641. unlock_kernel();
  642. }
  643. /* When creating a negative dentry, we want to renew d_time */
  644. nfs_renew_times(dentry);
  645. iput(inode);
  646. }
  647. struct dentry_operations nfs_dentry_operations = {
  648. .d_revalidate = nfs_lookup_revalidate,
  649. .d_delete = nfs_dentry_delete,
  650. .d_iput = nfs_dentry_iput,
  651. };
  652. /*
  653. * Use intent information to check whether or not we're going to do
  654. * an O_EXCL create using this path component.
  655. */
  656. static inline
  657. int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  658. {
  659. if (NFS_PROTO(dir)->version == 2)
  660. return 0;
  661. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
  662. return 0;
  663. return (nd->intent.open.flags & O_EXCL) != 0;
  664. }
  665. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  666. {
  667. struct dentry *res;
  668. struct inode *inode = NULL;
  669. int error;
  670. struct nfs_fh fhandle;
  671. struct nfs_fattr fattr;
  672. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  673. dentry->d_parent->d_name.name, dentry->d_name.name);
  674. res = ERR_PTR(-ENAMETOOLONG);
  675. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  676. goto out;
  677. res = ERR_PTR(-ENOMEM);
  678. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  679. lock_kernel();
  680. /* Revalidate parent directory attribute cache */
  681. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  682. if (error < 0) {
  683. res = ERR_PTR(error);
  684. goto out_unlock;
  685. }
  686. /* If we're doing an exclusive create, optimize away the lookup */
  687. if (nfs_is_exclusive_create(dir, nd))
  688. goto no_entry;
  689. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  690. if (error == -ENOENT)
  691. goto no_entry;
  692. if (error < 0) {
  693. res = ERR_PTR(error);
  694. goto out_unlock;
  695. }
  696. res = ERR_PTR(-EACCES);
  697. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  698. if (!inode)
  699. goto out_unlock;
  700. no_entry:
  701. res = d_add_unique(dentry, inode);
  702. if (res != NULL)
  703. dentry = res;
  704. nfs_renew_times(dentry);
  705. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  706. out_unlock:
  707. unlock_kernel();
  708. out:
  709. return res;
  710. }
  711. #ifdef CONFIG_NFS_V4
  712. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  713. struct dentry_operations nfs4_dentry_operations = {
  714. .d_revalidate = nfs_open_revalidate,
  715. .d_delete = nfs_dentry_delete,
  716. .d_iput = nfs_dentry_iput,
  717. };
  718. /*
  719. * Use intent information to determine whether we need to substitute
  720. * the NFSv4-style stateful OPEN for the LOOKUP call
  721. */
  722. static int is_atomic_open(struct inode *dir, struct nameidata *nd)
  723. {
  724. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  725. return 0;
  726. /* NFS does not (yet) have a stateful open for directories */
  727. if (nd->flags & LOOKUP_DIRECTORY)
  728. return 0;
  729. /* Are we trying to write to a read only partition? */
  730. if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  731. return 0;
  732. return 1;
  733. }
  734. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  735. {
  736. struct dentry *res = NULL;
  737. struct inode *inode = NULL;
  738. int error;
  739. /* Check that we are indeed trying to open this file */
  740. if (!is_atomic_open(dir, nd))
  741. goto no_open;
  742. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  743. res = ERR_PTR(-ENAMETOOLONG);
  744. goto out;
  745. }
  746. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  747. /* Let vfs_create() deal with O_EXCL */
  748. if (nd->intent.open.flags & O_EXCL)
  749. goto no_entry;
  750. /* Open the file on the server */
  751. lock_kernel();
  752. /* Revalidate parent directory attribute cache */
  753. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  754. if (error < 0) {
  755. res = ERR_PTR(error);
  756. goto out;
  757. }
  758. if (nd->intent.open.flags & O_CREAT) {
  759. nfs_begin_data_update(dir);
  760. inode = nfs4_atomic_open(dir, dentry, nd);
  761. nfs_end_data_update(dir);
  762. } else
  763. inode = nfs4_atomic_open(dir, dentry, nd);
  764. unlock_kernel();
  765. if (IS_ERR(inode)) {
  766. error = PTR_ERR(inode);
  767. switch (error) {
  768. /* Make a negative dentry */
  769. case -ENOENT:
  770. inode = NULL;
  771. break;
  772. /* This turned out not to be a regular file */
  773. case -ELOOP:
  774. if (!(nd->intent.open.flags & O_NOFOLLOW))
  775. goto no_open;
  776. /* case -EISDIR: */
  777. /* case -EINVAL: */
  778. default:
  779. res = ERR_PTR(error);
  780. goto out;
  781. }
  782. }
  783. no_entry:
  784. res = d_add_unique(dentry, inode);
  785. if (res != NULL)
  786. dentry = res;
  787. nfs_renew_times(dentry);
  788. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  789. out:
  790. return res;
  791. no_open:
  792. return nfs_lookup(dir, dentry, nd);
  793. }
  794. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  795. {
  796. struct dentry *parent = NULL;
  797. struct inode *inode = dentry->d_inode;
  798. struct inode *dir;
  799. unsigned long verifier;
  800. int openflags, ret = 0;
  801. parent = dget_parent(dentry);
  802. dir = parent->d_inode;
  803. if (!is_atomic_open(dir, nd))
  804. goto no_open;
  805. /* We can't create new files in nfs_open_revalidate(), so we
  806. * optimize away revalidation of negative dentries.
  807. */
  808. if (inode == NULL)
  809. goto out;
  810. /* NFS only supports OPEN on regular files */
  811. if (!S_ISREG(inode->i_mode))
  812. goto no_open;
  813. openflags = nd->intent.open.flags;
  814. /* We cannot do exclusive creation on a positive dentry */
  815. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  816. goto no_open;
  817. /* We can't create new files, or truncate existing ones here */
  818. openflags &= ~(O_CREAT|O_TRUNC);
  819. /*
  820. * Note: we're not holding inode->i_sem and so may be racing with
  821. * operations that change the directory. We therefore save the
  822. * change attribute *before* we do the RPC call.
  823. */
  824. lock_kernel();
  825. verifier = nfs_save_change_attribute(dir);
  826. ret = nfs4_open_revalidate(dir, dentry, openflags);
  827. if (!ret)
  828. nfs_set_verifier(dentry, verifier);
  829. unlock_kernel();
  830. out:
  831. dput(parent);
  832. if (!ret)
  833. d_drop(dentry);
  834. return ret;
  835. no_open:
  836. dput(parent);
  837. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  838. return 1;
  839. return nfs_lookup_revalidate(dentry, nd);
  840. }
  841. #endif /* CONFIG_NFSV4 */
  842. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  843. {
  844. struct dentry *parent = desc->file->f_dentry;
  845. struct inode *dir = parent->d_inode;
  846. struct nfs_entry *entry = desc->entry;
  847. struct dentry *dentry, *alias;
  848. struct qstr name = {
  849. .name = entry->name,
  850. .len = entry->len,
  851. };
  852. struct inode *inode;
  853. switch (name.len) {
  854. case 2:
  855. if (name.name[0] == '.' && name.name[1] == '.')
  856. return dget_parent(parent);
  857. break;
  858. case 1:
  859. if (name.name[0] == '.')
  860. return dget(parent);
  861. }
  862. name.hash = full_name_hash(name.name, name.len);
  863. dentry = d_lookup(parent, &name);
  864. if (dentry != NULL)
  865. return dentry;
  866. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  867. return NULL;
  868. /* Note: caller is already holding the dir->i_sem! */
  869. dentry = d_alloc(parent, &name);
  870. if (dentry == NULL)
  871. return NULL;
  872. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  873. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  874. if (!inode) {
  875. dput(dentry);
  876. return NULL;
  877. }
  878. alias = d_add_unique(dentry, inode);
  879. if (alias != NULL) {
  880. dput(dentry);
  881. dentry = alias;
  882. }
  883. nfs_renew_times(dentry);
  884. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  885. return dentry;
  886. }
  887. /*
  888. * Code common to create, mkdir, and mknod.
  889. */
  890. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  891. struct nfs_fattr *fattr)
  892. {
  893. struct inode *inode;
  894. int error = -EACCES;
  895. /* We may have been initialized further down */
  896. if (dentry->d_inode)
  897. return 0;
  898. if (fhandle->size == 0) {
  899. struct inode *dir = dentry->d_parent->d_inode;
  900. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  901. if (error)
  902. goto out_err;
  903. }
  904. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  905. struct nfs_server *server = NFS_SB(dentry->d_sb);
  906. error = server->rpc_ops->getattr(server, fhandle, fattr);
  907. if (error < 0)
  908. goto out_err;
  909. }
  910. error = -ENOMEM;
  911. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  912. if (inode == NULL)
  913. goto out_err;
  914. d_instantiate(dentry, inode);
  915. return 0;
  916. out_err:
  917. d_drop(dentry);
  918. return error;
  919. }
  920. /*
  921. * Following a failed create operation, we drop the dentry rather
  922. * than retain a negative dentry. This avoids a problem in the event
  923. * that the operation succeeded on the server, but an error in the
  924. * reply path made it appear to have failed.
  925. */
  926. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  927. struct nameidata *nd)
  928. {
  929. struct iattr attr;
  930. int error;
  931. int open_flags = 0;
  932. dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id,
  933. dir->i_ino, dentry->d_name.name);
  934. attr.ia_mode = mode;
  935. attr.ia_valid = ATTR_MODE;
  936. if (nd && (nd->flags & LOOKUP_CREATE))
  937. open_flags = nd->intent.open.flags;
  938. lock_kernel();
  939. nfs_begin_data_update(dir);
  940. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
  941. nfs_end_data_update(dir);
  942. if (error != 0)
  943. goto out_err;
  944. nfs_renew_times(dentry);
  945. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  946. unlock_kernel();
  947. return 0;
  948. out_err:
  949. unlock_kernel();
  950. d_drop(dentry);
  951. return error;
  952. }
  953. /*
  954. * See comments for nfs_proc_create regarding failed operations.
  955. */
  956. static int
  957. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  958. {
  959. struct iattr attr;
  960. int status;
  961. dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id,
  962. dir->i_ino, dentry->d_name.name);
  963. if (!new_valid_dev(rdev))
  964. return -EINVAL;
  965. attr.ia_mode = mode;
  966. attr.ia_valid = ATTR_MODE;
  967. lock_kernel();
  968. nfs_begin_data_update(dir);
  969. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  970. nfs_end_data_update(dir);
  971. if (status != 0)
  972. goto out_err;
  973. nfs_renew_times(dentry);
  974. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  975. unlock_kernel();
  976. return 0;
  977. out_err:
  978. unlock_kernel();
  979. d_drop(dentry);
  980. return status;
  981. }
  982. /*
  983. * See comments for nfs_proc_create regarding failed operations.
  984. */
  985. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  986. {
  987. struct iattr attr;
  988. int error;
  989. dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id,
  990. dir->i_ino, dentry->d_name.name);
  991. attr.ia_valid = ATTR_MODE;
  992. attr.ia_mode = mode | S_IFDIR;
  993. lock_kernel();
  994. nfs_begin_data_update(dir);
  995. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  996. nfs_end_data_update(dir);
  997. if (error != 0)
  998. goto out_err;
  999. nfs_renew_times(dentry);
  1000. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1001. unlock_kernel();
  1002. return 0;
  1003. out_err:
  1004. d_drop(dentry);
  1005. unlock_kernel();
  1006. return error;
  1007. }
  1008. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1009. {
  1010. int error;
  1011. dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id,
  1012. dir->i_ino, dentry->d_name.name);
  1013. lock_kernel();
  1014. nfs_begin_data_update(dir);
  1015. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1016. /* Ensure the VFS deletes this inode */
  1017. if (error == 0 && dentry->d_inode != NULL)
  1018. dentry->d_inode->i_nlink = 0;
  1019. nfs_end_data_update(dir);
  1020. unlock_kernel();
  1021. return error;
  1022. }
  1023. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1024. {
  1025. static unsigned int sillycounter;
  1026. const int i_inosize = sizeof(dir->i_ino)*2;
  1027. const int countersize = sizeof(sillycounter)*2;
  1028. const int slen = sizeof(".nfs") + i_inosize + countersize - 1;
  1029. char silly[slen+1];
  1030. struct qstr qsilly;
  1031. struct dentry *sdentry;
  1032. int error = -EIO;
  1033. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1034. dentry->d_parent->d_name.name, dentry->d_name.name,
  1035. atomic_read(&dentry->d_count));
  1036. #ifdef NFS_PARANOIA
  1037. if (!dentry->d_inode)
  1038. printk("NFS: silly-renaming %s/%s, negative dentry??\n",
  1039. dentry->d_parent->d_name.name, dentry->d_name.name);
  1040. #endif
  1041. /*
  1042. * We don't allow a dentry to be silly-renamed twice.
  1043. */
  1044. error = -EBUSY;
  1045. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1046. goto out;
  1047. sprintf(silly, ".nfs%*.*lx",
  1048. i_inosize, i_inosize, dentry->d_inode->i_ino);
  1049. sdentry = NULL;
  1050. do {
  1051. char *suffix = silly + slen - countersize;
  1052. dput(sdentry);
  1053. sillycounter++;
  1054. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1055. dfprintk(VFS, "trying to rename %s to %s\n",
  1056. dentry->d_name.name, silly);
  1057. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1058. /*
  1059. * N.B. Better to return EBUSY here ... it could be
  1060. * dangerous to delete the file while it's in use.
  1061. */
  1062. if (IS_ERR(sdentry))
  1063. goto out;
  1064. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1065. qsilly.name = silly;
  1066. qsilly.len = strlen(silly);
  1067. nfs_begin_data_update(dir);
  1068. if (dentry->d_inode) {
  1069. nfs_begin_data_update(dentry->d_inode);
  1070. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1071. dir, &qsilly);
  1072. nfs_end_data_update(dentry->d_inode);
  1073. } else
  1074. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1075. dir, &qsilly);
  1076. nfs_end_data_update(dir);
  1077. if (!error) {
  1078. nfs_renew_times(dentry);
  1079. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1080. d_move(dentry, sdentry);
  1081. error = nfs_async_unlink(dentry);
  1082. /* If we return 0 we don't unlink */
  1083. }
  1084. dput(sdentry);
  1085. out:
  1086. return error;
  1087. }
  1088. /*
  1089. * Remove a file after making sure there are no pending writes,
  1090. * and after checking that the file has only one user.
  1091. *
  1092. * We invalidate the attribute cache and free the inode prior to the operation
  1093. * to avoid possible races if the server reuses the inode.
  1094. */
  1095. static int nfs_safe_remove(struct dentry *dentry)
  1096. {
  1097. struct inode *dir = dentry->d_parent->d_inode;
  1098. struct inode *inode = dentry->d_inode;
  1099. int error = -EBUSY;
  1100. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1101. dentry->d_parent->d_name.name, dentry->d_name.name);
  1102. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1103. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1104. error = 0;
  1105. goto out;
  1106. }
  1107. nfs_begin_data_update(dir);
  1108. if (inode != NULL) {
  1109. nfs_begin_data_update(inode);
  1110. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1111. /* The VFS may want to delete this inode */
  1112. if (error == 0)
  1113. inode->i_nlink--;
  1114. nfs_end_data_update(inode);
  1115. } else
  1116. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1117. nfs_end_data_update(dir);
  1118. out:
  1119. return error;
  1120. }
  1121. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1122. * belongs to an active ".nfs..." file and we return -EBUSY.
  1123. *
  1124. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1125. */
  1126. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1127. {
  1128. int error;
  1129. int need_rehash = 0;
  1130. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1131. dir->i_ino, dentry->d_name.name);
  1132. lock_kernel();
  1133. spin_lock(&dcache_lock);
  1134. spin_lock(&dentry->d_lock);
  1135. if (atomic_read(&dentry->d_count) > 1) {
  1136. spin_unlock(&dentry->d_lock);
  1137. spin_unlock(&dcache_lock);
  1138. error = nfs_sillyrename(dir, dentry);
  1139. unlock_kernel();
  1140. return error;
  1141. }
  1142. if (!d_unhashed(dentry)) {
  1143. __d_drop(dentry);
  1144. need_rehash = 1;
  1145. }
  1146. spin_unlock(&dentry->d_lock);
  1147. spin_unlock(&dcache_lock);
  1148. error = nfs_safe_remove(dentry);
  1149. if (!error) {
  1150. nfs_renew_times(dentry);
  1151. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1152. } else if (need_rehash)
  1153. d_rehash(dentry);
  1154. unlock_kernel();
  1155. return error;
  1156. }
  1157. static int
  1158. nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1159. {
  1160. struct iattr attr;
  1161. struct nfs_fattr sym_attr;
  1162. struct nfs_fh sym_fh;
  1163. struct qstr qsymname;
  1164. int error;
  1165. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1166. dir->i_ino, dentry->d_name.name, symname);
  1167. #ifdef NFS_PARANOIA
  1168. if (dentry->d_inode)
  1169. printk("nfs_proc_symlink: %s/%s not negative!\n",
  1170. dentry->d_parent->d_name.name, dentry->d_name.name);
  1171. #endif
  1172. /*
  1173. * Fill in the sattr for the call.
  1174. * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
  1175. */
  1176. attr.ia_valid = ATTR_MODE;
  1177. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1178. qsymname.name = symname;
  1179. qsymname.len = strlen(symname);
  1180. lock_kernel();
  1181. nfs_begin_data_update(dir);
  1182. error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
  1183. &attr, &sym_fh, &sym_attr);
  1184. nfs_end_data_update(dir);
  1185. if (!error) {
  1186. error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
  1187. } else {
  1188. if (error == -EEXIST)
  1189. printk("nfs_proc_symlink: %s/%s already exists??\n",
  1190. dentry->d_parent->d_name.name, dentry->d_name.name);
  1191. d_drop(dentry);
  1192. }
  1193. unlock_kernel();
  1194. return error;
  1195. }
  1196. static int
  1197. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1198. {
  1199. struct inode *inode = old_dentry->d_inode;
  1200. int error;
  1201. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1202. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1203. dentry->d_parent->d_name.name, dentry->d_name.name);
  1204. /*
  1205. * Drop the dentry in advance to force a new lookup.
  1206. * Since nfs_proc_link doesn't return a file handle,
  1207. * we can't use the existing dentry.
  1208. */
  1209. lock_kernel();
  1210. d_drop(dentry);
  1211. nfs_begin_data_update(dir);
  1212. nfs_begin_data_update(inode);
  1213. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1214. nfs_end_data_update(inode);
  1215. nfs_end_data_update(dir);
  1216. unlock_kernel();
  1217. return error;
  1218. }
  1219. /*
  1220. * RENAME
  1221. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1222. * different file handle for the same inode after a rename (e.g. when
  1223. * moving to a different directory). A fail-safe method to do so would
  1224. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1225. * rename the old file using the sillyrename stuff. This way, the original
  1226. * file in old_dir will go away when the last process iput()s the inode.
  1227. *
  1228. * FIXED.
  1229. *
  1230. * It actually works quite well. One needs to have the possibility for
  1231. * at least one ".nfs..." file in each directory the file ever gets
  1232. * moved or linked to which happens automagically with the new
  1233. * implementation that only depends on the dcache stuff instead of
  1234. * using the inode layer
  1235. *
  1236. * Unfortunately, things are a little more complicated than indicated
  1237. * above. For a cross-directory move, we want to make sure we can get
  1238. * rid of the old inode after the operation. This means there must be
  1239. * no pending writes (if it's a file), and the use count must be 1.
  1240. * If these conditions are met, we can drop the dentries before doing
  1241. * the rename.
  1242. */
  1243. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1244. struct inode *new_dir, struct dentry *new_dentry)
  1245. {
  1246. struct inode *old_inode = old_dentry->d_inode;
  1247. struct inode *new_inode = new_dentry->d_inode;
  1248. struct dentry *dentry = NULL, *rehash = NULL;
  1249. int error = -EBUSY;
  1250. /*
  1251. * To prevent any new references to the target during the rename,
  1252. * we unhash the dentry and free the inode in advance.
  1253. */
  1254. lock_kernel();
  1255. if (!d_unhashed(new_dentry)) {
  1256. d_drop(new_dentry);
  1257. rehash = new_dentry;
  1258. }
  1259. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1260. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1261. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1262. atomic_read(&new_dentry->d_count));
  1263. /*
  1264. * First check whether the target is busy ... we can't
  1265. * safely do _any_ rename if the target is in use.
  1266. *
  1267. * For files, make a copy of the dentry and then do a
  1268. * silly-rename. If the silly-rename succeeds, the
  1269. * copied dentry is hashed and becomes the new target.
  1270. */
  1271. if (!new_inode)
  1272. goto go_ahead;
  1273. if (S_ISDIR(new_inode->i_mode))
  1274. goto out;
  1275. else if (atomic_read(&new_dentry->d_count) > 2) {
  1276. int err;
  1277. /* copy the target dentry's name */
  1278. dentry = d_alloc(new_dentry->d_parent,
  1279. &new_dentry->d_name);
  1280. if (!dentry)
  1281. goto out;
  1282. /* silly-rename the existing target ... */
  1283. err = nfs_sillyrename(new_dir, new_dentry);
  1284. if (!err) {
  1285. new_dentry = rehash = dentry;
  1286. new_inode = NULL;
  1287. /* instantiate the replacement target */
  1288. d_instantiate(new_dentry, NULL);
  1289. } else if (atomic_read(&new_dentry->d_count) > 1) {
  1290. /* dentry still busy? */
  1291. #ifdef NFS_PARANOIA
  1292. printk("nfs_rename: target %s/%s busy, d_count=%d\n",
  1293. new_dentry->d_parent->d_name.name,
  1294. new_dentry->d_name.name,
  1295. atomic_read(&new_dentry->d_count));
  1296. #endif
  1297. goto out;
  1298. }
  1299. }
  1300. go_ahead:
  1301. /*
  1302. * ... prune child dentries and writebacks if needed.
  1303. */
  1304. if (atomic_read(&old_dentry->d_count) > 1) {
  1305. nfs_wb_all(old_inode);
  1306. shrink_dcache_parent(old_dentry);
  1307. }
  1308. if (new_inode)
  1309. d_delete(new_dentry);
  1310. nfs_begin_data_update(old_dir);
  1311. nfs_begin_data_update(new_dir);
  1312. nfs_begin_data_update(old_inode);
  1313. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1314. new_dir, &new_dentry->d_name);
  1315. nfs_end_data_update(old_inode);
  1316. nfs_end_data_update(new_dir);
  1317. nfs_end_data_update(old_dir);
  1318. out:
  1319. if (rehash)
  1320. d_rehash(rehash);
  1321. if (!error) {
  1322. if (!S_ISDIR(old_inode->i_mode))
  1323. d_move(old_dentry, new_dentry);
  1324. nfs_renew_times(new_dentry);
  1325. nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
  1326. }
  1327. /* new dentry created? */
  1328. if (dentry)
  1329. dput(dentry);
  1330. unlock_kernel();
  1331. return error;
  1332. }
  1333. int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1334. {
  1335. struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
  1336. if (cache->cred != cred
  1337. || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
  1338. || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS))
  1339. return -ENOENT;
  1340. memcpy(res, cache, sizeof(*res));
  1341. return 0;
  1342. }
  1343. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1344. {
  1345. struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
  1346. if (cache->cred != set->cred) {
  1347. if (cache->cred)
  1348. put_rpccred(cache->cred);
  1349. cache->cred = get_rpccred(set->cred);
  1350. }
  1351. NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS;
  1352. cache->jiffies = set->jiffies;
  1353. cache->mask = set->mask;
  1354. }
  1355. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1356. {
  1357. struct nfs_access_entry cache;
  1358. int status;
  1359. status = nfs_access_get_cached(inode, cred, &cache);
  1360. if (status == 0)
  1361. goto out;
  1362. /* Be clever: ask server to check for all possible rights */
  1363. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1364. cache.cred = cred;
  1365. cache.jiffies = jiffies;
  1366. status = NFS_PROTO(inode)->access(inode, &cache);
  1367. if (status != 0)
  1368. return status;
  1369. nfs_access_add_cache(inode, &cache);
  1370. out:
  1371. if ((cache.mask & mask) == mask)
  1372. return 0;
  1373. return -EACCES;
  1374. }
  1375. int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1376. {
  1377. struct rpc_cred *cred;
  1378. int res = 0;
  1379. if (mask == 0)
  1380. goto out;
  1381. /* Is this sys_access() ? */
  1382. if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
  1383. goto force_lookup;
  1384. switch (inode->i_mode & S_IFMT) {
  1385. case S_IFLNK:
  1386. goto out;
  1387. case S_IFREG:
  1388. /* NFSv4 has atomic_open... */
  1389. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1390. && nd != NULL
  1391. && (nd->flags & LOOKUP_OPEN))
  1392. goto out;
  1393. break;
  1394. case S_IFDIR:
  1395. /*
  1396. * Optimize away all write operations, since the server
  1397. * will check permissions when we perform the op.
  1398. */
  1399. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1400. goto out;
  1401. }
  1402. force_lookup:
  1403. lock_kernel();
  1404. if (!NFS_PROTO(inode)->access)
  1405. goto out_notsup;
  1406. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1407. if (!IS_ERR(cred)) {
  1408. res = nfs_do_access(inode, cred, mask);
  1409. put_rpccred(cred);
  1410. } else
  1411. res = PTR_ERR(cred);
  1412. unlock_kernel();
  1413. out:
  1414. return res;
  1415. out_notsup:
  1416. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1417. if (res == 0)
  1418. res = generic_permission(inode, mask, NULL);
  1419. unlock_kernel();
  1420. return res;
  1421. }
  1422. /*
  1423. * Local variables:
  1424. * version-control: t
  1425. * kept-new-versions: 5
  1426. * End:
  1427. */