dir.c 67 KB

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