dir.c 51 KB

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