dir.c 56 KB

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