dir.c 44 KB

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