dir.c 41 KB

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