dir.c 53 KB

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