dir.c 56 KB

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