dir.c 52 KB

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