dir.c 42 KB

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