dir.c 55 KB

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