dir.c 56 KB

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