dir.c 64 KB

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