dir.c 57 KB

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