dir.c 57 KB

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