dir.c 57 KB

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