dir.c 51 KB

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