namei.c 60 KB

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