dir.c 53 KB

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