dir.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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 struct file *nfs_atomic_open(struct inode *, struct dentry *,
  105. struct opendata *, unsigned, umode_t,
  106. bool *);
  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. * Return the intent data that applies to this particular path component
  920. *
  921. * Note that the current set of intents only apply to the very last
  922. * component of the path and none of them is set before that last
  923. * component.
  924. */
  925. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
  926. unsigned int mask)
  927. {
  928. return nd->flags & mask;
  929. }
  930. /*
  931. * Use intent information to check whether or not we're going to do
  932. * an O_EXCL create using this path component.
  933. */
  934. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  935. {
  936. if (NFS_PROTO(dir)->version == 2)
  937. return 0;
  938. return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
  939. }
  940. /*
  941. * Inode and filehandle revalidation for lookups.
  942. *
  943. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  944. * or if the intent information indicates that we're about to open this
  945. * particular file and the "nocto" mount flag is not set.
  946. *
  947. */
  948. static inline
  949. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  950. {
  951. struct nfs_server *server = NFS_SERVER(inode);
  952. if (IS_AUTOMOUNT(inode))
  953. return 0;
  954. if (nd != NULL) {
  955. /* VFS wants an on-the-wire revalidation */
  956. if (nd->flags & LOOKUP_REVAL)
  957. goto out_force;
  958. /* This is an open(2) */
  959. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  960. !(server->flags & NFS_MOUNT_NOCTO) &&
  961. (S_ISREG(inode->i_mode) ||
  962. S_ISDIR(inode->i_mode)))
  963. goto out_force;
  964. return 0;
  965. }
  966. return nfs_revalidate_inode(server, inode);
  967. out_force:
  968. return __nfs_revalidate_inode(server, inode);
  969. }
  970. /*
  971. * We judge how long we want to trust negative
  972. * dentries by looking at the parent inode mtime.
  973. *
  974. * If parent mtime has changed, we revalidate, else we wait for a
  975. * period corresponding to the parent's attribute cache timeout value.
  976. */
  977. static inline
  978. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  979. struct nameidata *nd)
  980. {
  981. /* Don't revalidate a negative dentry if we're creating a new file */
  982. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  983. return 0;
  984. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  985. return 1;
  986. return !nfs_check_verifier(dir, dentry);
  987. }
  988. /*
  989. * This is called every time the dcache has a lookup hit,
  990. * and we should check whether we can really trust that
  991. * lookup.
  992. *
  993. * NOTE! The hit can be a negative hit too, don't assume
  994. * we have an inode!
  995. *
  996. * If the parent directory is seen to have changed, we throw out the
  997. * cached dentry and do a new lookup.
  998. */
  999. static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
  1000. {
  1001. struct inode *dir;
  1002. struct inode *inode;
  1003. struct dentry *parent;
  1004. struct nfs_fh *fhandle = NULL;
  1005. struct nfs_fattr *fattr = NULL;
  1006. int error;
  1007. if (nd->flags & LOOKUP_RCU)
  1008. return -ECHILD;
  1009. parent = dget_parent(dentry);
  1010. dir = parent->d_inode;
  1011. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  1012. inode = dentry->d_inode;
  1013. if (!inode) {
  1014. if (nfs_neg_need_reval(dir, dentry, nd))
  1015. goto out_bad;
  1016. goto out_valid_noent;
  1017. }
  1018. if (is_bad_inode(inode)) {
  1019. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  1020. __func__, dentry->d_parent->d_name.name,
  1021. dentry->d_name.name);
  1022. goto out_bad;
  1023. }
  1024. if (nfs_have_delegation(inode, FMODE_READ))
  1025. goto out_set_verifier;
  1026. /* Force a full look up iff the parent directory has changed */
  1027. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  1028. if (nfs_lookup_verify_inode(inode, nd))
  1029. goto out_zap_parent;
  1030. goto out_valid;
  1031. }
  1032. if (NFS_STALE(inode))
  1033. goto out_bad;
  1034. error = -ENOMEM;
  1035. fhandle = nfs_alloc_fhandle();
  1036. fattr = nfs_alloc_fattr();
  1037. if (fhandle == NULL || fattr == NULL)
  1038. goto out_error;
  1039. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1040. if (error)
  1041. goto out_bad;
  1042. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  1043. goto out_bad;
  1044. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  1045. goto out_bad;
  1046. nfs_free_fattr(fattr);
  1047. nfs_free_fhandle(fhandle);
  1048. out_set_verifier:
  1049. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1050. out_valid:
  1051. /* Success: notify readdir to use READDIRPLUS */
  1052. nfs_advise_use_readdirplus(dir);
  1053. out_valid_noent:
  1054. dput(parent);
  1055. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  1056. __func__, dentry->d_parent->d_name.name,
  1057. dentry->d_name.name);
  1058. return 1;
  1059. out_zap_parent:
  1060. nfs_zap_caches(dir);
  1061. out_bad:
  1062. nfs_mark_for_revalidate(dir);
  1063. if (inode && S_ISDIR(inode->i_mode)) {
  1064. /* Purge readdir caches. */
  1065. nfs_zap_caches(inode);
  1066. /* If we have submounts, don't unhash ! */
  1067. if (have_submounts(dentry))
  1068. goto out_valid;
  1069. if (dentry->d_flags & DCACHE_DISCONNECTED)
  1070. goto out_valid;
  1071. shrink_dcache_parent(dentry);
  1072. }
  1073. d_drop(dentry);
  1074. nfs_free_fattr(fattr);
  1075. nfs_free_fhandle(fhandle);
  1076. dput(parent);
  1077. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  1078. __func__, dentry->d_parent->d_name.name,
  1079. dentry->d_name.name);
  1080. return 0;
  1081. out_error:
  1082. nfs_free_fattr(fattr);
  1083. nfs_free_fhandle(fhandle);
  1084. dput(parent);
  1085. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
  1086. __func__, dentry->d_parent->d_name.name,
  1087. dentry->d_name.name, error);
  1088. return error;
  1089. }
  1090. /*
  1091. * This is called from dput() when d_count is going to 0.
  1092. */
  1093. static int nfs_dentry_delete(const struct dentry *dentry)
  1094. {
  1095. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  1096. dentry->d_parent->d_name.name, dentry->d_name.name,
  1097. dentry->d_flags);
  1098. /* Unhash any dentry with a stale inode */
  1099. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  1100. return 1;
  1101. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1102. /* Unhash it, so that ->d_iput() would be called */
  1103. return 1;
  1104. }
  1105. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  1106. /* Unhash it, so that ancestors of killed async unlink
  1107. * files will be cleaned up during umount */
  1108. return 1;
  1109. }
  1110. return 0;
  1111. }
  1112. static void nfs_drop_nlink(struct inode *inode)
  1113. {
  1114. spin_lock(&inode->i_lock);
  1115. if (inode->i_nlink > 0)
  1116. drop_nlink(inode);
  1117. spin_unlock(&inode->i_lock);
  1118. }
  1119. /*
  1120. * Called when the dentry loses inode.
  1121. * We use it to clean up silly-renamed files.
  1122. */
  1123. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1124. {
  1125. if (S_ISDIR(inode->i_mode))
  1126. /* drop any readdir cache as it could easily be old */
  1127. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  1128. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1129. drop_nlink(inode);
  1130. nfs_complete_unlink(dentry, inode);
  1131. }
  1132. iput(inode);
  1133. }
  1134. static void nfs_d_release(struct dentry *dentry)
  1135. {
  1136. /* free cached devname value, if it survived that far */
  1137. if (unlikely(dentry->d_fsdata)) {
  1138. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1139. WARN_ON(1);
  1140. else
  1141. kfree(dentry->d_fsdata);
  1142. }
  1143. }
  1144. const struct dentry_operations nfs_dentry_operations = {
  1145. .d_revalidate = nfs_lookup_revalidate,
  1146. .d_delete = nfs_dentry_delete,
  1147. .d_iput = nfs_dentry_iput,
  1148. .d_automount = nfs_d_automount,
  1149. .d_release = nfs_d_release,
  1150. };
  1151. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1152. {
  1153. struct dentry *res;
  1154. struct dentry *parent;
  1155. struct inode *inode = NULL;
  1156. struct nfs_fh *fhandle = NULL;
  1157. struct nfs_fattr *fattr = NULL;
  1158. int error;
  1159. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  1160. dentry->d_parent->d_name.name, dentry->d_name.name);
  1161. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1162. res = ERR_PTR(-ENAMETOOLONG);
  1163. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1164. goto out;
  1165. /*
  1166. * If we're doing an exclusive create, optimize away the lookup
  1167. * but don't hash the dentry.
  1168. */
  1169. if (nfs_is_exclusive_create(dir, nd)) {
  1170. d_instantiate(dentry, NULL);
  1171. res = NULL;
  1172. goto out;
  1173. }
  1174. res = ERR_PTR(-ENOMEM);
  1175. fhandle = nfs_alloc_fhandle();
  1176. fattr = nfs_alloc_fattr();
  1177. if (fhandle == NULL || fattr == NULL)
  1178. goto out;
  1179. parent = dentry->d_parent;
  1180. /* Protect against concurrent sillydeletes */
  1181. nfs_block_sillyrename(parent);
  1182. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1183. if (error == -ENOENT)
  1184. goto no_entry;
  1185. if (error < 0) {
  1186. res = ERR_PTR(error);
  1187. goto out_unblock_sillyrename;
  1188. }
  1189. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1190. res = ERR_CAST(inode);
  1191. if (IS_ERR(res))
  1192. goto out_unblock_sillyrename;
  1193. /* Success: notify readdir to use READDIRPLUS */
  1194. nfs_advise_use_readdirplus(dir);
  1195. no_entry:
  1196. res = d_materialise_unique(dentry, inode);
  1197. if (res != NULL) {
  1198. if (IS_ERR(res))
  1199. goto out_unblock_sillyrename;
  1200. dentry = res;
  1201. }
  1202. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1203. out_unblock_sillyrename:
  1204. nfs_unblock_sillyrename(parent);
  1205. out:
  1206. nfs_free_fattr(fattr);
  1207. nfs_free_fhandle(fhandle);
  1208. return res;
  1209. }
  1210. #ifdef CONFIG_NFS_V4
  1211. static int nfs4_lookup_revalidate(struct dentry *, struct nameidata *);
  1212. const struct dentry_operations nfs4_dentry_operations = {
  1213. .d_revalidate = nfs4_lookup_revalidate,
  1214. .d_delete = nfs_dentry_delete,
  1215. .d_iput = nfs_dentry_iput,
  1216. .d_automount = nfs_d_automount,
  1217. .d_release = nfs_d_release,
  1218. };
  1219. /*
  1220. * Use intent information to determine whether we need to substitute
  1221. * the NFSv4-style stateful OPEN for the LOOKUP call
  1222. */
  1223. static int is_atomic_open(struct nameidata *nd)
  1224. {
  1225. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  1226. return 0;
  1227. /* NFS does not (yet) have a stateful open for directories */
  1228. if (nd->flags & LOOKUP_DIRECTORY)
  1229. return 0;
  1230. /* Are we trying to write to a read only partition? */
  1231. if (__mnt_is_readonly(nd->path.mnt) &&
  1232. (nd->intent.open.flags & (O_CREAT|O_TRUNC|O_ACCMODE)))
  1233. return 0;
  1234. return 1;
  1235. }
  1236. static fmode_t flags_to_mode(int flags)
  1237. {
  1238. fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
  1239. if ((flags & O_ACCMODE) != O_WRONLY)
  1240. res |= FMODE_READ;
  1241. if ((flags & O_ACCMODE) != O_RDONLY)
  1242. res |= FMODE_WRITE;
  1243. return res;
  1244. }
  1245. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
  1246. {
  1247. return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
  1248. }
  1249. static int do_open(struct inode *inode, struct file *filp)
  1250. {
  1251. nfs_fscache_set_inode_cookie(inode, filp);
  1252. return 0;
  1253. }
  1254. static struct file *nfs_finish_open(struct nfs_open_context *ctx,
  1255. struct dentry *dentry,
  1256. struct opendata *od, unsigned open_flags)
  1257. {
  1258. struct file *filp;
  1259. int err;
  1260. if (ctx->dentry != dentry) {
  1261. dput(ctx->dentry);
  1262. ctx->dentry = dget(dentry);
  1263. }
  1264. /* If the open_intent is for execute, we have an extra check to make */
  1265. if (ctx->mode & FMODE_EXEC) {
  1266. err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
  1267. if (err < 0) {
  1268. filp = ERR_PTR(err);
  1269. goto out;
  1270. }
  1271. }
  1272. filp = finish_open(od, dentry, do_open);
  1273. if (!IS_ERR(filp))
  1274. nfs_file_set_open_context(filp, ctx);
  1275. out:
  1276. put_nfs_open_context(ctx);
  1277. return filp;
  1278. }
  1279. static struct file *nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  1280. struct opendata *od, unsigned open_flags,
  1281. umode_t mode, bool *created)
  1282. {
  1283. struct nfs_open_context *ctx;
  1284. struct dentry *res;
  1285. struct iattr attr = { .ia_valid = ATTR_OPEN };
  1286. struct inode *inode;
  1287. struct file *filp;
  1288. int err;
  1289. /* Expect a negative dentry */
  1290. BUG_ON(dentry->d_inode);
  1291. dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
  1292. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1293. /* NFS only supports OPEN on regular files */
  1294. if ((open_flags & O_DIRECTORY)) {
  1295. err = -ENOENT;
  1296. if (!d_unhashed(dentry)) {
  1297. /*
  1298. * Hashed negative dentry with O_DIRECTORY: dentry was
  1299. * revalidated and is fine, no need to perform lookup
  1300. * again
  1301. */
  1302. goto out_err;
  1303. }
  1304. goto no_open;
  1305. }
  1306. err = -ENAMETOOLONG;
  1307. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1308. goto out_err;
  1309. if (open_flags & O_CREAT) {
  1310. attr.ia_valid |= ATTR_MODE;
  1311. attr.ia_mode = mode & ~current_umask();
  1312. }
  1313. if (open_flags & O_TRUNC) {
  1314. attr.ia_valid |= ATTR_SIZE;
  1315. attr.ia_size = 0;
  1316. }
  1317. ctx = create_nfs_open_context(dentry, open_flags);
  1318. err = PTR_ERR(ctx);
  1319. if (IS_ERR(ctx))
  1320. goto out_err;
  1321. nfs_block_sillyrename(dentry->d_parent);
  1322. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
  1323. d_drop(dentry);
  1324. if (IS_ERR(inode)) {
  1325. nfs_unblock_sillyrename(dentry->d_parent);
  1326. put_nfs_open_context(ctx);
  1327. err = PTR_ERR(inode);
  1328. switch (err) {
  1329. case -ENOENT:
  1330. d_add(dentry, NULL);
  1331. break;
  1332. case -EISDIR:
  1333. case -ENOTDIR:
  1334. goto no_open;
  1335. case -ELOOP:
  1336. if (!(open_flags & O_NOFOLLOW))
  1337. goto no_open;
  1338. break;
  1339. /* case -EINVAL: */
  1340. default:
  1341. break;
  1342. }
  1343. goto out_err;
  1344. }
  1345. res = d_add_unique(dentry, inode);
  1346. if (res != NULL)
  1347. dentry = res;
  1348. nfs_unblock_sillyrename(dentry->d_parent);
  1349. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1350. filp = nfs_finish_open(ctx, dentry, od, open_flags);
  1351. dput(res);
  1352. return filp;
  1353. out_err:
  1354. return ERR_PTR(err);
  1355. no_open:
  1356. res = nfs_lookup(dir, dentry, NULL);
  1357. err = PTR_ERR(res);
  1358. if (IS_ERR(res))
  1359. goto out_err;
  1360. finish_no_open(od, res);
  1361. return NULL;
  1362. }
  1363. static int nfs4_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
  1364. {
  1365. struct dentry *parent = NULL;
  1366. struct inode *inode;
  1367. struct inode *dir;
  1368. int ret = 0;
  1369. if (nd->flags & LOOKUP_RCU)
  1370. return -ECHILD;
  1371. inode = dentry->d_inode;
  1372. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  1373. goto no_open;
  1374. parent = dget_parent(dentry);
  1375. dir = parent->d_inode;
  1376. /* We can't create new files in nfs_open_revalidate(), so we
  1377. * optimize away revalidation of negative dentries.
  1378. */
  1379. if (inode == NULL) {
  1380. if (!nfs_neg_need_reval(dir, dentry, nd))
  1381. ret = 1;
  1382. goto out;
  1383. }
  1384. /* NFS only supports OPEN on regular files */
  1385. if (!S_ISREG(inode->i_mode))
  1386. goto no_open_dput;
  1387. /* We cannot do exclusive creation on a positive dentry */
  1388. if (nd && nd->flags & LOOKUP_EXCL)
  1389. goto no_open_dput;
  1390. /* Let f_op->open() actually open (and revalidate) the file */
  1391. ret = 1;
  1392. out:
  1393. dput(parent);
  1394. return ret;
  1395. no_open_dput:
  1396. dput(parent);
  1397. no_open:
  1398. return nfs_lookup_revalidate(dentry, nd);
  1399. }
  1400. #endif /* CONFIG_NFSV4 */
  1401. /*
  1402. * Code common to create, mkdir, and mknod.
  1403. */
  1404. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1405. struct nfs_fattr *fattr)
  1406. {
  1407. struct dentry *parent = dget_parent(dentry);
  1408. struct inode *dir = parent->d_inode;
  1409. struct inode *inode;
  1410. int error = -EACCES;
  1411. d_drop(dentry);
  1412. /* We may have been initialized further down */
  1413. if (dentry->d_inode)
  1414. goto out;
  1415. if (fhandle->size == 0) {
  1416. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1417. if (error)
  1418. goto out_error;
  1419. }
  1420. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1421. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1422. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1423. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1424. if (error < 0)
  1425. goto out_error;
  1426. }
  1427. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1428. error = PTR_ERR(inode);
  1429. if (IS_ERR(inode))
  1430. goto out_error;
  1431. d_add(dentry, inode);
  1432. out:
  1433. dput(parent);
  1434. return 0;
  1435. out_error:
  1436. nfs_mark_for_revalidate(dir);
  1437. dput(parent);
  1438. return error;
  1439. }
  1440. /*
  1441. * Following a failed create operation, we drop the dentry rather
  1442. * than retain a negative dentry. This avoids a problem in the event
  1443. * that the operation succeeded on the server, but an error in the
  1444. * reply path made it appear to have failed.
  1445. */
  1446. static int nfs_create(struct inode *dir, struct dentry *dentry,
  1447. umode_t mode, struct nameidata *nd)
  1448. {
  1449. struct iattr attr;
  1450. int error;
  1451. int open_flags = O_CREAT|O_EXCL;
  1452. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1453. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1454. attr.ia_mode = mode;
  1455. attr.ia_valid = ATTR_MODE;
  1456. if (nd && !(nd->flags & LOOKUP_EXCL))
  1457. open_flags = O_CREAT;
  1458. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
  1459. if (error != 0)
  1460. goto out_err;
  1461. return 0;
  1462. out_err:
  1463. d_drop(dentry);
  1464. return error;
  1465. }
  1466. /*
  1467. * See comments for nfs_proc_create regarding failed operations.
  1468. */
  1469. static int
  1470. nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1471. {
  1472. struct iattr attr;
  1473. int status;
  1474. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1475. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1476. if (!new_valid_dev(rdev))
  1477. return -EINVAL;
  1478. attr.ia_mode = mode;
  1479. attr.ia_valid = ATTR_MODE;
  1480. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1481. if (status != 0)
  1482. goto out_err;
  1483. return 0;
  1484. out_err:
  1485. d_drop(dentry);
  1486. return status;
  1487. }
  1488. /*
  1489. * See comments for nfs_proc_create regarding failed operations.
  1490. */
  1491. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1492. {
  1493. struct iattr attr;
  1494. int error;
  1495. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1496. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1497. attr.ia_valid = ATTR_MODE;
  1498. attr.ia_mode = mode | S_IFDIR;
  1499. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1500. if (error != 0)
  1501. goto out_err;
  1502. return 0;
  1503. out_err:
  1504. d_drop(dentry);
  1505. return error;
  1506. }
  1507. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1508. {
  1509. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1510. d_delete(dentry);
  1511. }
  1512. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1513. {
  1514. int error;
  1515. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1516. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1517. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1518. /* Ensure the VFS deletes this inode */
  1519. if (error == 0 && dentry->d_inode != NULL)
  1520. clear_nlink(dentry->d_inode);
  1521. else if (error == -ENOENT)
  1522. nfs_dentry_handle_enoent(dentry);
  1523. return error;
  1524. }
  1525. /*
  1526. * Remove a file after making sure there are no pending writes,
  1527. * and after checking that the file has only one user.
  1528. *
  1529. * We invalidate the attribute cache and free the inode prior to the operation
  1530. * to avoid possible races if the server reuses the inode.
  1531. */
  1532. static int nfs_safe_remove(struct dentry *dentry)
  1533. {
  1534. struct inode *dir = dentry->d_parent->d_inode;
  1535. struct inode *inode = dentry->d_inode;
  1536. int error = -EBUSY;
  1537. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1538. dentry->d_parent->d_name.name, dentry->d_name.name);
  1539. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1540. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1541. error = 0;
  1542. goto out;
  1543. }
  1544. if (inode != NULL) {
  1545. nfs_inode_return_delegation(inode);
  1546. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1547. /* The VFS may want to delete this inode */
  1548. if (error == 0)
  1549. nfs_drop_nlink(inode);
  1550. nfs_mark_for_revalidate(inode);
  1551. } else
  1552. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1553. if (error == -ENOENT)
  1554. nfs_dentry_handle_enoent(dentry);
  1555. out:
  1556. return error;
  1557. }
  1558. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1559. * belongs to an active ".nfs..." file and we return -EBUSY.
  1560. *
  1561. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1562. */
  1563. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1564. {
  1565. int error;
  1566. int need_rehash = 0;
  1567. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1568. dir->i_ino, dentry->d_name.name);
  1569. spin_lock(&dentry->d_lock);
  1570. if (dentry->d_count > 1) {
  1571. spin_unlock(&dentry->d_lock);
  1572. /* Start asynchronous writeout of the inode */
  1573. write_inode_now(dentry->d_inode, 0);
  1574. error = nfs_sillyrename(dir, dentry);
  1575. return error;
  1576. }
  1577. if (!d_unhashed(dentry)) {
  1578. __d_drop(dentry);
  1579. need_rehash = 1;
  1580. }
  1581. spin_unlock(&dentry->d_lock);
  1582. error = nfs_safe_remove(dentry);
  1583. if (!error || error == -ENOENT) {
  1584. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1585. } else if (need_rehash)
  1586. d_rehash(dentry);
  1587. return error;
  1588. }
  1589. /*
  1590. * To create a symbolic link, most file systems instantiate a new inode,
  1591. * add a page to it containing the path, then write it out to the disk
  1592. * using prepare_write/commit_write.
  1593. *
  1594. * Unfortunately the NFS client can't create the in-core inode first
  1595. * because it needs a file handle to create an in-core inode (see
  1596. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1597. * symlink request has completed on the server.
  1598. *
  1599. * So instead we allocate a raw page, copy the symname into it, then do
  1600. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1601. * now have a new file handle and can instantiate an in-core NFS inode
  1602. * and move the raw page into its mapping.
  1603. */
  1604. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1605. {
  1606. struct pagevec lru_pvec;
  1607. struct page *page;
  1608. char *kaddr;
  1609. struct iattr attr;
  1610. unsigned int pathlen = strlen(symname);
  1611. int error;
  1612. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1613. dir->i_ino, dentry->d_name.name, symname);
  1614. if (pathlen > PAGE_SIZE)
  1615. return -ENAMETOOLONG;
  1616. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1617. attr.ia_valid = ATTR_MODE;
  1618. page = alloc_page(GFP_HIGHUSER);
  1619. if (!page)
  1620. return -ENOMEM;
  1621. kaddr = kmap_atomic(page);
  1622. memcpy(kaddr, symname, pathlen);
  1623. if (pathlen < PAGE_SIZE)
  1624. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1625. kunmap_atomic(kaddr);
  1626. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1627. if (error != 0) {
  1628. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1629. dir->i_sb->s_id, dir->i_ino,
  1630. dentry->d_name.name, symname, error);
  1631. d_drop(dentry);
  1632. __free_page(page);
  1633. return error;
  1634. }
  1635. /*
  1636. * No big deal if we can't add this page to the page cache here.
  1637. * READLINK will get the missing page from the server if needed.
  1638. */
  1639. pagevec_init(&lru_pvec, 0);
  1640. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1641. GFP_KERNEL)) {
  1642. pagevec_add(&lru_pvec, page);
  1643. pagevec_lru_add_file(&lru_pvec);
  1644. SetPageUptodate(page);
  1645. unlock_page(page);
  1646. } else
  1647. __free_page(page);
  1648. return 0;
  1649. }
  1650. static int
  1651. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1652. {
  1653. struct inode *inode = old_dentry->d_inode;
  1654. int error;
  1655. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1656. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1657. dentry->d_parent->d_name.name, dentry->d_name.name);
  1658. nfs_inode_return_delegation(inode);
  1659. d_drop(dentry);
  1660. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1661. if (error == 0) {
  1662. ihold(inode);
  1663. d_add(dentry, inode);
  1664. }
  1665. return error;
  1666. }
  1667. /*
  1668. * RENAME
  1669. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1670. * different file handle for the same inode after a rename (e.g. when
  1671. * moving to a different directory). A fail-safe method to do so would
  1672. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1673. * rename the old file using the sillyrename stuff. This way, the original
  1674. * file in old_dir will go away when the last process iput()s the inode.
  1675. *
  1676. * FIXED.
  1677. *
  1678. * It actually works quite well. One needs to have the possibility for
  1679. * at least one ".nfs..." file in each directory the file ever gets
  1680. * moved or linked to which happens automagically with the new
  1681. * implementation that only depends on the dcache stuff instead of
  1682. * using the inode layer
  1683. *
  1684. * Unfortunately, things are a little more complicated than indicated
  1685. * above. For a cross-directory move, we want to make sure we can get
  1686. * rid of the old inode after the operation. This means there must be
  1687. * no pending writes (if it's a file), and the use count must be 1.
  1688. * If these conditions are met, we can drop the dentries before doing
  1689. * the rename.
  1690. */
  1691. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1692. struct inode *new_dir, struct dentry *new_dentry)
  1693. {
  1694. struct inode *old_inode = old_dentry->d_inode;
  1695. struct inode *new_inode = new_dentry->d_inode;
  1696. struct dentry *dentry = NULL, *rehash = NULL;
  1697. int error = -EBUSY;
  1698. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1699. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1700. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1701. new_dentry->d_count);
  1702. /*
  1703. * For non-directories, check whether the target is busy and if so,
  1704. * make a copy of the dentry and then do a silly-rename. If the
  1705. * silly-rename succeeds, the copied dentry is hashed and becomes
  1706. * the new target.
  1707. */
  1708. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1709. /*
  1710. * To prevent any new references to the target during the
  1711. * rename, we unhash the dentry in advance.
  1712. */
  1713. if (!d_unhashed(new_dentry)) {
  1714. d_drop(new_dentry);
  1715. rehash = new_dentry;
  1716. }
  1717. if (new_dentry->d_count > 2) {
  1718. int err;
  1719. /* copy the target dentry's name */
  1720. dentry = d_alloc(new_dentry->d_parent,
  1721. &new_dentry->d_name);
  1722. if (!dentry)
  1723. goto out;
  1724. /* silly-rename the existing target ... */
  1725. err = nfs_sillyrename(new_dir, new_dentry);
  1726. if (err)
  1727. goto out;
  1728. new_dentry = dentry;
  1729. rehash = NULL;
  1730. new_inode = NULL;
  1731. }
  1732. }
  1733. nfs_inode_return_delegation(old_inode);
  1734. if (new_inode != NULL)
  1735. nfs_inode_return_delegation(new_inode);
  1736. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1737. new_dir, &new_dentry->d_name);
  1738. nfs_mark_for_revalidate(old_inode);
  1739. out:
  1740. if (rehash)
  1741. d_rehash(rehash);
  1742. if (!error) {
  1743. if (new_inode != NULL)
  1744. nfs_drop_nlink(new_inode);
  1745. d_move(old_dentry, new_dentry);
  1746. nfs_set_verifier(new_dentry,
  1747. nfs_save_change_attribute(new_dir));
  1748. } else if (error == -ENOENT)
  1749. nfs_dentry_handle_enoent(old_dentry);
  1750. /* new dentry created? */
  1751. if (dentry)
  1752. dput(dentry);
  1753. return error;
  1754. }
  1755. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1756. static LIST_HEAD(nfs_access_lru_list);
  1757. static atomic_long_t nfs_access_nr_entries;
  1758. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1759. {
  1760. put_rpccred(entry->cred);
  1761. kfree(entry);
  1762. smp_mb__before_atomic_dec();
  1763. atomic_long_dec(&nfs_access_nr_entries);
  1764. smp_mb__after_atomic_dec();
  1765. }
  1766. static void nfs_access_free_list(struct list_head *head)
  1767. {
  1768. struct nfs_access_entry *cache;
  1769. while (!list_empty(head)) {
  1770. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1771. list_del(&cache->lru);
  1772. nfs_access_free_entry(cache);
  1773. }
  1774. }
  1775. int nfs_access_cache_shrinker(struct shrinker *shrink,
  1776. struct shrink_control *sc)
  1777. {
  1778. LIST_HEAD(head);
  1779. struct nfs_inode *nfsi, *next;
  1780. struct nfs_access_entry *cache;
  1781. int nr_to_scan = sc->nr_to_scan;
  1782. gfp_t gfp_mask = sc->gfp_mask;
  1783. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1784. return (nr_to_scan == 0) ? 0 : -1;
  1785. spin_lock(&nfs_access_lru_lock);
  1786. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1787. struct inode *inode;
  1788. if (nr_to_scan-- == 0)
  1789. break;
  1790. inode = &nfsi->vfs_inode;
  1791. spin_lock(&inode->i_lock);
  1792. if (list_empty(&nfsi->access_cache_entry_lru))
  1793. goto remove_lru_entry;
  1794. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1795. struct nfs_access_entry, lru);
  1796. list_move(&cache->lru, &head);
  1797. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1798. if (!list_empty(&nfsi->access_cache_entry_lru))
  1799. list_move_tail(&nfsi->access_cache_inode_lru,
  1800. &nfs_access_lru_list);
  1801. else {
  1802. remove_lru_entry:
  1803. list_del_init(&nfsi->access_cache_inode_lru);
  1804. smp_mb__before_clear_bit();
  1805. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1806. smp_mb__after_clear_bit();
  1807. }
  1808. spin_unlock(&inode->i_lock);
  1809. }
  1810. spin_unlock(&nfs_access_lru_lock);
  1811. nfs_access_free_list(&head);
  1812. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1813. }
  1814. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1815. {
  1816. struct rb_root *root_node = &nfsi->access_cache;
  1817. struct rb_node *n;
  1818. struct nfs_access_entry *entry;
  1819. /* Unhook entries from the cache */
  1820. while ((n = rb_first(root_node)) != NULL) {
  1821. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1822. rb_erase(n, root_node);
  1823. list_move(&entry->lru, head);
  1824. }
  1825. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1826. }
  1827. void nfs_access_zap_cache(struct inode *inode)
  1828. {
  1829. LIST_HEAD(head);
  1830. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1831. return;
  1832. /* Remove from global LRU init */
  1833. spin_lock(&nfs_access_lru_lock);
  1834. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1835. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1836. spin_lock(&inode->i_lock);
  1837. __nfs_access_zap_cache(NFS_I(inode), &head);
  1838. spin_unlock(&inode->i_lock);
  1839. spin_unlock(&nfs_access_lru_lock);
  1840. nfs_access_free_list(&head);
  1841. }
  1842. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1843. {
  1844. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1845. struct nfs_access_entry *entry;
  1846. while (n != NULL) {
  1847. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1848. if (cred < entry->cred)
  1849. n = n->rb_left;
  1850. else if (cred > entry->cred)
  1851. n = n->rb_right;
  1852. else
  1853. return entry;
  1854. }
  1855. return NULL;
  1856. }
  1857. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1858. {
  1859. struct nfs_inode *nfsi = NFS_I(inode);
  1860. struct nfs_access_entry *cache;
  1861. int err = -ENOENT;
  1862. spin_lock(&inode->i_lock);
  1863. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1864. goto out_zap;
  1865. cache = nfs_access_search_rbtree(inode, cred);
  1866. if (cache == NULL)
  1867. goto out;
  1868. if (!nfs_have_delegated_attributes(inode) &&
  1869. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1870. goto out_stale;
  1871. res->jiffies = cache->jiffies;
  1872. res->cred = cache->cred;
  1873. res->mask = cache->mask;
  1874. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1875. err = 0;
  1876. out:
  1877. spin_unlock(&inode->i_lock);
  1878. return err;
  1879. out_stale:
  1880. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1881. list_del(&cache->lru);
  1882. spin_unlock(&inode->i_lock);
  1883. nfs_access_free_entry(cache);
  1884. return -ENOENT;
  1885. out_zap:
  1886. spin_unlock(&inode->i_lock);
  1887. nfs_access_zap_cache(inode);
  1888. return -ENOENT;
  1889. }
  1890. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1891. {
  1892. struct nfs_inode *nfsi = NFS_I(inode);
  1893. struct rb_root *root_node = &nfsi->access_cache;
  1894. struct rb_node **p = &root_node->rb_node;
  1895. struct rb_node *parent = NULL;
  1896. struct nfs_access_entry *entry;
  1897. spin_lock(&inode->i_lock);
  1898. while (*p != NULL) {
  1899. parent = *p;
  1900. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1901. if (set->cred < entry->cred)
  1902. p = &parent->rb_left;
  1903. else if (set->cred > entry->cred)
  1904. p = &parent->rb_right;
  1905. else
  1906. goto found;
  1907. }
  1908. rb_link_node(&set->rb_node, parent, p);
  1909. rb_insert_color(&set->rb_node, root_node);
  1910. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1911. spin_unlock(&inode->i_lock);
  1912. return;
  1913. found:
  1914. rb_replace_node(parent, &set->rb_node, root_node);
  1915. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1916. list_del(&entry->lru);
  1917. spin_unlock(&inode->i_lock);
  1918. nfs_access_free_entry(entry);
  1919. }
  1920. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1921. {
  1922. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1923. if (cache == NULL)
  1924. return;
  1925. RB_CLEAR_NODE(&cache->rb_node);
  1926. cache->jiffies = set->jiffies;
  1927. cache->cred = get_rpccred(set->cred);
  1928. cache->mask = set->mask;
  1929. nfs_access_add_rbtree(inode, cache);
  1930. /* Update accounting */
  1931. smp_mb__before_atomic_inc();
  1932. atomic_long_inc(&nfs_access_nr_entries);
  1933. smp_mb__after_atomic_inc();
  1934. /* Add inode to global LRU list */
  1935. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1936. spin_lock(&nfs_access_lru_lock);
  1937. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1938. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1939. &nfs_access_lru_list);
  1940. spin_unlock(&nfs_access_lru_lock);
  1941. }
  1942. }
  1943. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1944. {
  1945. struct nfs_access_entry cache;
  1946. int status;
  1947. status = nfs_access_get_cached(inode, cred, &cache);
  1948. if (status == 0)
  1949. goto out;
  1950. /* Be clever: ask server to check for all possible rights */
  1951. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1952. cache.cred = cred;
  1953. cache.jiffies = jiffies;
  1954. status = NFS_PROTO(inode)->access(inode, &cache);
  1955. if (status != 0) {
  1956. if (status == -ESTALE) {
  1957. nfs_zap_caches(inode);
  1958. if (!S_ISDIR(inode->i_mode))
  1959. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1960. }
  1961. return status;
  1962. }
  1963. nfs_access_add_cache(inode, &cache);
  1964. out:
  1965. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1966. return 0;
  1967. return -EACCES;
  1968. }
  1969. static int nfs_open_permission_mask(int openflags)
  1970. {
  1971. int mask = 0;
  1972. if ((openflags & O_ACCMODE) != O_WRONLY)
  1973. mask |= MAY_READ;
  1974. if ((openflags & O_ACCMODE) != O_RDONLY)
  1975. mask |= MAY_WRITE;
  1976. if (openflags & __FMODE_EXEC)
  1977. mask |= MAY_EXEC;
  1978. return mask;
  1979. }
  1980. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1981. {
  1982. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1983. }
  1984. int nfs_permission(struct inode *inode, int mask)
  1985. {
  1986. struct rpc_cred *cred;
  1987. int res = 0;
  1988. if (mask & MAY_NOT_BLOCK)
  1989. return -ECHILD;
  1990. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1991. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1992. goto out;
  1993. /* Is this sys_access() ? */
  1994. if (mask & (MAY_ACCESS | MAY_CHDIR))
  1995. goto force_lookup;
  1996. switch (inode->i_mode & S_IFMT) {
  1997. case S_IFLNK:
  1998. goto out;
  1999. case S_IFREG:
  2000. /* NFSv4 has atomic_open... */
  2001. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  2002. && (mask & MAY_OPEN)
  2003. && !(mask & MAY_EXEC))
  2004. goto out;
  2005. break;
  2006. case S_IFDIR:
  2007. /*
  2008. * Optimize away all write operations, since the server
  2009. * will check permissions when we perform the op.
  2010. */
  2011. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2012. goto out;
  2013. }
  2014. force_lookup:
  2015. if (!NFS_PROTO(inode)->access)
  2016. goto out_notsup;
  2017. cred = rpc_lookup_cred();
  2018. if (!IS_ERR(cred)) {
  2019. res = nfs_do_access(inode, cred, mask);
  2020. put_rpccred(cred);
  2021. } else
  2022. res = PTR_ERR(cred);
  2023. out:
  2024. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  2025. res = -EACCES;
  2026. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  2027. inode->i_sb->s_id, inode->i_ino, mask, res);
  2028. return res;
  2029. out_notsup:
  2030. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2031. if (res == 0)
  2032. res = generic_permission(inode, mask);
  2033. goto out;
  2034. }
  2035. /*
  2036. * Local variables:
  2037. * version-control: t
  2038. * kept-new-versions: 5
  2039. * End:
  2040. */