dir.c 55 KB

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