dir.c 59 KB

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