dir.c 71 KB

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