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 inode *dir, const struct nfs_fattr *fattr)
  816. {
  817. struct nfs_server *server = NFS_SERVER(dir);
  818. if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
  819. /* Revalidate fsid using the parent directory */
  820. return __nfs_revalidate_inode(server, dir);
  821. return 0;
  822. }
  823. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  824. {
  825. struct dentry *res;
  826. struct inode *inode = NULL;
  827. int error;
  828. struct nfs_fh fhandle;
  829. struct nfs_fattr fattr;
  830. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  831. dentry->d_parent->d_name.name, dentry->d_name.name);
  832. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  833. res = ERR_PTR(-ENAMETOOLONG);
  834. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  835. goto out;
  836. res = ERR_PTR(-ENOMEM);
  837. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  838. lock_kernel();
  839. /*
  840. * If we're doing an exclusive create, optimize away the lookup
  841. * but don't hash the dentry.
  842. */
  843. if (nfs_is_exclusive_create(dir, nd)) {
  844. d_instantiate(dentry, NULL);
  845. res = NULL;
  846. goto out_unlock;
  847. }
  848. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  849. if (error == -ENOENT)
  850. goto no_entry;
  851. if (error < 0) {
  852. res = ERR_PTR(error);
  853. goto out_unlock;
  854. }
  855. error = nfs_reval_fsid(dir, &fattr);
  856. if (error < 0) {
  857. res = ERR_PTR(error);
  858. goto out_unlock;
  859. }
  860. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  861. res = (struct dentry *)inode;
  862. if (IS_ERR(res))
  863. goto out_unlock;
  864. no_entry:
  865. res = d_materialise_unique(dentry, inode);
  866. if (res != NULL) {
  867. struct dentry *parent;
  868. if (IS_ERR(res))
  869. goto out_unlock;
  870. /* Was a directory renamed! */
  871. parent = dget_parent(res);
  872. if (!IS_ROOT(parent))
  873. nfs_mark_for_revalidate(parent->d_inode);
  874. dput(parent);
  875. dentry = res;
  876. }
  877. nfs_renew_times(dentry);
  878. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  879. out_unlock:
  880. unlock_kernel();
  881. out:
  882. return res;
  883. }
  884. #ifdef CONFIG_NFS_V4
  885. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  886. struct dentry_operations nfs4_dentry_operations = {
  887. .d_revalidate = nfs_open_revalidate,
  888. .d_delete = nfs_dentry_delete,
  889. .d_iput = nfs_dentry_iput,
  890. };
  891. /*
  892. * Use intent information to determine whether we need to substitute
  893. * the NFSv4-style stateful OPEN for the LOOKUP call
  894. */
  895. static int is_atomic_open(struct inode *dir, struct nameidata *nd)
  896. {
  897. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  898. return 0;
  899. /* NFS does not (yet) have a stateful open for directories */
  900. if (nd->flags & LOOKUP_DIRECTORY)
  901. return 0;
  902. /* Are we trying to write to a read only partition? */
  903. if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  904. return 0;
  905. return 1;
  906. }
  907. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  908. {
  909. struct dentry *res = NULL;
  910. int error;
  911. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  912. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  913. /* Check that we are indeed trying to open this file */
  914. if (!is_atomic_open(dir, nd))
  915. goto no_open;
  916. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  917. res = ERR_PTR(-ENAMETOOLONG);
  918. goto out;
  919. }
  920. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  921. /* Let vfs_create() deal with O_EXCL */
  922. if (nd->intent.open.flags & O_EXCL) {
  923. d_add(dentry, NULL);
  924. goto out;
  925. }
  926. /* Open the file on the server */
  927. lock_kernel();
  928. /* Revalidate parent directory attribute cache */
  929. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  930. if (error < 0) {
  931. res = ERR_PTR(error);
  932. unlock_kernel();
  933. goto out;
  934. }
  935. if (nd->intent.open.flags & O_CREAT) {
  936. nfs_begin_data_update(dir);
  937. res = nfs4_atomic_open(dir, dentry, nd);
  938. nfs_end_data_update(dir);
  939. } else
  940. res = nfs4_atomic_open(dir, dentry, nd);
  941. unlock_kernel();
  942. if (IS_ERR(res)) {
  943. error = PTR_ERR(res);
  944. switch (error) {
  945. /* Make a negative dentry */
  946. case -ENOENT:
  947. res = NULL;
  948. goto out;
  949. /* This turned out not to be a regular file */
  950. case -EISDIR:
  951. case -ENOTDIR:
  952. goto no_open;
  953. case -ELOOP:
  954. if (!(nd->intent.open.flags & O_NOFOLLOW))
  955. goto no_open;
  956. /* case -EINVAL: */
  957. default:
  958. goto out;
  959. }
  960. } else if (res != NULL)
  961. dentry = res;
  962. nfs_renew_times(dentry);
  963. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  964. out:
  965. return res;
  966. no_open:
  967. return nfs_lookup(dir, dentry, nd);
  968. }
  969. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  970. {
  971. struct dentry *parent = NULL;
  972. struct inode *inode = dentry->d_inode;
  973. struct inode *dir;
  974. unsigned long verifier;
  975. int openflags, ret = 0;
  976. parent = dget_parent(dentry);
  977. dir = parent->d_inode;
  978. if (!is_atomic_open(dir, nd))
  979. goto no_open;
  980. /* We can't create new files in nfs_open_revalidate(), so we
  981. * optimize away revalidation of negative dentries.
  982. */
  983. if (inode == NULL)
  984. goto out;
  985. /* NFS only supports OPEN on regular files */
  986. if (!S_ISREG(inode->i_mode))
  987. goto no_open;
  988. openflags = nd->intent.open.flags;
  989. /* We cannot do exclusive creation on a positive dentry */
  990. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  991. goto no_open;
  992. /* We can't create new files, or truncate existing ones here */
  993. openflags &= ~(O_CREAT|O_TRUNC);
  994. /*
  995. * Note: we're not holding inode->i_mutex and so may be racing with
  996. * operations that change the directory. We therefore save the
  997. * change attribute *before* we do the RPC call.
  998. */
  999. lock_kernel();
  1000. verifier = nfs_save_change_attribute(dir);
  1001. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  1002. if (!ret)
  1003. nfs_refresh_verifier(dentry, verifier);
  1004. unlock_kernel();
  1005. out:
  1006. dput(parent);
  1007. if (!ret)
  1008. d_drop(dentry);
  1009. return ret;
  1010. no_open:
  1011. dput(parent);
  1012. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  1013. return 1;
  1014. return nfs_lookup_revalidate(dentry, nd);
  1015. }
  1016. #endif /* CONFIG_NFSV4 */
  1017. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  1018. {
  1019. struct dentry *parent = desc->file->f_path.dentry;
  1020. struct inode *dir = parent->d_inode;
  1021. struct nfs_entry *entry = desc->entry;
  1022. struct dentry *dentry, *alias;
  1023. struct qstr name = {
  1024. .name = entry->name,
  1025. .len = entry->len,
  1026. };
  1027. struct inode *inode;
  1028. switch (name.len) {
  1029. case 2:
  1030. if (name.name[0] == '.' && name.name[1] == '.')
  1031. return dget_parent(parent);
  1032. break;
  1033. case 1:
  1034. if (name.name[0] == '.')
  1035. return dget(parent);
  1036. }
  1037. name.hash = full_name_hash(name.name, name.len);
  1038. dentry = d_lookup(parent, &name);
  1039. if (dentry != NULL) {
  1040. /* Is this a positive dentry that matches the readdir info? */
  1041. if (dentry->d_inode != NULL &&
  1042. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1043. d_mountpoint(dentry))) {
  1044. if (!desc->plus || entry->fh->size == 0)
  1045. return dentry;
  1046. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1047. entry->fh) == 0)
  1048. goto out_renew;
  1049. }
  1050. /* No, so d_drop to allow one to be created */
  1051. d_drop(dentry);
  1052. dput(dentry);
  1053. }
  1054. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1055. return NULL;
  1056. /* Note: caller is already holding the dir->i_mutex! */
  1057. dentry = d_alloc(parent, &name);
  1058. if (dentry == NULL)
  1059. return NULL;
  1060. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1061. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1062. if (IS_ERR(inode)) {
  1063. dput(dentry);
  1064. return NULL;
  1065. }
  1066. alias = d_materialise_unique(dentry, inode);
  1067. if (alias != NULL) {
  1068. dput(dentry);
  1069. if (IS_ERR(alias))
  1070. return NULL;
  1071. dentry = alias;
  1072. }
  1073. nfs_renew_times(dentry);
  1074. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1075. return dentry;
  1076. out_renew:
  1077. nfs_renew_times(dentry);
  1078. nfs_refresh_verifier(dentry, nfs_save_change_attribute(dir));
  1079. return dentry;
  1080. }
  1081. /*
  1082. * Code common to create, mkdir, and mknod.
  1083. */
  1084. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1085. struct nfs_fattr *fattr)
  1086. {
  1087. struct inode *inode;
  1088. int error = -EACCES;
  1089. /* We may have been initialized further down */
  1090. if (dentry->d_inode)
  1091. return 0;
  1092. if (fhandle->size == 0) {
  1093. struct inode *dir = dentry->d_parent->d_inode;
  1094. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1095. if (error)
  1096. return error;
  1097. }
  1098. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1099. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1100. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1101. if (error < 0)
  1102. return error;
  1103. }
  1104. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1105. error = PTR_ERR(inode);
  1106. if (IS_ERR(inode))
  1107. return error;
  1108. d_instantiate(dentry, inode);
  1109. if (d_unhashed(dentry))
  1110. d_rehash(dentry);
  1111. return 0;
  1112. }
  1113. /*
  1114. * Following a failed create operation, we drop the dentry rather
  1115. * than retain a negative dentry. This avoids a problem in the event
  1116. * that the operation succeeded on the server, but an error in the
  1117. * reply path made it appear to have failed.
  1118. */
  1119. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1120. struct nameidata *nd)
  1121. {
  1122. struct iattr attr;
  1123. int error;
  1124. int open_flags = 0;
  1125. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1126. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1127. attr.ia_mode = mode;
  1128. attr.ia_valid = ATTR_MODE;
  1129. if ((nd->flags & LOOKUP_CREATE) != 0)
  1130. open_flags = nd->intent.open.flags;
  1131. lock_kernel();
  1132. nfs_begin_data_update(dir);
  1133. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1134. nfs_end_data_update(dir);
  1135. if (error != 0)
  1136. goto out_err;
  1137. nfs_renew_times(dentry);
  1138. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1139. unlock_kernel();
  1140. return 0;
  1141. out_err:
  1142. unlock_kernel();
  1143. d_drop(dentry);
  1144. return error;
  1145. }
  1146. /*
  1147. * See comments for nfs_proc_create regarding failed operations.
  1148. */
  1149. static int
  1150. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1151. {
  1152. struct iattr attr;
  1153. int status;
  1154. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1155. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1156. if (!new_valid_dev(rdev))
  1157. return -EINVAL;
  1158. attr.ia_mode = mode;
  1159. attr.ia_valid = ATTR_MODE;
  1160. lock_kernel();
  1161. nfs_begin_data_update(dir);
  1162. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1163. nfs_end_data_update(dir);
  1164. if (status != 0)
  1165. goto out_err;
  1166. nfs_renew_times(dentry);
  1167. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1168. unlock_kernel();
  1169. return 0;
  1170. out_err:
  1171. unlock_kernel();
  1172. d_drop(dentry);
  1173. return status;
  1174. }
  1175. /*
  1176. * See comments for nfs_proc_create regarding failed operations.
  1177. */
  1178. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1179. {
  1180. struct iattr attr;
  1181. int error;
  1182. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1183. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1184. attr.ia_valid = ATTR_MODE;
  1185. attr.ia_mode = mode | S_IFDIR;
  1186. lock_kernel();
  1187. nfs_begin_data_update(dir);
  1188. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1189. nfs_end_data_update(dir);
  1190. if (error != 0)
  1191. goto out_err;
  1192. nfs_renew_times(dentry);
  1193. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1194. unlock_kernel();
  1195. return 0;
  1196. out_err:
  1197. d_drop(dentry);
  1198. unlock_kernel();
  1199. return error;
  1200. }
  1201. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1202. {
  1203. int error;
  1204. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1205. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1206. lock_kernel();
  1207. nfs_begin_data_update(dir);
  1208. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1209. /* Ensure the VFS deletes this inode */
  1210. if (error == 0 && dentry->d_inode != NULL)
  1211. clear_nlink(dentry->d_inode);
  1212. nfs_end_data_update(dir);
  1213. unlock_kernel();
  1214. return error;
  1215. }
  1216. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1217. {
  1218. static unsigned int sillycounter;
  1219. const int i_inosize = sizeof(dir->i_ino)*2;
  1220. const int countersize = sizeof(sillycounter)*2;
  1221. const int slen = sizeof(".nfs") + i_inosize + countersize - 1;
  1222. char silly[slen+1];
  1223. struct qstr qsilly;
  1224. struct dentry *sdentry;
  1225. int error = -EIO;
  1226. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1227. dentry->d_parent->d_name.name, dentry->d_name.name,
  1228. atomic_read(&dentry->d_count));
  1229. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1230. /*
  1231. * We don't allow a dentry to be silly-renamed twice.
  1232. */
  1233. error = -EBUSY;
  1234. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1235. goto out;
  1236. sprintf(silly, ".nfs%*.*lx",
  1237. i_inosize, i_inosize, dentry->d_inode->i_ino);
  1238. /* Return delegation in anticipation of the rename */
  1239. nfs_inode_return_delegation(dentry->d_inode);
  1240. sdentry = NULL;
  1241. do {
  1242. char *suffix = silly + slen - countersize;
  1243. dput(sdentry);
  1244. sillycounter++;
  1245. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1246. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1247. dentry->d_name.name, silly);
  1248. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1249. /*
  1250. * N.B. Better to return EBUSY here ... it could be
  1251. * dangerous to delete the file while it's in use.
  1252. */
  1253. if (IS_ERR(sdentry))
  1254. goto out;
  1255. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1256. qsilly.name = silly;
  1257. qsilly.len = strlen(silly);
  1258. nfs_begin_data_update(dir);
  1259. if (dentry->d_inode) {
  1260. nfs_begin_data_update(dentry->d_inode);
  1261. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1262. dir, &qsilly);
  1263. nfs_mark_for_revalidate(dentry->d_inode);
  1264. nfs_end_data_update(dentry->d_inode);
  1265. } else
  1266. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1267. dir, &qsilly);
  1268. nfs_end_data_update(dir);
  1269. if (!error) {
  1270. nfs_renew_times(dentry);
  1271. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1272. d_move(dentry, sdentry);
  1273. error = nfs_async_unlink(dentry);
  1274. /* If we return 0 we don't unlink */
  1275. }
  1276. dput(sdentry);
  1277. out:
  1278. return error;
  1279. }
  1280. /*
  1281. * Remove a file after making sure there are no pending writes,
  1282. * and after checking that the file has only one user.
  1283. *
  1284. * We invalidate the attribute cache and free the inode prior to the operation
  1285. * to avoid possible races if the server reuses the inode.
  1286. */
  1287. static int nfs_safe_remove(struct dentry *dentry)
  1288. {
  1289. struct inode *dir = dentry->d_parent->d_inode;
  1290. struct inode *inode = dentry->d_inode;
  1291. int error = -EBUSY;
  1292. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1293. dentry->d_parent->d_name.name, dentry->d_name.name);
  1294. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1295. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1296. error = 0;
  1297. goto out;
  1298. }
  1299. nfs_begin_data_update(dir);
  1300. if (inode != NULL) {
  1301. nfs_inode_return_delegation(inode);
  1302. nfs_begin_data_update(inode);
  1303. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1304. /* The VFS may want to delete this inode */
  1305. if (error == 0)
  1306. drop_nlink(inode);
  1307. nfs_mark_for_revalidate(inode);
  1308. nfs_end_data_update(inode);
  1309. } else
  1310. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1311. nfs_end_data_update(dir);
  1312. out:
  1313. return error;
  1314. }
  1315. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1316. * belongs to an active ".nfs..." file and we return -EBUSY.
  1317. *
  1318. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1319. */
  1320. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1321. {
  1322. int error;
  1323. int need_rehash = 0;
  1324. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1325. dir->i_ino, dentry->d_name.name);
  1326. lock_kernel();
  1327. spin_lock(&dcache_lock);
  1328. spin_lock(&dentry->d_lock);
  1329. if (atomic_read(&dentry->d_count) > 1) {
  1330. spin_unlock(&dentry->d_lock);
  1331. spin_unlock(&dcache_lock);
  1332. /* Start asynchronous writeout of the inode */
  1333. write_inode_now(dentry->d_inode, 0);
  1334. error = nfs_sillyrename(dir, dentry);
  1335. unlock_kernel();
  1336. return error;
  1337. }
  1338. if (!d_unhashed(dentry)) {
  1339. __d_drop(dentry);
  1340. need_rehash = 1;
  1341. }
  1342. spin_unlock(&dentry->d_lock);
  1343. spin_unlock(&dcache_lock);
  1344. error = nfs_safe_remove(dentry);
  1345. if (!error) {
  1346. nfs_renew_times(dentry);
  1347. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1348. } else if (need_rehash)
  1349. d_rehash(dentry);
  1350. unlock_kernel();
  1351. return error;
  1352. }
  1353. /*
  1354. * To create a symbolic link, most file systems instantiate a new inode,
  1355. * add a page to it containing the path, then write it out to the disk
  1356. * using prepare_write/commit_write.
  1357. *
  1358. * Unfortunately the NFS client can't create the in-core inode first
  1359. * because it needs a file handle to create an in-core inode (see
  1360. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1361. * symlink request has completed on the server.
  1362. *
  1363. * So instead we allocate a raw page, copy the symname into it, then do
  1364. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1365. * now have a new file handle and can instantiate an in-core NFS inode
  1366. * and move the raw page into its mapping.
  1367. */
  1368. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1369. {
  1370. struct pagevec lru_pvec;
  1371. struct page *page;
  1372. char *kaddr;
  1373. struct iattr attr;
  1374. unsigned int pathlen = strlen(symname);
  1375. int error;
  1376. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1377. dir->i_ino, dentry->d_name.name, symname);
  1378. if (pathlen > PAGE_SIZE)
  1379. return -ENAMETOOLONG;
  1380. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1381. attr.ia_valid = ATTR_MODE;
  1382. lock_kernel();
  1383. page = alloc_page(GFP_HIGHUSER);
  1384. if (!page) {
  1385. unlock_kernel();
  1386. return -ENOMEM;
  1387. }
  1388. kaddr = kmap_atomic(page, KM_USER0);
  1389. memcpy(kaddr, symname, pathlen);
  1390. if (pathlen < PAGE_SIZE)
  1391. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1392. kunmap_atomic(kaddr, KM_USER0);
  1393. nfs_begin_data_update(dir);
  1394. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1395. nfs_end_data_update(dir);
  1396. if (error != 0) {
  1397. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1398. dir->i_sb->s_id, dir->i_ino,
  1399. dentry->d_name.name, symname, error);
  1400. d_drop(dentry);
  1401. __free_page(page);
  1402. unlock_kernel();
  1403. return error;
  1404. }
  1405. /*
  1406. * No big deal if we can't add this page to the page cache here.
  1407. * READLINK will get the missing page from the server if needed.
  1408. */
  1409. pagevec_init(&lru_pvec, 0);
  1410. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1411. GFP_KERNEL)) {
  1412. pagevec_add(&lru_pvec, page);
  1413. pagevec_lru_add(&lru_pvec);
  1414. SetPageUptodate(page);
  1415. unlock_page(page);
  1416. } else
  1417. __free_page(page);
  1418. unlock_kernel();
  1419. return 0;
  1420. }
  1421. static int
  1422. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1423. {
  1424. struct inode *inode = old_dentry->d_inode;
  1425. int error;
  1426. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1427. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1428. dentry->d_parent->d_name.name, dentry->d_name.name);
  1429. lock_kernel();
  1430. nfs_begin_data_update(dir);
  1431. nfs_begin_data_update(inode);
  1432. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1433. if (error == 0) {
  1434. atomic_inc(&inode->i_count);
  1435. d_instantiate(dentry, inode);
  1436. }
  1437. nfs_end_data_update(inode);
  1438. nfs_end_data_update(dir);
  1439. unlock_kernel();
  1440. return error;
  1441. }
  1442. /*
  1443. * RENAME
  1444. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1445. * different file handle for the same inode after a rename (e.g. when
  1446. * moving to a different directory). A fail-safe method to do so would
  1447. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1448. * rename the old file using the sillyrename stuff. This way, the original
  1449. * file in old_dir will go away when the last process iput()s the inode.
  1450. *
  1451. * FIXED.
  1452. *
  1453. * It actually works quite well. One needs to have the possibility for
  1454. * at least one ".nfs..." file in each directory the file ever gets
  1455. * moved or linked to which happens automagically with the new
  1456. * implementation that only depends on the dcache stuff instead of
  1457. * using the inode layer
  1458. *
  1459. * Unfortunately, things are a little more complicated than indicated
  1460. * above. For a cross-directory move, we want to make sure we can get
  1461. * rid of the old inode after the operation. This means there must be
  1462. * no pending writes (if it's a file), and the use count must be 1.
  1463. * If these conditions are met, we can drop the dentries before doing
  1464. * the rename.
  1465. */
  1466. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1467. struct inode *new_dir, struct dentry *new_dentry)
  1468. {
  1469. struct inode *old_inode = old_dentry->d_inode;
  1470. struct inode *new_inode = new_dentry->d_inode;
  1471. struct dentry *dentry = NULL, *rehash = NULL;
  1472. int error = -EBUSY;
  1473. /*
  1474. * To prevent any new references to the target during the rename,
  1475. * we unhash the dentry and free the inode in advance.
  1476. */
  1477. lock_kernel();
  1478. if (!d_unhashed(new_dentry)) {
  1479. d_drop(new_dentry);
  1480. rehash = new_dentry;
  1481. }
  1482. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1483. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1484. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1485. atomic_read(&new_dentry->d_count));
  1486. /*
  1487. * First check whether the target is busy ... we can't
  1488. * safely do _any_ rename if the target is in use.
  1489. *
  1490. * For files, make a copy of the dentry and then do a
  1491. * silly-rename. If the silly-rename succeeds, the
  1492. * copied dentry is hashed and becomes the new target.
  1493. */
  1494. if (!new_inode)
  1495. goto go_ahead;
  1496. if (S_ISDIR(new_inode->i_mode)) {
  1497. error = -EISDIR;
  1498. if (!S_ISDIR(old_inode->i_mode))
  1499. goto out;
  1500. } else if (atomic_read(&new_dentry->d_count) > 2) {
  1501. int err;
  1502. /* copy the target dentry's name */
  1503. dentry = d_alloc(new_dentry->d_parent,
  1504. &new_dentry->d_name);
  1505. if (!dentry)
  1506. goto out;
  1507. /* silly-rename the existing target ... */
  1508. err = nfs_sillyrename(new_dir, new_dentry);
  1509. if (!err) {
  1510. new_dentry = rehash = dentry;
  1511. new_inode = NULL;
  1512. /* instantiate the replacement target */
  1513. d_instantiate(new_dentry, NULL);
  1514. } else if (atomic_read(&new_dentry->d_count) > 1)
  1515. /* dentry still busy? */
  1516. goto out;
  1517. } else
  1518. drop_nlink(new_inode);
  1519. go_ahead:
  1520. /*
  1521. * ... prune child dentries and writebacks if needed.
  1522. */
  1523. if (atomic_read(&old_dentry->d_count) > 1) {
  1524. if (S_ISREG(old_inode->i_mode))
  1525. nfs_wb_all(old_inode);
  1526. shrink_dcache_parent(old_dentry);
  1527. }
  1528. nfs_inode_return_delegation(old_inode);
  1529. if (new_inode != NULL) {
  1530. nfs_inode_return_delegation(new_inode);
  1531. d_delete(new_dentry);
  1532. }
  1533. nfs_begin_data_update(old_dir);
  1534. nfs_begin_data_update(new_dir);
  1535. nfs_begin_data_update(old_inode);
  1536. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1537. new_dir, &new_dentry->d_name);
  1538. nfs_mark_for_revalidate(old_inode);
  1539. nfs_end_data_update(old_inode);
  1540. nfs_end_data_update(new_dir);
  1541. nfs_end_data_update(old_dir);
  1542. out:
  1543. if (rehash)
  1544. d_rehash(rehash);
  1545. if (!error) {
  1546. d_move(old_dentry, new_dentry);
  1547. nfs_renew_times(new_dentry);
  1548. nfs_refresh_verifier(new_dentry, nfs_save_change_attribute(new_dir));
  1549. }
  1550. /* new dentry created? */
  1551. if (dentry)
  1552. dput(dentry);
  1553. unlock_kernel();
  1554. return error;
  1555. }
  1556. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1557. static LIST_HEAD(nfs_access_lru_list);
  1558. static atomic_long_t nfs_access_nr_entries;
  1559. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1560. {
  1561. put_rpccred(entry->cred);
  1562. kfree(entry);
  1563. smp_mb__before_atomic_dec();
  1564. atomic_long_dec(&nfs_access_nr_entries);
  1565. smp_mb__after_atomic_dec();
  1566. }
  1567. int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  1568. {
  1569. LIST_HEAD(head);
  1570. struct nfs_inode *nfsi;
  1571. struct nfs_access_entry *cache;
  1572. restart:
  1573. spin_lock(&nfs_access_lru_lock);
  1574. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1575. struct inode *inode;
  1576. if (nr_to_scan-- == 0)
  1577. break;
  1578. inode = igrab(&nfsi->vfs_inode);
  1579. if (inode == NULL)
  1580. continue;
  1581. spin_lock(&inode->i_lock);
  1582. if (list_empty(&nfsi->access_cache_entry_lru))
  1583. goto remove_lru_entry;
  1584. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1585. struct nfs_access_entry, lru);
  1586. list_move(&cache->lru, &head);
  1587. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1588. if (!list_empty(&nfsi->access_cache_entry_lru))
  1589. list_move_tail(&nfsi->access_cache_inode_lru,
  1590. &nfs_access_lru_list);
  1591. else {
  1592. remove_lru_entry:
  1593. list_del_init(&nfsi->access_cache_inode_lru);
  1594. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1595. }
  1596. spin_unlock(&inode->i_lock);
  1597. spin_unlock(&nfs_access_lru_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. */