dir.c 57 KB

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