dir.c 65 KB

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