dir.c 53 KB

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