dir.c 51 KB

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