dir.c 50 KB

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