dir.c 58 KB

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