dir.c 53 KB

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