dir.c 57 KB

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