namei.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/personality.h>
  24. #include <linux/security.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/file.h>
  29. #include <asm/namei.h>
  30. #include <asm/uaccess.h>
  31. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  32. /* [Feb-1997 T. Schoebel-Theuer]
  33. * Fundamental changes in the pathname lookup mechanisms (namei)
  34. * were necessary because of omirr. The reason is that omirr needs
  35. * to know the _real_ pathname, not the user-supplied one, in case
  36. * of symlinks (and also when transname replacements occur).
  37. *
  38. * The new code replaces the old recursive symlink resolution with
  39. * an iterative one (in case of non-nested symlink chains). It does
  40. * this with calls to <fs>_follow_link().
  41. * As a side effect, dir_namei(), _namei() and follow_link() are now
  42. * replaced with a single function lookup_dentry() that can handle all
  43. * the special cases of the former code.
  44. *
  45. * With the new dcache, the pathname is stored at each inode, at least as
  46. * long as the refcount of the inode is positive. As a side effect, the
  47. * size of the dcache depends on the inode cache and thus is dynamic.
  48. *
  49. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  50. * resolution to correspond with current state of the code.
  51. *
  52. * Note that the symlink resolution is not *completely* iterative.
  53. * There is still a significant amount of tail- and mid- recursion in
  54. * the algorithm. Also, note that <fs>_readlink() is not used in
  55. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  56. * may return different results than <fs>_follow_link(). Many virtual
  57. * filesystems (including /proc) exhibit this behavior.
  58. */
  59. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  60. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  61. * and the name already exists in form of a symlink, try to create the new
  62. * name indicated by the symlink. The old code always complained that the
  63. * name already exists, due to not following the symlink even if its target
  64. * is nonexistent. The new semantics affects also mknod() and link() when
  65. * the name is a symlink pointing to a non-existant name.
  66. *
  67. * I don't know which semantics is the right one, since I have no access
  68. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  69. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  70. * "old" one. Personally, I think the new semantics is much more logical.
  71. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  72. * file does succeed in both HP-UX and SunOs, but not in Solaris
  73. * and in the old Linux semantics.
  74. */
  75. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  76. * semantics. See the comments in "open_namei" and "do_link" below.
  77. *
  78. * [10-Sep-98 Alan Modra] Another symlink change.
  79. */
  80. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  81. * inside the path - always follow.
  82. * in the last component in creation/removal/renaming - never follow.
  83. * if LOOKUP_FOLLOW passed - follow.
  84. * if the pathname has trailing slashes - follow.
  85. * otherwise - don't follow.
  86. * (applied in that order).
  87. *
  88. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  89. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  90. * During the 2.4 we need to fix the userland stuff depending on it -
  91. * hopefully we will be able to get rid of that wart in 2.5. So far only
  92. * XEmacs seems to be relying on it...
  93. */
  94. /*
  95. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  96. * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
  97. * any extra contention...
  98. */
  99. /* In order to reduce some races, while at the same time doing additional
  100. * checking and hopefully speeding things up, we copy filenames to the
  101. * kernel data space before using them..
  102. *
  103. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  104. * PATH_MAX includes the nul terminator --RR.
  105. */
  106. static inline int do_getname(const char __user *filename, char *page)
  107. {
  108. int retval;
  109. unsigned long len = PATH_MAX;
  110. if (!segment_eq(get_fs(), KERNEL_DS)) {
  111. if ((unsigned long) filename >= TASK_SIZE)
  112. return -EFAULT;
  113. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  114. len = TASK_SIZE - (unsigned long) filename;
  115. }
  116. retval = strncpy_from_user(page, filename, len);
  117. if (retval > 0) {
  118. if (retval < len)
  119. return 0;
  120. return -ENAMETOOLONG;
  121. } else if (!retval)
  122. retval = -ENOENT;
  123. return retval;
  124. }
  125. char * getname(const char __user * filename)
  126. {
  127. char *tmp, *result;
  128. result = ERR_PTR(-ENOMEM);
  129. tmp = __getname();
  130. if (tmp) {
  131. int retval = do_getname(filename, tmp);
  132. result = tmp;
  133. if (retval < 0) {
  134. __putname(tmp);
  135. result = ERR_PTR(retval);
  136. }
  137. }
  138. audit_getname(result);
  139. return result;
  140. }
  141. #ifdef CONFIG_AUDITSYSCALL
  142. void putname(const char *name)
  143. {
  144. if (unlikely(current->audit_context))
  145. audit_putname(name);
  146. else
  147. __putname(name);
  148. }
  149. EXPORT_SYMBOL(putname);
  150. #endif
  151. /**
  152. * generic_permission - check for access rights on a Posix-like filesystem
  153. * @inode: inode to check access rights for
  154. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  155. * @check_acl: optional callback to check for Posix ACLs
  156. *
  157. * Used to check for read/write/execute permissions on a file.
  158. * We use "fsuid" for this, letting us set arbitrary permissions
  159. * for filesystem access without changing the "normal" uids which
  160. * are used for other things..
  161. */
  162. int generic_permission(struct inode *inode, int mask,
  163. int (*check_acl)(struct inode *inode, int mask))
  164. {
  165. umode_t mode = inode->i_mode;
  166. if (current->fsuid == inode->i_uid)
  167. mode >>= 6;
  168. else {
  169. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  170. int error = check_acl(inode, mask);
  171. if (error == -EACCES)
  172. goto check_capabilities;
  173. else if (error != -EAGAIN)
  174. return error;
  175. }
  176. if (in_group_p(inode->i_gid))
  177. mode >>= 3;
  178. }
  179. /*
  180. * If the DACs are ok we don't need any capability check.
  181. */
  182. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  183. return 0;
  184. check_capabilities:
  185. /*
  186. * Read/write DACs are always overridable.
  187. * Executable DACs are overridable if at least one exec bit is set.
  188. */
  189. if (!(mask & MAY_EXEC) ||
  190. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  191. if (capable(CAP_DAC_OVERRIDE))
  192. return 0;
  193. /*
  194. * Searching includes executable on directories, else just read.
  195. */
  196. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  197. if (capable(CAP_DAC_READ_SEARCH))
  198. return 0;
  199. return -EACCES;
  200. }
  201. int permission(struct inode *inode, int mask, struct nameidata *nd)
  202. {
  203. int retval, submask;
  204. if (mask & MAY_WRITE) {
  205. umode_t mode = inode->i_mode;
  206. /*
  207. * Nobody gets write access to a read-only fs.
  208. */
  209. if (IS_RDONLY(inode) &&
  210. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  211. return -EROFS;
  212. /*
  213. * Nobody gets write access to an immutable file.
  214. */
  215. if (IS_IMMUTABLE(inode))
  216. return -EACCES;
  217. }
  218. /* Ordinary permission routines do not understand MAY_APPEND. */
  219. submask = mask & ~MAY_APPEND;
  220. if (inode->i_op && inode->i_op->permission)
  221. retval = inode->i_op->permission(inode, submask, nd);
  222. else
  223. retval = generic_permission(inode, submask, NULL);
  224. if (retval)
  225. return retval;
  226. return security_inode_permission(inode, mask, nd);
  227. }
  228. /**
  229. * vfs_permission - check for access rights to a given path
  230. * @nd: lookup result that describes the path
  231. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  232. *
  233. * Used to check for read/write/execute permissions on a path.
  234. * We use "fsuid" for this, letting us set arbitrary permissions
  235. * for filesystem access without changing the "normal" uids which
  236. * are used for other things.
  237. */
  238. int vfs_permission(struct nameidata *nd, int mask)
  239. {
  240. return permission(nd->dentry->d_inode, mask, nd);
  241. }
  242. /**
  243. * file_permission - check for additional access rights to a given file
  244. * @file: file to check access rights for
  245. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  246. *
  247. * Used to check for read/write/execute permissions on an already opened
  248. * file.
  249. *
  250. * Note:
  251. * Do not use this function in new code. All access checks should
  252. * be done using vfs_permission().
  253. */
  254. int file_permission(struct file *file, int mask)
  255. {
  256. return permission(file->f_dentry->d_inode, mask, NULL);
  257. }
  258. /*
  259. * get_write_access() gets write permission for a file.
  260. * put_write_access() releases this write permission.
  261. * This is used for regular files.
  262. * We cannot support write (and maybe mmap read-write shared) accesses and
  263. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  264. * can have the following values:
  265. * 0: no writers, no VM_DENYWRITE mappings
  266. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  267. * > 0: (i_writecount) users are writing to the file.
  268. *
  269. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  270. * except for the cases where we don't hold i_writecount yet. Then we need to
  271. * use {get,deny}_write_access() - these functions check the sign and refuse
  272. * to do the change if sign is wrong. Exclusion between them is provided by
  273. * the inode->i_lock spinlock.
  274. */
  275. int get_write_access(struct inode * inode)
  276. {
  277. spin_lock(&inode->i_lock);
  278. if (atomic_read(&inode->i_writecount) < 0) {
  279. spin_unlock(&inode->i_lock);
  280. return -ETXTBSY;
  281. }
  282. atomic_inc(&inode->i_writecount);
  283. spin_unlock(&inode->i_lock);
  284. return 0;
  285. }
  286. int deny_write_access(struct file * file)
  287. {
  288. struct inode *inode = file->f_dentry->d_inode;
  289. spin_lock(&inode->i_lock);
  290. if (atomic_read(&inode->i_writecount) > 0) {
  291. spin_unlock(&inode->i_lock);
  292. return -ETXTBSY;
  293. }
  294. atomic_dec(&inode->i_writecount);
  295. spin_unlock(&inode->i_lock);
  296. return 0;
  297. }
  298. void path_release(struct nameidata *nd)
  299. {
  300. dput(nd->dentry);
  301. mntput(nd->mnt);
  302. }
  303. /*
  304. * umount() mustn't call path_release()/mntput() as that would clear
  305. * mnt_expiry_mark
  306. */
  307. void path_release_on_umount(struct nameidata *nd)
  308. {
  309. dput(nd->dentry);
  310. mntput_no_expire(nd->mnt);
  311. }
  312. /**
  313. * release_open_intent - free up open intent resources
  314. * @nd: pointer to nameidata
  315. */
  316. void release_open_intent(struct nameidata *nd)
  317. {
  318. if (nd->intent.open.file->f_dentry == NULL)
  319. put_filp(nd->intent.open.file);
  320. else
  321. fput(nd->intent.open.file);
  322. }
  323. /*
  324. * Internal lookup() using the new generic dcache.
  325. * SMP-safe
  326. */
  327. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  328. {
  329. struct dentry * dentry = __d_lookup(parent, name);
  330. /* lockess __d_lookup may fail due to concurrent d_move()
  331. * in some unrelated directory, so try with d_lookup
  332. */
  333. if (!dentry)
  334. dentry = d_lookup(parent, name);
  335. if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
  336. if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
  337. dput(dentry);
  338. dentry = NULL;
  339. }
  340. }
  341. return dentry;
  342. }
  343. /*
  344. * Short-cut version of permission(), for calling by
  345. * path_walk(), when dcache lock is held. Combines parts
  346. * of permission() and generic_permission(), and tests ONLY for
  347. * MAY_EXEC permission.
  348. *
  349. * If appropriate, check DAC only. If not appropriate, or
  350. * short-cut DAC fails, then call permission() to do more
  351. * complete permission check.
  352. */
  353. static inline int exec_permission_lite(struct inode *inode,
  354. struct nameidata *nd)
  355. {
  356. umode_t mode = inode->i_mode;
  357. if (inode->i_op && inode->i_op->permission)
  358. return -EAGAIN;
  359. if (current->fsuid == inode->i_uid)
  360. mode >>= 6;
  361. else if (in_group_p(inode->i_gid))
  362. mode >>= 3;
  363. if (mode & MAY_EXEC)
  364. goto ok;
  365. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  366. goto ok;
  367. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  368. goto ok;
  369. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  370. goto ok;
  371. return -EACCES;
  372. ok:
  373. return security_inode_permission(inode, MAY_EXEC, nd);
  374. }
  375. /*
  376. * This is called when everything else fails, and we actually have
  377. * to go to the low-level filesystem to find out what we should do..
  378. *
  379. * We get the directory semaphore, and after getting that we also
  380. * make sure that nobody added the entry to the dcache in the meantime..
  381. * SMP-safe
  382. */
  383. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  384. {
  385. struct dentry * result;
  386. struct inode *dir = parent->d_inode;
  387. down(&dir->i_sem);
  388. /*
  389. * First re-do the cached lookup just in case it was created
  390. * while we waited for the directory semaphore..
  391. *
  392. * FIXME! This could use version numbering or similar to
  393. * avoid unnecessary cache lookups.
  394. *
  395. * The "dcache_lock" is purely to protect the RCU list walker
  396. * from concurrent renames at this point (we mustn't get false
  397. * negatives from the RCU list walk here, unlike the optimistic
  398. * fast walk).
  399. *
  400. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  401. */
  402. result = d_lookup(parent, name);
  403. if (!result) {
  404. struct dentry * dentry = d_alloc(parent, name);
  405. result = ERR_PTR(-ENOMEM);
  406. if (dentry) {
  407. result = dir->i_op->lookup(dir, dentry, nd);
  408. if (result)
  409. dput(dentry);
  410. else
  411. result = dentry;
  412. }
  413. up(&dir->i_sem);
  414. return result;
  415. }
  416. /*
  417. * Uhhuh! Nasty case: the cache was re-populated while
  418. * we waited on the semaphore. Need to revalidate.
  419. */
  420. up(&dir->i_sem);
  421. if (result->d_op && result->d_op->d_revalidate) {
  422. if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
  423. dput(result);
  424. result = ERR_PTR(-ENOENT);
  425. }
  426. }
  427. return result;
  428. }
  429. static int __emul_lookup_dentry(const char *, struct nameidata *);
  430. /* SMP-safe */
  431. static inline int
  432. walk_init_root(const char *name, struct nameidata *nd)
  433. {
  434. read_lock(&current->fs->lock);
  435. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  436. nd->mnt = mntget(current->fs->altrootmnt);
  437. nd->dentry = dget(current->fs->altroot);
  438. read_unlock(&current->fs->lock);
  439. if (__emul_lookup_dentry(name,nd))
  440. return 0;
  441. read_lock(&current->fs->lock);
  442. }
  443. nd->mnt = mntget(current->fs->rootmnt);
  444. nd->dentry = dget(current->fs->root);
  445. read_unlock(&current->fs->lock);
  446. return 1;
  447. }
  448. static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  449. {
  450. int res = 0;
  451. char *name;
  452. if (IS_ERR(link))
  453. goto fail;
  454. if (*link == '/') {
  455. path_release(nd);
  456. if (!walk_init_root(link, nd))
  457. /* weird __emul_prefix() stuff did it */
  458. goto out;
  459. }
  460. res = link_path_walk(link, nd);
  461. out:
  462. if (nd->depth || res || nd->last_type!=LAST_NORM)
  463. return res;
  464. /*
  465. * If it is an iterative symlinks resolution in open_namei() we
  466. * have to copy the last component. And all that crap because of
  467. * bloody create() on broken symlinks. Furrfu...
  468. */
  469. name = __getname();
  470. if (unlikely(!name)) {
  471. path_release(nd);
  472. return -ENOMEM;
  473. }
  474. strcpy(name, nd->last.name);
  475. nd->last.name = name;
  476. return 0;
  477. fail:
  478. path_release(nd);
  479. return PTR_ERR(link);
  480. }
  481. struct path {
  482. struct vfsmount *mnt;
  483. struct dentry *dentry;
  484. };
  485. static inline int __do_follow_link(struct path *path, struct nameidata *nd)
  486. {
  487. int error;
  488. void *cookie;
  489. struct dentry *dentry = path->dentry;
  490. touch_atime(path->mnt, dentry);
  491. nd_set_link(nd, NULL);
  492. if (path->mnt == nd->mnt)
  493. mntget(path->mnt);
  494. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  495. error = PTR_ERR(cookie);
  496. if (!IS_ERR(cookie)) {
  497. char *s = nd_get_link(nd);
  498. error = 0;
  499. if (s)
  500. error = __vfs_follow_link(nd, s);
  501. if (dentry->d_inode->i_op->put_link)
  502. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  503. }
  504. dput(dentry);
  505. mntput(path->mnt);
  506. return error;
  507. }
  508. static inline void dput_path(struct path *path, struct nameidata *nd)
  509. {
  510. dput(path->dentry);
  511. if (path->mnt != nd->mnt)
  512. mntput(path->mnt);
  513. }
  514. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  515. {
  516. dput(nd->dentry);
  517. if (nd->mnt != path->mnt)
  518. mntput(nd->mnt);
  519. nd->mnt = path->mnt;
  520. nd->dentry = path->dentry;
  521. }
  522. /*
  523. * This limits recursive symlink follows to 8, while
  524. * limiting consecutive symlinks to 40.
  525. *
  526. * Without that kind of total limit, nasty chains of consecutive
  527. * symlinks can cause almost arbitrarily long lookups.
  528. */
  529. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  530. {
  531. int err = -ELOOP;
  532. if (current->link_count >= MAX_NESTED_LINKS)
  533. goto loop;
  534. if (current->total_link_count >= 40)
  535. goto loop;
  536. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  537. cond_resched();
  538. err = security_inode_follow_link(path->dentry, nd);
  539. if (err)
  540. goto loop;
  541. current->link_count++;
  542. current->total_link_count++;
  543. nd->depth++;
  544. err = __do_follow_link(path, nd);
  545. current->link_count--;
  546. nd->depth--;
  547. return err;
  548. loop:
  549. dput_path(path, nd);
  550. path_release(nd);
  551. return err;
  552. }
  553. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  554. {
  555. struct vfsmount *parent;
  556. struct dentry *mountpoint;
  557. spin_lock(&vfsmount_lock);
  558. parent=(*mnt)->mnt_parent;
  559. if (parent == *mnt) {
  560. spin_unlock(&vfsmount_lock);
  561. return 0;
  562. }
  563. mntget(parent);
  564. mountpoint=dget((*mnt)->mnt_mountpoint);
  565. spin_unlock(&vfsmount_lock);
  566. dput(*dentry);
  567. *dentry = mountpoint;
  568. mntput(*mnt);
  569. *mnt = parent;
  570. return 1;
  571. }
  572. /* no need for dcache_lock, as serialization is taken care in
  573. * namespace.c
  574. */
  575. static int __follow_mount(struct path *path)
  576. {
  577. int res = 0;
  578. while (d_mountpoint(path->dentry)) {
  579. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  580. if (!mounted)
  581. break;
  582. dput(path->dentry);
  583. if (res)
  584. mntput(path->mnt);
  585. path->mnt = mounted;
  586. path->dentry = dget(mounted->mnt_root);
  587. res = 1;
  588. }
  589. return res;
  590. }
  591. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  592. {
  593. while (d_mountpoint(*dentry)) {
  594. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  595. if (!mounted)
  596. break;
  597. dput(*dentry);
  598. mntput(*mnt);
  599. *mnt = mounted;
  600. *dentry = dget(mounted->mnt_root);
  601. }
  602. }
  603. /* no need for dcache_lock, as serialization is taken care in
  604. * namespace.c
  605. */
  606. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  607. {
  608. struct vfsmount *mounted;
  609. mounted = lookup_mnt(*mnt, *dentry);
  610. if (mounted) {
  611. dput(*dentry);
  612. mntput(*mnt);
  613. *mnt = mounted;
  614. *dentry = dget(mounted->mnt_root);
  615. return 1;
  616. }
  617. return 0;
  618. }
  619. static inline void follow_dotdot(struct nameidata *nd)
  620. {
  621. while(1) {
  622. struct vfsmount *parent;
  623. struct dentry *old = nd->dentry;
  624. read_lock(&current->fs->lock);
  625. if (nd->dentry == current->fs->root &&
  626. nd->mnt == current->fs->rootmnt) {
  627. read_unlock(&current->fs->lock);
  628. break;
  629. }
  630. read_unlock(&current->fs->lock);
  631. spin_lock(&dcache_lock);
  632. if (nd->dentry != nd->mnt->mnt_root) {
  633. nd->dentry = dget(nd->dentry->d_parent);
  634. spin_unlock(&dcache_lock);
  635. dput(old);
  636. break;
  637. }
  638. spin_unlock(&dcache_lock);
  639. spin_lock(&vfsmount_lock);
  640. parent = nd->mnt->mnt_parent;
  641. if (parent == nd->mnt) {
  642. spin_unlock(&vfsmount_lock);
  643. break;
  644. }
  645. mntget(parent);
  646. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  647. spin_unlock(&vfsmount_lock);
  648. dput(old);
  649. mntput(nd->mnt);
  650. nd->mnt = parent;
  651. }
  652. follow_mount(&nd->mnt, &nd->dentry);
  653. }
  654. /*
  655. * It's more convoluted than I'd like it to be, but... it's still fairly
  656. * small and for now I'd prefer to have fast path as straight as possible.
  657. * It _is_ time-critical.
  658. */
  659. static int do_lookup(struct nameidata *nd, struct qstr *name,
  660. struct path *path)
  661. {
  662. struct vfsmount *mnt = nd->mnt;
  663. struct dentry *dentry = __d_lookup(nd->dentry, name);
  664. if (!dentry)
  665. goto need_lookup;
  666. if (dentry->d_op && dentry->d_op->d_revalidate)
  667. goto need_revalidate;
  668. done:
  669. path->mnt = mnt;
  670. path->dentry = dentry;
  671. __follow_mount(path);
  672. return 0;
  673. need_lookup:
  674. dentry = real_lookup(nd->dentry, name, nd);
  675. if (IS_ERR(dentry))
  676. goto fail;
  677. goto done;
  678. need_revalidate:
  679. if (dentry->d_op->d_revalidate(dentry, nd))
  680. goto done;
  681. if (d_invalidate(dentry))
  682. goto done;
  683. dput(dentry);
  684. goto need_lookup;
  685. fail:
  686. return PTR_ERR(dentry);
  687. }
  688. /*
  689. * Name resolution.
  690. * This is the basic name resolution function, turning a pathname into
  691. * the final dentry. We expect 'base' to be positive and a directory.
  692. *
  693. * Returns 0 and nd will have valid dentry and mnt on success.
  694. * Returns error and drops reference to input namei data on failure.
  695. */
  696. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  697. {
  698. struct path next;
  699. struct inode *inode;
  700. int err;
  701. unsigned int lookup_flags = nd->flags;
  702. while (*name=='/')
  703. name++;
  704. if (!*name)
  705. goto return_reval;
  706. inode = nd->dentry->d_inode;
  707. if (nd->depth)
  708. lookup_flags = LOOKUP_FOLLOW;
  709. /* At this point we know we have a real path component. */
  710. for(;;) {
  711. unsigned long hash;
  712. struct qstr this;
  713. unsigned int c;
  714. nd->flags |= LOOKUP_CONTINUE;
  715. err = exec_permission_lite(inode, nd);
  716. if (err == -EAGAIN)
  717. err = vfs_permission(nd, MAY_EXEC);
  718. if (err)
  719. break;
  720. this.name = name;
  721. c = *(const unsigned char *)name;
  722. hash = init_name_hash();
  723. do {
  724. name++;
  725. hash = partial_name_hash(c, hash);
  726. c = *(const unsigned char *)name;
  727. } while (c && (c != '/'));
  728. this.len = name - (const char *) this.name;
  729. this.hash = end_name_hash(hash);
  730. /* remove trailing slashes? */
  731. if (!c)
  732. goto last_component;
  733. while (*++name == '/');
  734. if (!*name)
  735. goto last_with_slashes;
  736. /*
  737. * "." and ".." are special - ".." especially so because it has
  738. * to be able to know about the current root directory and
  739. * parent relationships.
  740. */
  741. if (this.name[0] == '.') switch (this.len) {
  742. default:
  743. break;
  744. case 2:
  745. if (this.name[1] != '.')
  746. break;
  747. follow_dotdot(nd);
  748. inode = nd->dentry->d_inode;
  749. /* fallthrough */
  750. case 1:
  751. continue;
  752. }
  753. /*
  754. * See if the low-level filesystem might want
  755. * to use its own hash..
  756. */
  757. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  758. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  759. if (err < 0)
  760. break;
  761. }
  762. /* This does the actual lookups.. */
  763. err = do_lookup(nd, &this, &next);
  764. if (err)
  765. break;
  766. err = -ENOENT;
  767. inode = next.dentry->d_inode;
  768. if (!inode)
  769. goto out_dput;
  770. err = -ENOTDIR;
  771. if (!inode->i_op)
  772. goto out_dput;
  773. if (inode->i_op->follow_link) {
  774. err = do_follow_link(&next, nd);
  775. if (err)
  776. goto return_err;
  777. err = -ENOENT;
  778. inode = nd->dentry->d_inode;
  779. if (!inode)
  780. break;
  781. err = -ENOTDIR;
  782. if (!inode->i_op)
  783. break;
  784. } else
  785. path_to_nameidata(&next, nd);
  786. err = -ENOTDIR;
  787. if (!inode->i_op->lookup)
  788. break;
  789. continue;
  790. /* here ends the main loop */
  791. last_with_slashes:
  792. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  793. last_component:
  794. nd->flags &= ~LOOKUP_CONTINUE;
  795. if (lookup_flags & LOOKUP_PARENT)
  796. goto lookup_parent;
  797. if (this.name[0] == '.') switch (this.len) {
  798. default:
  799. break;
  800. case 2:
  801. if (this.name[1] != '.')
  802. break;
  803. follow_dotdot(nd);
  804. inode = nd->dentry->d_inode;
  805. /* fallthrough */
  806. case 1:
  807. goto return_reval;
  808. }
  809. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  810. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  811. if (err < 0)
  812. break;
  813. }
  814. err = do_lookup(nd, &this, &next);
  815. if (err)
  816. break;
  817. inode = next.dentry->d_inode;
  818. if ((lookup_flags & LOOKUP_FOLLOW)
  819. && inode && inode->i_op && inode->i_op->follow_link) {
  820. err = do_follow_link(&next, nd);
  821. if (err)
  822. goto return_err;
  823. inode = nd->dentry->d_inode;
  824. } else
  825. path_to_nameidata(&next, nd);
  826. err = -ENOENT;
  827. if (!inode)
  828. break;
  829. if (lookup_flags & LOOKUP_DIRECTORY) {
  830. err = -ENOTDIR;
  831. if (!inode->i_op || !inode->i_op->lookup)
  832. break;
  833. }
  834. goto return_base;
  835. lookup_parent:
  836. nd->last = this;
  837. nd->last_type = LAST_NORM;
  838. if (this.name[0] != '.')
  839. goto return_base;
  840. if (this.len == 1)
  841. nd->last_type = LAST_DOT;
  842. else if (this.len == 2 && this.name[1] == '.')
  843. nd->last_type = LAST_DOTDOT;
  844. else
  845. goto return_base;
  846. return_reval:
  847. /*
  848. * We bypassed the ordinary revalidation routines.
  849. * We may need to check the cached dentry for staleness.
  850. */
  851. if (nd->dentry && nd->dentry->d_sb &&
  852. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  853. err = -ESTALE;
  854. /* Note: we do not d_invalidate() */
  855. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  856. break;
  857. }
  858. return_base:
  859. return 0;
  860. out_dput:
  861. dput_path(&next, nd);
  862. break;
  863. }
  864. path_release(nd);
  865. return_err:
  866. return err;
  867. }
  868. /*
  869. * Wrapper to retry pathname resolution whenever the underlying
  870. * file system returns an ESTALE.
  871. *
  872. * Retry the whole path once, forcing real lookup requests
  873. * instead of relying on the dcache.
  874. */
  875. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  876. {
  877. struct nameidata save = *nd;
  878. int result;
  879. /* make sure the stuff we saved doesn't go away */
  880. dget(save.dentry);
  881. mntget(save.mnt);
  882. result = __link_path_walk(name, nd);
  883. if (result == -ESTALE) {
  884. *nd = save;
  885. dget(nd->dentry);
  886. mntget(nd->mnt);
  887. nd->flags |= LOOKUP_REVAL;
  888. result = __link_path_walk(name, nd);
  889. }
  890. dput(save.dentry);
  891. mntput(save.mnt);
  892. return result;
  893. }
  894. int fastcall path_walk(const char * name, struct nameidata *nd)
  895. {
  896. current->total_link_count = 0;
  897. return link_path_walk(name, nd);
  898. }
  899. /*
  900. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  901. * everything is done. Returns 0 and drops input nd, if lookup failed;
  902. */
  903. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  904. {
  905. if (path_walk(name, nd))
  906. return 0; /* something went wrong... */
  907. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  908. struct dentry *old_dentry = nd->dentry;
  909. struct vfsmount *old_mnt = nd->mnt;
  910. struct qstr last = nd->last;
  911. int last_type = nd->last_type;
  912. /*
  913. * NAME was not found in alternate root or it's a directory. Try to find
  914. * it in the normal root:
  915. */
  916. nd->last_type = LAST_ROOT;
  917. read_lock(&current->fs->lock);
  918. nd->mnt = mntget(current->fs->rootmnt);
  919. nd->dentry = dget(current->fs->root);
  920. read_unlock(&current->fs->lock);
  921. if (path_walk(name, nd) == 0) {
  922. if (nd->dentry->d_inode) {
  923. dput(old_dentry);
  924. mntput(old_mnt);
  925. return 1;
  926. }
  927. path_release(nd);
  928. }
  929. nd->dentry = old_dentry;
  930. nd->mnt = old_mnt;
  931. nd->last = last;
  932. nd->last_type = last_type;
  933. }
  934. return 1;
  935. }
  936. void set_fs_altroot(void)
  937. {
  938. char *emul = __emul_prefix();
  939. struct nameidata nd;
  940. struct vfsmount *mnt = NULL, *oldmnt;
  941. struct dentry *dentry = NULL, *olddentry;
  942. int err;
  943. if (!emul)
  944. goto set_it;
  945. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  946. if (!err) {
  947. mnt = nd.mnt;
  948. dentry = nd.dentry;
  949. }
  950. set_it:
  951. write_lock(&current->fs->lock);
  952. oldmnt = current->fs->altrootmnt;
  953. olddentry = current->fs->altroot;
  954. current->fs->altrootmnt = mnt;
  955. current->fs->altroot = dentry;
  956. write_unlock(&current->fs->lock);
  957. if (olddentry) {
  958. dput(olddentry);
  959. mntput(oldmnt);
  960. }
  961. }
  962. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  963. int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
  964. {
  965. int retval = 0;
  966. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  967. nd->flags = flags;
  968. nd->depth = 0;
  969. read_lock(&current->fs->lock);
  970. if (*name=='/') {
  971. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  972. nd->mnt = mntget(current->fs->altrootmnt);
  973. nd->dentry = dget(current->fs->altroot);
  974. read_unlock(&current->fs->lock);
  975. if (__emul_lookup_dentry(name,nd))
  976. goto out; /* found in altroot */
  977. read_lock(&current->fs->lock);
  978. }
  979. nd->mnt = mntget(current->fs->rootmnt);
  980. nd->dentry = dget(current->fs->root);
  981. } else {
  982. nd->mnt = mntget(current->fs->pwdmnt);
  983. nd->dentry = dget(current->fs->pwd);
  984. }
  985. read_unlock(&current->fs->lock);
  986. current->total_link_count = 0;
  987. retval = link_path_walk(name, nd);
  988. out:
  989. if (unlikely(current->audit_context
  990. && nd && nd->dentry && nd->dentry->d_inode))
  991. audit_inode(name, nd->dentry->d_inode, flags);
  992. return retval;
  993. }
  994. static int __path_lookup_intent_open(const char *name, unsigned int lookup_flags,
  995. struct nameidata *nd, int open_flags, int create_mode)
  996. {
  997. struct file *filp = get_empty_filp();
  998. int err;
  999. if (filp == NULL)
  1000. return -ENFILE;
  1001. nd->intent.open.file = filp;
  1002. nd->intent.open.flags = open_flags;
  1003. nd->intent.open.create_mode = create_mode;
  1004. err = path_lookup(name, lookup_flags|LOOKUP_OPEN, nd);
  1005. if (IS_ERR(nd->intent.open.file)) {
  1006. if (err == 0) {
  1007. err = PTR_ERR(nd->intent.open.file);
  1008. path_release(nd);
  1009. }
  1010. } else if (err != 0)
  1011. release_open_intent(nd);
  1012. return err;
  1013. }
  1014. /**
  1015. * path_lookup_open - lookup a file path with open intent
  1016. * @name: pointer to file name
  1017. * @lookup_flags: lookup intent flags
  1018. * @nd: pointer to nameidata
  1019. * @open_flags: open intent flags
  1020. */
  1021. int path_lookup_open(const char *name, unsigned int lookup_flags,
  1022. struct nameidata *nd, int open_flags)
  1023. {
  1024. return __path_lookup_intent_open(name, lookup_flags, nd,
  1025. open_flags, 0);
  1026. }
  1027. /**
  1028. * path_lookup_create - lookup a file path with open + create intent
  1029. * @name: pointer to file name
  1030. * @lookup_flags: lookup intent flags
  1031. * @nd: pointer to nameidata
  1032. * @open_flags: open intent flags
  1033. * @create_mode: create intent flags
  1034. */
  1035. int path_lookup_create(const char *name, unsigned int lookup_flags,
  1036. struct nameidata *nd, int open_flags, int create_mode)
  1037. {
  1038. return __path_lookup_intent_open(name, lookup_flags|LOOKUP_CREATE, nd,
  1039. open_flags, create_mode);
  1040. }
  1041. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1042. struct nameidata *nd, int open_flags)
  1043. {
  1044. char *tmp = getname(name);
  1045. int err = PTR_ERR(tmp);
  1046. if (!IS_ERR(tmp)) {
  1047. err = __path_lookup_intent_open(tmp, lookup_flags, nd, open_flags, 0);
  1048. putname(tmp);
  1049. }
  1050. return err;
  1051. }
  1052. /*
  1053. * Restricted form of lookup. Doesn't follow links, single-component only,
  1054. * needs parent already locked. Doesn't follow mounts.
  1055. * SMP-safe.
  1056. */
  1057. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  1058. {
  1059. struct dentry * dentry;
  1060. struct inode *inode;
  1061. int err;
  1062. inode = base->d_inode;
  1063. err = permission(inode, MAY_EXEC, nd);
  1064. dentry = ERR_PTR(err);
  1065. if (err)
  1066. goto out;
  1067. /*
  1068. * See if the low-level filesystem might want
  1069. * to use its own hash..
  1070. */
  1071. if (base->d_op && base->d_op->d_hash) {
  1072. err = base->d_op->d_hash(base, name);
  1073. dentry = ERR_PTR(err);
  1074. if (err < 0)
  1075. goto out;
  1076. }
  1077. dentry = cached_lookup(base, name, nd);
  1078. if (!dentry) {
  1079. struct dentry *new = d_alloc(base, name);
  1080. dentry = ERR_PTR(-ENOMEM);
  1081. if (!new)
  1082. goto out;
  1083. dentry = inode->i_op->lookup(inode, new, nd);
  1084. if (!dentry)
  1085. dentry = new;
  1086. else
  1087. dput(new);
  1088. }
  1089. out:
  1090. return dentry;
  1091. }
  1092. struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
  1093. {
  1094. return __lookup_hash(name, base, NULL);
  1095. }
  1096. /* SMP-safe */
  1097. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  1098. {
  1099. unsigned long hash;
  1100. struct qstr this;
  1101. unsigned int c;
  1102. this.name = name;
  1103. this.len = len;
  1104. if (!len)
  1105. goto access;
  1106. hash = init_name_hash();
  1107. while (len--) {
  1108. c = *(const unsigned char *)name++;
  1109. if (c == '/' || c == '\0')
  1110. goto access;
  1111. hash = partial_name_hash(c, hash);
  1112. }
  1113. this.hash = end_name_hash(hash);
  1114. return lookup_hash(&this, base);
  1115. access:
  1116. return ERR_PTR(-EACCES);
  1117. }
  1118. /*
  1119. * namei()
  1120. *
  1121. * is used by most simple commands to get the inode of a specified name.
  1122. * Open, link etc use their own routines, but this is enough for things
  1123. * like 'chmod' etc.
  1124. *
  1125. * namei exists in two versions: namei/lnamei. The only difference is
  1126. * that namei follows links, while lnamei does not.
  1127. * SMP-safe
  1128. */
  1129. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1130. {
  1131. char *tmp = getname(name);
  1132. int err = PTR_ERR(tmp);
  1133. if (!IS_ERR(tmp)) {
  1134. err = path_lookup(tmp, flags, nd);
  1135. putname(tmp);
  1136. }
  1137. return err;
  1138. }
  1139. /*
  1140. * It's inline, so penalty for filesystems that don't use sticky bit is
  1141. * minimal.
  1142. */
  1143. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1144. {
  1145. if (!(dir->i_mode & S_ISVTX))
  1146. return 0;
  1147. if (inode->i_uid == current->fsuid)
  1148. return 0;
  1149. if (dir->i_uid == current->fsuid)
  1150. return 0;
  1151. return !capable(CAP_FOWNER);
  1152. }
  1153. /*
  1154. * Check whether we can remove a link victim from directory dir, check
  1155. * whether the type of victim is right.
  1156. * 1. We can't do it if dir is read-only (done in permission())
  1157. * 2. We should have write and exec permissions on dir
  1158. * 3. We can't remove anything from append-only dir
  1159. * 4. We can't do anything with immutable dir (done in permission())
  1160. * 5. If the sticky bit on dir is set we should either
  1161. * a. be owner of dir, or
  1162. * b. be owner of victim, or
  1163. * c. have CAP_FOWNER capability
  1164. * 6. If the victim is append-only or immutable we can't do antyhing with
  1165. * links pointing to it.
  1166. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1167. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1168. * 9. We can't remove a root or mountpoint.
  1169. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1170. * nfs_async_unlink().
  1171. */
  1172. static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1173. {
  1174. int error;
  1175. if (!victim->d_inode)
  1176. return -ENOENT;
  1177. BUG_ON(victim->d_parent->d_inode != dir);
  1178. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1179. if (error)
  1180. return error;
  1181. if (IS_APPEND(dir))
  1182. return -EPERM;
  1183. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1184. IS_IMMUTABLE(victim->d_inode))
  1185. return -EPERM;
  1186. if (isdir) {
  1187. if (!S_ISDIR(victim->d_inode->i_mode))
  1188. return -ENOTDIR;
  1189. if (IS_ROOT(victim))
  1190. return -EBUSY;
  1191. } else if (S_ISDIR(victim->d_inode->i_mode))
  1192. return -EISDIR;
  1193. if (IS_DEADDIR(dir))
  1194. return -ENOENT;
  1195. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1196. return -EBUSY;
  1197. return 0;
  1198. }
  1199. /* Check whether we can create an object with dentry child in directory
  1200. * dir.
  1201. * 1. We can't do it if child already exists (open has special treatment for
  1202. * this case, but since we are inlined it's OK)
  1203. * 2. We can't do it if dir is read-only (done in permission())
  1204. * 3. We should have write and exec permissions on dir
  1205. * 4. We can't do it if dir is immutable (done in permission())
  1206. */
  1207. static inline int may_create(struct inode *dir, struct dentry *child,
  1208. struct nameidata *nd)
  1209. {
  1210. if (child->d_inode)
  1211. return -EEXIST;
  1212. if (IS_DEADDIR(dir))
  1213. return -ENOENT;
  1214. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1215. }
  1216. /*
  1217. * O_DIRECTORY translates into forcing a directory lookup.
  1218. */
  1219. static inline int lookup_flags(unsigned int f)
  1220. {
  1221. unsigned long retval = LOOKUP_FOLLOW;
  1222. if (f & O_NOFOLLOW)
  1223. retval &= ~LOOKUP_FOLLOW;
  1224. if (f & O_DIRECTORY)
  1225. retval |= LOOKUP_DIRECTORY;
  1226. return retval;
  1227. }
  1228. /*
  1229. * p1 and p2 should be directories on the same fs.
  1230. */
  1231. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1232. {
  1233. struct dentry *p;
  1234. if (p1 == p2) {
  1235. down(&p1->d_inode->i_sem);
  1236. return NULL;
  1237. }
  1238. down(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1239. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1240. if (p->d_parent == p2) {
  1241. down(&p2->d_inode->i_sem);
  1242. down(&p1->d_inode->i_sem);
  1243. return p;
  1244. }
  1245. }
  1246. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1247. if (p->d_parent == p1) {
  1248. down(&p1->d_inode->i_sem);
  1249. down(&p2->d_inode->i_sem);
  1250. return p;
  1251. }
  1252. }
  1253. down(&p1->d_inode->i_sem);
  1254. down(&p2->d_inode->i_sem);
  1255. return NULL;
  1256. }
  1257. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1258. {
  1259. up(&p1->d_inode->i_sem);
  1260. if (p1 != p2) {
  1261. up(&p2->d_inode->i_sem);
  1262. up(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1263. }
  1264. }
  1265. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1266. struct nameidata *nd)
  1267. {
  1268. int error = may_create(dir, dentry, nd);
  1269. if (error)
  1270. return error;
  1271. if (!dir->i_op || !dir->i_op->create)
  1272. return -EACCES; /* shouldn't it be ENOSYS? */
  1273. mode &= S_IALLUGO;
  1274. mode |= S_IFREG;
  1275. error = security_inode_create(dir, dentry, mode);
  1276. if (error)
  1277. return error;
  1278. DQUOT_INIT(dir);
  1279. error = dir->i_op->create(dir, dentry, mode, nd);
  1280. if (!error)
  1281. fsnotify_create(dir, dentry->d_name.name);
  1282. return error;
  1283. }
  1284. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1285. {
  1286. struct dentry *dentry = nd->dentry;
  1287. struct inode *inode = dentry->d_inode;
  1288. int error;
  1289. if (!inode)
  1290. return -ENOENT;
  1291. if (S_ISLNK(inode->i_mode))
  1292. return -ELOOP;
  1293. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1294. return -EISDIR;
  1295. error = vfs_permission(nd, acc_mode);
  1296. if (error)
  1297. return error;
  1298. /*
  1299. * FIFO's, sockets and device files are special: they don't
  1300. * actually live on the filesystem itself, and as such you
  1301. * can write to them even if the filesystem is read-only.
  1302. */
  1303. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1304. flag &= ~O_TRUNC;
  1305. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1306. if (nd->mnt->mnt_flags & MNT_NODEV)
  1307. return -EACCES;
  1308. flag &= ~O_TRUNC;
  1309. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1310. return -EROFS;
  1311. /*
  1312. * An append-only file must be opened in append mode for writing.
  1313. */
  1314. if (IS_APPEND(inode)) {
  1315. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1316. return -EPERM;
  1317. if (flag & O_TRUNC)
  1318. return -EPERM;
  1319. }
  1320. /* O_NOATIME can only be set by the owner or superuser */
  1321. if (flag & O_NOATIME)
  1322. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1323. return -EPERM;
  1324. /*
  1325. * Ensure there are no outstanding leases on the file.
  1326. */
  1327. error = break_lease(inode, flag);
  1328. if (error)
  1329. return error;
  1330. if (flag & O_TRUNC) {
  1331. error = get_write_access(inode);
  1332. if (error)
  1333. return error;
  1334. /*
  1335. * Refuse to truncate files with mandatory locks held on them.
  1336. */
  1337. error = locks_verify_locked(inode);
  1338. if (!error) {
  1339. DQUOT_INIT(inode);
  1340. error = do_truncate(dentry, 0, NULL);
  1341. }
  1342. put_write_access(inode);
  1343. if (error)
  1344. return error;
  1345. } else
  1346. if (flag & FMODE_WRITE)
  1347. DQUOT_INIT(inode);
  1348. return 0;
  1349. }
  1350. /*
  1351. * open_namei()
  1352. *
  1353. * namei for open - this is in fact almost the whole open-routine.
  1354. *
  1355. * Note that the low bits of "flag" aren't the same as in the open
  1356. * system call - they are 00 - no permissions needed
  1357. * 01 - read permission needed
  1358. * 10 - write permission needed
  1359. * 11 - read/write permissions needed
  1360. * which is a lot more logical, and also allows the "no perm" needed
  1361. * for symlinks (where the permissions are checked later).
  1362. * SMP-safe
  1363. */
  1364. int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
  1365. {
  1366. int acc_mode, error;
  1367. struct path path;
  1368. struct dentry *dir;
  1369. int count = 0;
  1370. acc_mode = ACC_MODE(flag);
  1371. /* O_TRUNC implies we need access checks for write permissions */
  1372. if (flag & O_TRUNC)
  1373. acc_mode |= MAY_WRITE;
  1374. /* Allow the LSM permission hook to distinguish append
  1375. access from general write access. */
  1376. if (flag & O_APPEND)
  1377. acc_mode |= MAY_APPEND;
  1378. /*
  1379. * The simplest case - just a plain lookup.
  1380. */
  1381. if (!(flag & O_CREAT)) {
  1382. error = path_lookup_open(pathname, lookup_flags(flag), nd, flag);
  1383. if (error)
  1384. return error;
  1385. goto ok;
  1386. }
  1387. /*
  1388. * Create - we need to know the parent.
  1389. */
  1390. error = path_lookup_create(pathname, LOOKUP_PARENT, nd, flag, mode);
  1391. if (error)
  1392. return error;
  1393. /*
  1394. * We have the parent and last component. First of all, check
  1395. * that we are not asked to creat(2) an obvious directory - that
  1396. * will not do.
  1397. */
  1398. error = -EISDIR;
  1399. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1400. goto exit;
  1401. dir = nd->dentry;
  1402. nd->flags &= ~LOOKUP_PARENT;
  1403. down(&dir->d_inode->i_sem);
  1404. path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1405. path.mnt = nd->mnt;
  1406. do_last:
  1407. error = PTR_ERR(path.dentry);
  1408. if (IS_ERR(path.dentry)) {
  1409. up(&dir->d_inode->i_sem);
  1410. goto exit;
  1411. }
  1412. /* Negative dentry, just create the file */
  1413. if (!path.dentry->d_inode) {
  1414. if (!IS_POSIXACL(dir->d_inode))
  1415. mode &= ~current->fs->umask;
  1416. error = vfs_create(dir->d_inode, path.dentry, mode, nd);
  1417. up(&dir->d_inode->i_sem);
  1418. dput(nd->dentry);
  1419. nd->dentry = path.dentry;
  1420. if (error)
  1421. goto exit;
  1422. /* Don't check for write permission, don't truncate */
  1423. acc_mode = 0;
  1424. flag &= ~O_TRUNC;
  1425. goto ok;
  1426. }
  1427. /*
  1428. * It already exists.
  1429. */
  1430. up(&dir->d_inode->i_sem);
  1431. error = -EEXIST;
  1432. if (flag & O_EXCL)
  1433. goto exit_dput;
  1434. if (__follow_mount(&path)) {
  1435. error = -ELOOP;
  1436. if (flag & O_NOFOLLOW)
  1437. goto exit_dput;
  1438. }
  1439. error = -ENOENT;
  1440. if (!path.dentry->d_inode)
  1441. goto exit_dput;
  1442. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1443. goto do_link;
  1444. path_to_nameidata(&path, nd);
  1445. error = -EISDIR;
  1446. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1447. goto exit;
  1448. ok:
  1449. error = may_open(nd, acc_mode, flag);
  1450. if (error)
  1451. goto exit;
  1452. return 0;
  1453. exit_dput:
  1454. dput_path(&path, nd);
  1455. exit:
  1456. if (!IS_ERR(nd->intent.open.file))
  1457. release_open_intent(nd);
  1458. path_release(nd);
  1459. return error;
  1460. do_link:
  1461. error = -ELOOP;
  1462. if (flag & O_NOFOLLOW)
  1463. goto exit_dput;
  1464. /*
  1465. * This is subtle. Instead of calling do_follow_link() we do the
  1466. * thing by hands. The reason is that this way we have zero link_count
  1467. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1468. * After that we have the parent and last component, i.e.
  1469. * we are in the same situation as after the first path_walk().
  1470. * Well, almost - if the last component is normal we get its copy
  1471. * stored in nd->last.name and we will have to putname() it when we
  1472. * are done. Procfs-like symlinks just set LAST_BIND.
  1473. */
  1474. nd->flags |= LOOKUP_PARENT;
  1475. error = security_inode_follow_link(path.dentry, nd);
  1476. if (error)
  1477. goto exit_dput;
  1478. error = __do_follow_link(&path, nd);
  1479. if (error)
  1480. return error;
  1481. nd->flags &= ~LOOKUP_PARENT;
  1482. if (nd->last_type == LAST_BIND)
  1483. goto ok;
  1484. error = -EISDIR;
  1485. if (nd->last_type != LAST_NORM)
  1486. goto exit;
  1487. if (nd->last.name[nd->last.len]) {
  1488. __putname(nd->last.name);
  1489. goto exit;
  1490. }
  1491. error = -ELOOP;
  1492. if (count++==32) {
  1493. __putname(nd->last.name);
  1494. goto exit;
  1495. }
  1496. dir = nd->dentry;
  1497. down(&dir->d_inode->i_sem);
  1498. path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1499. path.mnt = nd->mnt;
  1500. __putname(nd->last.name);
  1501. goto do_last;
  1502. }
  1503. /**
  1504. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1505. * @nd: nameidata info
  1506. * @is_dir: directory flag
  1507. *
  1508. * Simple function to lookup and return a dentry and create it
  1509. * if it doesn't exist. Is SMP-safe.
  1510. *
  1511. * Returns with nd->dentry->d_inode->i_sem locked.
  1512. */
  1513. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1514. {
  1515. struct dentry *dentry = ERR_PTR(-EEXIST);
  1516. down(&nd->dentry->d_inode->i_sem);
  1517. /*
  1518. * Yucky last component or no last component at all?
  1519. * (foo/., foo/.., /////)
  1520. */
  1521. if (nd->last_type != LAST_NORM)
  1522. goto fail;
  1523. nd->flags &= ~LOOKUP_PARENT;
  1524. /*
  1525. * Do the final lookup.
  1526. */
  1527. dentry = lookup_hash(&nd->last, nd->dentry);
  1528. if (IS_ERR(dentry))
  1529. goto fail;
  1530. /*
  1531. * Special case - lookup gave negative, but... we had foo/bar/
  1532. * From the vfs_mknod() POV we just have a negative dentry -
  1533. * all is fine. Let's be bastards - you had / on the end, you've
  1534. * been asking for (non-existent) directory. -ENOENT for you.
  1535. */
  1536. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1537. goto enoent;
  1538. return dentry;
  1539. enoent:
  1540. dput(dentry);
  1541. dentry = ERR_PTR(-ENOENT);
  1542. fail:
  1543. return dentry;
  1544. }
  1545. EXPORT_SYMBOL_GPL(lookup_create);
  1546. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1547. {
  1548. int error = may_create(dir, dentry, NULL);
  1549. if (error)
  1550. return error;
  1551. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1552. return -EPERM;
  1553. if (!dir->i_op || !dir->i_op->mknod)
  1554. return -EPERM;
  1555. error = security_inode_mknod(dir, dentry, mode, dev);
  1556. if (error)
  1557. return error;
  1558. DQUOT_INIT(dir);
  1559. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1560. if (!error)
  1561. fsnotify_create(dir, dentry->d_name.name);
  1562. return error;
  1563. }
  1564. asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
  1565. {
  1566. int error = 0;
  1567. char * tmp;
  1568. struct dentry * dentry;
  1569. struct nameidata nd;
  1570. if (S_ISDIR(mode))
  1571. return -EPERM;
  1572. tmp = getname(filename);
  1573. if (IS_ERR(tmp))
  1574. return PTR_ERR(tmp);
  1575. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1576. if (error)
  1577. goto out;
  1578. dentry = lookup_create(&nd, 0);
  1579. error = PTR_ERR(dentry);
  1580. if (!IS_POSIXACL(nd.dentry->d_inode))
  1581. mode &= ~current->fs->umask;
  1582. if (!IS_ERR(dentry)) {
  1583. switch (mode & S_IFMT) {
  1584. case 0: case S_IFREG:
  1585. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1586. break;
  1587. case S_IFCHR: case S_IFBLK:
  1588. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1589. new_decode_dev(dev));
  1590. break;
  1591. case S_IFIFO: case S_IFSOCK:
  1592. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1593. break;
  1594. case S_IFDIR:
  1595. error = -EPERM;
  1596. break;
  1597. default:
  1598. error = -EINVAL;
  1599. }
  1600. dput(dentry);
  1601. }
  1602. up(&nd.dentry->d_inode->i_sem);
  1603. path_release(&nd);
  1604. out:
  1605. putname(tmp);
  1606. return error;
  1607. }
  1608. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1609. {
  1610. int error = may_create(dir, dentry, NULL);
  1611. if (error)
  1612. return error;
  1613. if (!dir->i_op || !dir->i_op->mkdir)
  1614. return -EPERM;
  1615. mode &= (S_IRWXUGO|S_ISVTX);
  1616. error = security_inode_mkdir(dir, dentry, mode);
  1617. if (error)
  1618. return error;
  1619. DQUOT_INIT(dir);
  1620. error = dir->i_op->mkdir(dir, dentry, mode);
  1621. if (!error)
  1622. fsnotify_mkdir(dir, dentry->d_name.name);
  1623. return error;
  1624. }
  1625. asmlinkage long sys_mkdir(const char __user * pathname, int mode)
  1626. {
  1627. int error = 0;
  1628. char * tmp;
  1629. tmp = getname(pathname);
  1630. error = PTR_ERR(tmp);
  1631. if (!IS_ERR(tmp)) {
  1632. struct dentry *dentry;
  1633. struct nameidata nd;
  1634. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1635. if (error)
  1636. goto out;
  1637. dentry = lookup_create(&nd, 1);
  1638. error = PTR_ERR(dentry);
  1639. if (!IS_ERR(dentry)) {
  1640. if (!IS_POSIXACL(nd.dentry->d_inode))
  1641. mode &= ~current->fs->umask;
  1642. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1643. dput(dentry);
  1644. }
  1645. up(&nd.dentry->d_inode->i_sem);
  1646. path_release(&nd);
  1647. out:
  1648. putname(tmp);
  1649. }
  1650. return error;
  1651. }
  1652. /*
  1653. * We try to drop the dentry early: we should have
  1654. * a usage count of 2 if we're the only user of this
  1655. * dentry, and if that is true (possibly after pruning
  1656. * the dcache), then we drop the dentry now.
  1657. *
  1658. * A low-level filesystem can, if it choses, legally
  1659. * do a
  1660. *
  1661. * if (!d_unhashed(dentry))
  1662. * return -EBUSY;
  1663. *
  1664. * if it cannot handle the case of removing a directory
  1665. * that is still in use by something else..
  1666. */
  1667. void dentry_unhash(struct dentry *dentry)
  1668. {
  1669. dget(dentry);
  1670. if (atomic_read(&dentry->d_count))
  1671. shrink_dcache_parent(dentry);
  1672. spin_lock(&dcache_lock);
  1673. spin_lock(&dentry->d_lock);
  1674. if (atomic_read(&dentry->d_count) == 2)
  1675. __d_drop(dentry);
  1676. spin_unlock(&dentry->d_lock);
  1677. spin_unlock(&dcache_lock);
  1678. }
  1679. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1680. {
  1681. int error = may_delete(dir, dentry, 1);
  1682. if (error)
  1683. return error;
  1684. if (!dir->i_op || !dir->i_op->rmdir)
  1685. return -EPERM;
  1686. DQUOT_INIT(dir);
  1687. down(&dentry->d_inode->i_sem);
  1688. dentry_unhash(dentry);
  1689. if (d_mountpoint(dentry))
  1690. error = -EBUSY;
  1691. else {
  1692. error = security_inode_rmdir(dir, dentry);
  1693. if (!error) {
  1694. error = dir->i_op->rmdir(dir, dentry);
  1695. if (!error)
  1696. dentry->d_inode->i_flags |= S_DEAD;
  1697. }
  1698. }
  1699. up(&dentry->d_inode->i_sem);
  1700. if (!error) {
  1701. d_delete(dentry);
  1702. }
  1703. dput(dentry);
  1704. return error;
  1705. }
  1706. asmlinkage long sys_rmdir(const char __user * pathname)
  1707. {
  1708. int error = 0;
  1709. char * name;
  1710. struct dentry *dentry;
  1711. struct nameidata nd;
  1712. name = getname(pathname);
  1713. if(IS_ERR(name))
  1714. return PTR_ERR(name);
  1715. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1716. if (error)
  1717. goto exit;
  1718. switch(nd.last_type) {
  1719. case LAST_DOTDOT:
  1720. error = -ENOTEMPTY;
  1721. goto exit1;
  1722. case LAST_DOT:
  1723. error = -EINVAL;
  1724. goto exit1;
  1725. case LAST_ROOT:
  1726. error = -EBUSY;
  1727. goto exit1;
  1728. }
  1729. down(&nd.dentry->d_inode->i_sem);
  1730. dentry = lookup_hash(&nd.last, nd.dentry);
  1731. error = PTR_ERR(dentry);
  1732. if (!IS_ERR(dentry)) {
  1733. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1734. dput(dentry);
  1735. }
  1736. up(&nd.dentry->d_inode->i_sem);
  1737. exit1:
  1738. path_release(&nd);
  1739. exit:
  1740. putname(name);
  1741. return error;
  1742. }
  1743. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1744. {
  1745. int error = may_delete(dir, dentry, 0);
  1746. if (error)
  1747. return error;
  1748. if (!dir->i_op || !dir->i_op->unlink)
  1749. return -EPERM;
  1750. DQUOT_INIT(dir);
  1751. down(&dentry->d_inode->i_sem);
  1752. if (d_mountpoint(dentry))
  1753. error = -EBUSY;
  1754. else {
  1755. error = security_inode_unlink(dir, dentry);
  1756. if (!error)
  1757. error = dir->i_op->unlink(dir, dentry);
  1758. }
  1759. up(&dentry->d_inode->i_sem);
  1760. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1761. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1762. d_delete(dentry);
  1763. }
  1764. return error;
  1765. }
  1766. /*
  1767. * Make sure that the actual truncation of the file will occur outside its
  1768. * directory's i_sem. Truncate can take a long time if there is a lot of
  1769. * writeout happening, and we don't want to prevent access to the directory
  1770. * while waiting on the I/O.
  1771. */
  1772. asmlinkage long sys_unlink(const char __user * pathname)
  1773. {
  1774. int error = 0;
  1775. char * name;
  1776. struct dentry *dentry;
  1777. struct nameidata nd;
  1778. struct inode *inode = NULL;
  1779. name = getname(pathname);
  1780. if(IS_ERR(name))
  1781. return PTR_ERR(name);
  1782. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1783. if (error)
  1784. goto exit;
  1785. error = -EISDIR;
  1786. if (nd.last_type != LAST_NORM)
  1787. goto exit1;
  1788. down(&nd.dentry->d_inode->i_sem);
  1789. dentry = lookup_hash(&nd.last, nd.dentry);
  1790. error = PTR_ERR(dentry);
  1791. if (!IS_ERR(dentry)) {
  1792. /* Why not before? Because we want correct error value */
  1793. if (nd.last.name[nd.last.len])
  1794. goto slashes;
  1795. inode = dentry->d_inode;
  1796. if (inode)
  1797. atomic_inc(&inode->i_count);
  1798. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1799. exit2:
  1800. dput(dentry);
  1801. }
  1802. up(&nd.dentry->d_inode->i_sem);
  1803. if (inode)
  1804. iput(inode); /* truncate the inode here */
  1805. exit1:
  1806. path_release(&nd);
  1807. exit:
  1808. putname(name);
  1809. return error;
  1810. slashes:
  1811. error = !dentry->d_inode ? -ENOENT :
  1812. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1813. goto exit2;
  1814. }
  1815. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1816. {
  1817. int error = may_create(dir, dentry, NULL);
  1818. if (error)
  1819. return error;
  1820. if (!dir->i_op || !dir->i_op->symlink)
  1821. return -EPERM;
  1822. error = security_inode_symlink(dir, dentry, oldname);
  1823. if (error)
  1824. return error;
  1825. DQUOT_INIT(dir);
  1826. error = dir->i_op->symlink(dir, dentry, oldname);
  1827. if (!error)
  1828. fsnotify_create(dir, dentry->d_name.name);
  1829. return error;
  1830. }
  1831. asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
  1832. {
  1833. int error = 0;
  1834. char * from;
  1835. char * to;
  1836. from = getname(oldname);
  1837. if(IS_ERR(from))
  1838. return PTR_ERR(from);
  1839. to = getname(newname);
  1840. error = PTR_ERR(to);
  1841. if (!IS_ERR(to)) {
  1842. struct dentry *dentry;
  1843. struct nameidata nd;
  1844. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1845. if (error)
  1846. goto out;
  1847. dentry = lookup_create(&nd, 0);
  1848. error = PTR_ERR(dentry);
  1849. if (!IS_ERR(dentry)) {
  1850. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1851. dput(dentry);
  1852. }
  1853. up(&nd.dentry->d_inode->i_sem);
  1854. path_release(&nd);
  1855. out:
  1856. putname(to);
  1857. }
  1858. putname(from);
  1859. return error;
  1860. }
  1861. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1862. {
  1863. struct inode *inode = old_dentry->d_inode;
  1864. int error;
  1865. if (!inode)
  1866. return -ENOENT;
  1867. error = may_create(dir, new_dentry, NULL);
  1868. if (error)
  1869. return error;
  1870. if (dir->i_sb != inode->i_sb)
  1871. return -EXDEV;
  1872. /*
  1873. * A link to an append-only or immutable file cannot be created.
  1874. */
  1875. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1876. return -EPERM;
  1877. if (!dir->i_op || !dir->i_op->link)
  1878. return -EPERM;
  1879. if (S_ISDIR(old_dentry->d_inode->i_mode))
  1880. return -EPERM;
  1881. error = security_inode_link(old_dentry, dir, new_dentry);
  1882. if (error)
  1883. return error;
  1884. down(&old_dentry->d_inode->i_sem);
  1885. DQUOT_INIT(dir);
  1886. error = dir->i_op->link(old_dentry, dir, new_dentry);
  1887. up(&old_dentry->d_inode->i_sem);
  1888. if (!error)
  1889. fsnotify_create(dir, new_dentry->d_name.name);
  1890. return error;
  1891. }
  1892. /*
  1893. * Hardlinks are often used in delicate situations. We avoid
  1894. * security-related surprises by not following symlinks on the
  1895. * newname. --KAB
  1896. *
  1897. * We don't follow them on the oldname either to be compatible
  1898. * with linux 2.0, and to avoid hard-linking to directories
  1899. * and other special files. --ADM
  1900. */
  1901. asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
  1902. {
  1903. struct dentry *new_dentry;
  1904. struct nameidata nd, old_nd;
  1905. int error;
  1906. char * to;
  1907. to = getname(newname);
  1908. if (IS_ERR(to))
  1909. return PTR_ERR(to);
  1910. error = __user_walk(oldname, 0, &old_nd);
  1911. if (error)
  1912. goto exit;
  1913. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1914. if (error)
  1915. goto out;
  1916. error = -EXDEV;
  1917. if (old_nd.mnt != nd.mnt)
  1918. goto out_release;
  1919. new_dentry = lookup_create(&nd, 0);
  1920. error = PTR_ERR(new_dentry);
  1921. if (!IS_ERR(new_dentry)) {
  1922. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  1923. dput(new_dentry);
  1924. }
  1925. up(&nd.dentry->d_inode->i_sem);
  1926. out_release:
  1927. path_release(&nd);
  1928. out:
  1929. path_release(&old_nd);
  1930. exit:
  1931. putname(to);
  1932. return error;
  1933. }
  1934. /*
  1935. * The worst of all namespace operations - renaming directory. "Perverted"
  1936. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  1937. * Problems:
  1938. * a) we can get into loop creation. Check is done in is_subdir().
  1939. * b) race potential - two innocent renames can create a loop together.
  1940. * That's where 4.4 screws up. Current fix: serialization on
  1941. * sb->s_vfs_rename_sem. We might be more accurate, but that's another
  1942. * story.
  1943. * c) we have to lock _three_ objects - parents and victim (if it exists).
  1944. * And that - after we got ->i_sem on parents (until then we don't know
  1945. * whether the target exists). Solution: try to be smart with locking
  1946. * order for inodes. We rely on the fact that tree topology may change
  1947. * only under ->s_vfs_rename_sem _and_ that parent of the object we
  1948. * move will be locked. Thus we can rank directories by the tree
  1949. * (ancestors first) and rank all non-directories after them.
  1950. * That works since everybody except rename does "lock parent, lookup,
  1951. * lock child" and rename is under ->s_vfs_rename_sem.
  1952. * HOWEVER, it relies on the assumption that any object with ->lookup()
  1953. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  1954. * we'd better make sure that there's no link(2) for them.
  1955. * d) some filesystems don't support opened-but-unlinked directories,
  1956. * either because of layout or because they are not ready to deal with
  1957. * all cases correctly. The latter will be fixed (taking this sort of
  1958. * stuff into VFS), but the former is not going away. Solution: the same
  1959. * trick as in rmdir().
  1960. * e) conversion from fhandle to dentry may come in the wrong moment - when
  1961. * we are removing the target. Solution: we will have to grab ->i_sem
  1962. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  1963. * ->i_sem on parents, which works but leads to some truely excessive
  1964. * locking].
  1965. */
  1966. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  1967. struct inode *new_dir, struct dentry *new_dentry)
  1968. {
  1969. int error = 0;
  1970. struct inode *target;
  1971. /*
  1972. * If we are going to change the parent - check write permissions,
  1973. * we'll need to flip '..'.
  1974. */
  1975. if (new_dir != old_dir) {
  1976. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  1977. if (error)
  1978. return error;
  1979. }
  1980. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1981. if (error)
  1982. return error;
  1983. target = new_dentry->d_inode;
  1984. if (target) {
  1985. down(&target->i_sem);
  1986. dentry_unhash(new_dentry);
  1987. }
  1988. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1989. error = -EBUSY;
  1990. else
  1991. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1992. if (target) {
  1993. if (!error)
  1994. target->i_flags |= S_DEAD;
  1995. up(&target->i_sem);
  1996. if (d_unhashed(new_dentry))
  1997. d_rehash(new_dentry);
  1998. dput(new_dentry);
  1999. }
  2000. if (!error)
  2001. d_move(old_dentry,new_dentry);
  2002. return error;
  2003. }
  2004. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2005. struct inode *new_dir, struct dentry *new_dentry)
  2006. {
  2007. struct inode *target;
  2008. int error;
  2009. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2010. if (error)
  2011. return error;
  2012. dget(new_dentry);
  2013. target = new_dentry->d_inode;
  2014. if (target)
  2015. down(&target->i_sem);
  2016. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2017. error = -EBUSY;
  2018. else
  2019. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2020. if (!error) {
  2021. /* The following d_move() should become unconditional */
  2022. if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
  2023. d_move(old_dentry, new_dentry);
  2024. }
  2025. if (target)
  2026. up(&target->i_sem);
  2027. dput(new_dentry);
  2028. return error;
  2029. }
  2030. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2031. struct inode *new_dir, struct dentry *new_dentry)
  2032. {
  2033. int error;
  2034. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2035. const char *old_name;
  2036. if (old_dentry->d_inode == new_dentry->d_inode)
  2037. return 0;
  2038. error = may_delete(old_dir, old_dentry, is_dir);
  2039. if (error)
  2040. return error;
  2041. if (!new_dentry->d_inode)
  2042. error = may_create(new_dir, new_dentry, NULL);
  2043. else
  2044. error = may_delete(new_dir, new_dentry, is_dir);
  2045. if (error)
  2046. return error;
  2047. if (!old_dir->i_op || !old_dir->i_op->rename)
  2048. return -EPERM;
  2049. DQUOT_INIT(old_dir);
  2050. DQUOT_INIT(new_dir);
  2051. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2052. if (is_dir)
  2053. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2054. else
  2055. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2056. if (!error) {
  2057. const char *new_name = old_dentry->d_name.name;
  2058. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2059. new_dentry->d_inode, old_dentry->d_inode);
  2060. }
  2061. fsnotify_oldname_free(old_name);
  2062. return error;
  2063. }
  2064. static inline int do_rename(const char * oldname, const char * newname)
  2065. {
  2066. int error = 0;
  2067. struct dentry * old_dir, * new_dir;
  2068. struct dentry * old_dentry, *new_dentry;
  2069. struct dentry * trap;
  2070. struct nameidata oldnd, newnd;
  2071. error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
  2072. if (error)
  2073. goto exit;
  2074. error = path_lookup(newname, LOOKUP_PARENT, &newnd);
  2075. if (error)
  2076. goto exit1;
  2077. error = -EXDEV;
  2078. if (oldnd.mnt != newnd.mnt)
  2079. goto exit2;
  2080. old_dir = oldnd.dentry;
  2081. error = -EBUSY;
  2082. if (oldnd.last_type != LAST_NORM)
  2083. goto exit2;
  2084. new_dir = newnd.dentry;
  2085. if (newnd.last_type != LAST_NORM)
  2086. goto exit2;
  2087. trap = lock_rename(new_dir, old_dir);
  2088. old_dentry = lookup_hash(&oldnd.last, old_dir);
  2089. error = PTR_ERR(old_dentry);
  2090. if (IS_ERR(old_dentry))
  2091. goto exit3;
  2092. /* source must exist */
  2093. error = -ENOENT;
  2094. if (!old_dentry->d_inode)
  2095. goto exit4;
  2096. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2097. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2098. error = -ENOTDIR;
  2099. if (oldnd.last.name[oldnd.last.len])
  2100. goto exit4;
  2101. if (newnd.last.name[newnd.last.len])
  2102. goto exit4;
  2103. }
  2104. /* source should not be ancestor of target */
  2105. error = -EINVAL;
  2106. if (old_dentry == trap)
  2107. goto exit4;
  2108. new_dentry = lookup_hash(&newnd.last, new_dir);
  2109. error = PTR_ERR(new_dentry);
  2110. if (IS_ERR(new_dentry))
  2111. goto exit4;
  2112. /* target should not be an ancestor of source */
  2113. error = -ENOTEMPTY;
  2114. if (new_dentry == trap)
  2115. goto exit5;
  2116. error = vfs_rename(old_dir->d_inode, old_dentry,
  2117. new_dir->d_inode, new_dentry);
  2118. exit5:
  2119. dput(new_dentry);
  2120. exit4:
  2121. dput(old_dentry);
  2122. exit3:
  2123. unlock_rename(new_dir, old_dir);
  2124. exit2:
  2125. path_release(&newnd);
  2126. exit1:
  2127. path_release(&oldnd);
  2128. exit:
  2129. return error;
  2130. }
  2131. asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
  2132. {
  2133. int error;
  2134. char * from;
  2135. char * to;
  2136. from = getname(oldname);
  2137. if(IS_ERR(from))
  2138. return PTR_ERR(from);
  2139. to = getname(newname);
  2140. error = PTR_ERR(to);
  2141. if (!IS_ERR(to)) {
  2142. error = do_rename(from,to);
  2143. putname(to);
  2144. }
  2145. putname(from);
  2146. return error;
  2147. }
  2148. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2149. {
  2150. int len;
  2151. len = PTR_ERR(link);
  2152. if (IS_ERR(link))
  2153. goto out;
  2154. len = strlen(link);
  2155. if (len > (unsigned) buflen)
  2156. len = buflen;
  2157. if (copy_to_user(buffer, link, len))
  2158. len = -EFAULT;
  2159. out:
  2160. return len;
  2161. }
  2162. /*
  2163. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2164. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2165. * using) it for any given inode is up to filesystem.
  2166. */
  2167. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2168. {
  2169. struct nameidata nd;
  2170. void *cookie;
  2171. nd.depth = 0;
  2172. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2173. if (!IS_ERR(cookie)) {
  2174. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2175. if (dentry->d_inode->i_op->put_link)
  2176. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2177. cookie = ERR_PTR(res);
  2178. }
  2179. return PTR_ERR(cookie);
  2180. }
  2181. int vfs_follow_link(struct nameidata *nd, const char *link)
  2182. {
  2183. return __vfs_follow_link(nd, link);
  2184. }
  2185. /* get the link contents into pagecache */
  2186. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2187. {
  2188. struct page * page;
  2189. struct address_space *mapping = dentry->d_inode->i_mapping;
  2190. page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
  2191. NULL);
  2192. if (IS_ERR(page))
  2193. goto sync_fail;
  2194. wait_on_page_locked(page);
  2195. if (!PageUptodate(page))
  2196. goto async_fail;
  2197. *ppage = page;
  2198. return kmap(page);
  2199. async_fail:
  2200. page_cache_release(page);
  2201. return ERR_PTR(-EIO);
  2202. sync_fail:
  2203. return (char*)page;
  2204. }
  2205. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2206. {
  2207. struct page *page = NULL;
  2208. char *s = page_getlink(dentry, &page);
  2209. int res = vfs_readlink(dentry,buffer,buflen,s);
  2210. if (page) {
  2211. kunmap(page);
  2212. page_cache_release(page);
  2213. }
  2214. return res;
  2215. }
  2216. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2217. {
  2218. struct page *page = NULL;
  2219. nd_set_link(nd, page_getlink(dentry, &page));
  2220. return page;
  2221. }
  2222. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2223. {
  2224. struct page *page = cookie;
  2225. if (page) {
  2226. kunmap(page);
  2227. page_cache_release(page);
  2228. }
  2229. }
  2230. int page_symlink(struct inode *inode, const char *symname, int len)
  2231. {
  2232. struct address_space *mapping = inode->i_mapping;
  2233. struct page *page = grab_cache_page(mapping, 0);
  2234. int err = -ENOMEM;
  2235. char *kaddr;
  2236. if (!page)
  2237. goto fail;
  2238. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2239. if (err)
  2240. goto fail_map;
  2241. kaddr = kmap_atomic(page, KM_USER0);
  2242. memcpy(kaddr, symname, len-1);
  2243. kunmap_atomic(kaddr, KM_USER0);
  2244. mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2245. /*
  2246. * Notice that we are _not_ going to block here - end of page is
  2247. * unmapped, so this will only try to map the rest of page, see
  2248. * that it is unmapped (typically even will not look into inode -
  2249. * ->i_size will be enough for everything) and zero it out.
  2250. * OTOH it's obviously correct and should make the page up-to-date.
  2251. */
  2252. if (!PageUptodate(page)) {
  2253. err = mapping->a_ops->readpage(NULL, page);
  2254. wait_on_page_locked(page);
  2255. } else {
  2256. unlock_page(page);
  2257. }
  2258. page_cache_release(page);
  2259. if (err < 0)
  2260. goto fail;
  2261. mark_inode_dirty(inode);
  2262. return 0;
  2263. fail_map:
  2264. unlock_page(page);
  2265. page_cache_release(page);
  2266. fail:
  2267. return err;
  2268. }
  2269. struct inode_operations page_symlink_inode_operations = {
  2270. .readlink = generic_readlink,
  2271. .follow_link = page_follow_link_light,
  2272. .put_link = page_put_link,
  2273. };
  2274. EXPORT_SYMBOL(__user_walk);
  2275. EXPORT_SYMBOL(follow_down);
  2276. EXPORT_SYMBOL(follow_up);
  2277. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2278. EXPORT_SYMBOL(getname);
  2279. EXPORT_SYMBOL(lock_rename);
  2280. EXPORT_SYMBOL(lookup_hash);
  2281. EXPORT_SYMBOL(lookup_one_len);
  2282. EXPORT_SYMBOL(page_follow_link_light);
  2283. EXPORT_SYMBOL(page_put_link);
  2284. EXPORT_SYMBOL(page_readlink);
  2285. EXPORT_SYMBOL(page_symlink);
  2286. EXPORT_SYMBOL(page_symlink_inode_operations);
  2287. EXPORT_SYMBOL(path_lookup);
  2288. EXPORT_SYMBOL(path_release);
  2289. EXPORT_SYMBOL(path_walk);
  2290. EXPORT_SYMBOL(permission);
  2291. EXPORT_SYMBOL(vfs_permission);
  2292. EXPORT_SYMBOL(file_permission);
  2293. EXPORT_SYMBOL(unlock_rename);
  2294. EXPORT_SYMBOL(vfs_create);
  2295. EXPORT_SYMBOL(vfs_follow_link);
  2296. EXPORT_SYMBOL(vfs_link);
  2297. EXPORT_SYMBOL(vfs_mkdir);
  2298. EXPORT_SYMBOL(vfs_mknod);
  2299. EXPORT_SYMBOL(generic_permission);
  2300. EXPORT_SYMBOL(vfs_readlink);
  2301. EXPORT_SYMBOL(vfs_rename);
  2302. EXPORT_SYMBOL(vfs_rmdir);
  2303. EXPORT_SYMBOL(vfs_symlink);
  2304. EXPORT_SYMBOL(vfs_unlink);
  2305. EXPORT_SYMBOL(dentry_unhash);
  2306. EXPORT_SYMBOL(generic_readlink);