dir.c 57 KB

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