dir.c 57 KB

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