dir.c 57 KB

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