dir.c 57 KB

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