namei.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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 void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  535. {
  536. while (d_mountpoint(*dentry)) {
  537. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  538. if (!mounted)
  539. break;
  540. dput(*dentry);
  541. mntput(*mnt);
  542. *mnt = mounted;
  543. *dentry = dget(mounted->mnt_root);
  544. }
  545. }
  546. /* no need for dcache_lock, as serialization is taken care in
  547. * namespace.c
  548. */
  549. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  550. {
  551. struct vfsmount *mounted;
  552. mounted = lookup_mnt(*mnt, *dentry);
  553. if (mounted) {
  554. dput(*dentry);
  555. mntput(*mnt);
  556. *mnt = mounted;
  557. *dentry = dget(mounted->mnt_root);
  558. return 1;
  559. }
  560. return 0;
  561. }
  562. static inline void follow_dotdot(struct nameidata *nd)
  563. {
  564. while(1) {
  565. struct vfsmount *parent;
  566. struct dentry *old = nd->dentry;
  567. read_lock(&current->fs->lock);
  568. if (nd->dentry == current->fs->root &&
  569. nd->mnt == current->fs->rootmnt) {
  570. read_unlock(&current->fs->lock);
  571. break;
  572. }
  573. read_unlock(&current->fs->lock);
  574. spin_lock(&dcache_lock);
  575. if (nd->dentry != nd->mnt->mnt_root) {
  576. nd->dentry = dget(nd->dentry->d_parent);
  577. spin_unlock(&dcache_lock);
  578. dput(old);
  579. break;
  580. }
  581. spin_unlock(&dcache_lock);
  582. spin_lock(&vfsmount_lock);
  583. parent = nd->mnt->mnt_parent;
  584. if (parent == nd->mnt) {
  585. spin_unlock(&vfsmount_lock);
  586. break;
  587. }
  588. mntget(parent);
  589. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  590. spin_unlock(&vfsmount_lock);
  591. dput(old);
  592. mntput(nd->mnt);
  593. nd->mnt = parent;
  594. }
  595. follow_mount(&nd->mnt, &nd->dentry);
  596. }
  597. /*
  598. * It's more convoluted than I'd like it to be, but... it's still fairly
  599. * small and for now I'd prefer to have fast path as straight as possible.
  600. * It _is_ time-critical.
  601. */
  602. static int do_lookup(struct nameidata *nd, struct qstr *name,
  603. struct path *path)
  604. {
  605. struct vfsmount *mnt = nd->mnt;
  606. struct dentry *dentry = __d_lookup(nd->dentry, name);
  607. if (!dentry)
  608. goto need_lookup;
  609. if (dentry->d_op && dentry->d_op->d_revalidate)
  610. goto need_revalidate;
  611. done:
  612. path->mnt = mnt;
  613. path->dentry = dentry;
  614. return 0;
  615. need_lookup:
  616. dentry = real_lookup(nd->dentry, name, nd);
  617. if (IS_ERR(dentry))
  618. goto fail;
  619. goto done;
  620. need_revalidate:
  621. if (dentry->d_op->d_revalidate(dentry, nd))
  622. goto done;
  623. if (d_invalidate(dentry))
  624. goto done;
  625. dput(dentry);
  626. goto need_lookup;
  627. fail:
  628. return PTR_ERR(dentry);
  629. }
  630. /*
  631. * Name resolution.
  632. * This is the basic name resolution function, turning a pathname into
  633. * the final dentry. We expect 'base' to be positive and a directory.
  634. *
  635. * Returns 0 and nd will have valid dentry and mnt on success.
  636. * Returns error and drops reference to input namei data on failure.
  637. */
  638. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  639. {
  640. struct path next;
  641. struct inode *inode;
  642. int err;
  643. unsigned int lookup_flags = nd->flags;
  644. while (*name=='/')
  645. name++;
  646. if (!*name)
  647. goto return_reval;
  648. inode = nd->dentry->d_inode;
  649. if (nd->depth)
  650. lookup_flags = LOOKUP_FOLLOW;
  651. /* At this point we know we have a real path component. */
  652. for(;;) {
  653. unsigned long hash;
  654. struct qstr this;
  655. unsigned int c;
  656. err = exec_permission_lite(inode, nd);
  657. if (err == -EAGAIN) {
  658. err = permission(inode, MAY_EXEC, nd);
  659. }
  660. if (err)
  661. break;
  662. this.name = name;
  663. c = *(const unsigned char *)name;
  664. hash = init_name_hash();
  665. do {
  666. name++;
  667. hash = partial_name_hash(c, hash);
  668. c = *(const unsigned char *)name;
  669. } while (c && (c != '/'));
  670. this.len = name - (const char *) this.name;
  671. this.hash = end_name_hash(hash);
  672. /* remove trailing slashes? */
  673. if (!c)
  674. goto last_component;
  675. while (*++name == '/');
  676. if (!*name)
  677. goto last_with_slashes;
  678. /*
  679. * "." and ".." are special - ".." especially so because it has
  680. * to be able to know about the current root directory and
  681. * parent relationships.
  682. */
  683. if (this.name[0] == '.') switch (this.len) {
  684. default:
  685. break;
  686. case 2:
  687. if (this.name[1] != '.')
  688. break;
  689. follow_dotdot(nd);
  690. inode = nd->dentry->d_inode;
  691. /* fallthrough */
  692. case 1:
  693. continue;
  694. }
  695. /*
  696. * See if the low-level filesystem might want
  697. * to use its own hash..
  698. */
  699. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  700. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  701. if (err < 0)
  702. break;
  703. }
  704. nd->flags |= LOOKUP_CONTINUE;
  705. /* This does the actual lookups.. */
  706. err = do_lookup(nd, &this, &next);
  707. if (err)
  708. break;
  709. /* Check mountpoints.. */
  710. __follow_mount(&next);
  711. err = -ENOENT;
  712. inode = next.dentry->d_inode;
  713. if (!inode)
  714. goto out_dput;
  715. err = -ENOTDIR;
  716. if (!inode->i_op)
  717. goto out_dput;
  718. if (inode->i_op->follow_link) {
  719. err = do_follow_link(&next, nd);
  720. if (err)
  721. goto return_err;
  722. err = -ENOENT;
  723. inode = nd->dentry->d_inode;
  724. if (!inode)
  725. break;
  726. err = -ENOTDIR;
  727. if (!inode->i_op)
  728. break;
  729. } else {
  730. dput(nd->dentry);
  731. if (nd->mnt != next.mnt)
  732. mntput(nd->mnt);
  733. nd->mnt = next.mnt;
  734. nd->dentry = next.dentry;
  735. }
  736. err = -ENOTDIR;
  737. if (!inode->i_op->lookup)
  738. break;
  739. continue;
  740. /* here ends the main loop */
  741. last_with_slashes:
  742. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  743. last_component:
  744. nd->flags &= ~LOOKUP_CONTINUE;
  745. if (lookup_flags & LOOKUP_PARENT)
  746. goto lookup_parent;
  747. if (this.name[0] == '.') switch (this.len) {
  748. default:
  749. break;
  750. case 2:
  751. if (this.name[1] != '.')
  752. break;
  753. follow_dotdot(nd);
  754. inode = nd->dentry->d_inode;
  755. /* fallthrough */
  756. case 1:
  757. goto return_reval;
  758. }
  759. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  760. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  761. if (err < 0)
  762. break;
  763. }
  764. err = do_lookup(nd, &this, &next);
  765. if (err)
  766. break;
  767. __follow_mount(&next);
  768. inode = next.dentry->d_inode;
  769. if ((lookup_flags & LOOKUP_FOLLOW)
  770. && inode && inode->i_op && inode->i_op->follow_link) {
  771. err = do_follow_link(&next, nd);
  772. if (err)
  773. goto return_err;
  774. inode = nd->dentry->d_inode;
  775. } else {
  776. dput(nd->dentry);
  777. if (nd->mnt != next.mnt)
  778. mntput(nd->mnt);
  779. nd->mnt = next.mnt;
  780. nd->dentry = next.dentry;
  781. }
  782. err = -ENOENT;
  783. if (!inode)
  784. break;
  785. if (lookup_flags & LOOKUP_DIRECTORY) {
  786. err = -ENOTDIR;
  787. if (!inode->i_op || !inode->i_op->lookup)
  788. break;
  789. }
  790. goto return_base;
  791. lookup_parent:
  792. nd->last = this;
  793. nd->last_type = LAST_NORM;
  794. if (this.name[0] != '.')
  795. goto return_base;
  796. if (this.len == 1)
  797. nd->last_type = LAST_DOT;
  798. else if (this.len == 2 && this.name[1] == '.')
  799. nd->last_type = LAST_DOTDOT;
  800. else
  801. goto return_base;
  802. return_reval:
  803. /*
  804. * We bypassed the ordinary revalidation routines.
  805. * We may need to check the cached dentry for staleness.
  806. */
  807. if (nd->dentry && nd->dentry->d_sb &&
  808. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  809. err = -ESTALE;
  810. /* Note: we do not d_invalidate() */
  811. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  812. break;
  813. }
  814. return_base:
  815. return 0;
  816. out_dput:
  817. dput(next.dentry);
  818. if (nd->mnt != next.mnt)
  819. mntput(next.mnt);
  820. break;
  821. }
  822. path_release(nd);
  823. return_err:
  824. return err;
  825. }
  826. /*
  827. * Wrapper to retry pathname resolution whenever the underlying
  828. * file system returns an ESTALE.
  829. *
  830. * Retry the whole path once, forcing real lookup requests
  831. * instead of relying on the dcache.
  832. */
  833. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  834. {
  835. struct nameidata save = *nd;
  836. int result;
  837. /* make sure the stuff we saved doesn't go away */
  838. dget(save.dentry);
  839. mntget(save.mnt);
  840. result = __link_path_walk(name, nd);
  841. if (result == -ESTALE) {
  842. *nd = save;
  843. dget(nd->dentry);
  844. mntget(nd->mnt);
  845. nd->flags |= LOOKUP_REVAL;
  846. result = __link_path_walk(name, nd);
  847. }
  848. dput(save.dentry);
  849. mntput(save.mnt);
  850. return result;
  851. }
  852. int fastcall path_walk(const char * name, struct nameidata *nd)
  853. {
  854. current->total_link_count = 0;
  855. return link_path_walk(name, nd);
  856. }
  857. /*
  858. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  859. * everything is done. Returns 0 and drops input nd, if lookup failed;
  860. */
  861. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  862. {
  863. if (path_walk(name, nd))
  864. return 0; /* something went wrong... */
  865. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  866. struct dentry *old_dentry = nd->dentry;
  867. struct vfsmount *old_mnt = nd->mnt;
  868. struct qstr last = nd->last;
  869. int last_type = nd->last_type;
  870. /*
  871. * NAME was not found in alternate root or it's a directory. Try to find
  872. * it in the normal root:
  873. */
  874. nd->last_type = LAST_ROOT;
  875. read_lock(&current->fs->lock);
  876. nd->mnt = mntget(current->fs->rootmnt);
  877. nd->dentry = dget(current->fs->root);
  878. read_unlock(&current->fs->lock);
  879. if (path_walk(name, nd) == 0) {
  880. if (nd->dentry->d_inode) {
  881. dput(old_dentry);
  882. mntput(old_mnt);
  883. return 1;
  884. }
  885. path_release(nd);
  886. }
  887. nd->dentry = old_dentry;
  888. nd->mnt = old_mnt;
  889. nd->last = last;
  890. nd->last_type = last_type;
  891. }
  892. return 1;
  893. }
  894. void set_fs_altroot(void)
  895. {
  896. char *emul = __emul_prefix();
  897. struct nameidata nd;
  898. struct vfsmount *mnt = NULL, *oldmnt;
  899. struct dentry *dentry = NULL, *olddentry;
  900. int err;
  901. if (!emul)
  902. goto set_it;
  903. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  904. if (!err) {
  905. mnt = nd.mnt;
  906. dentry = nd.dentry;
  907. }
  908. set_it:
  909. write_lock(&current->fs->lock);
  910. oldmnt = current->fs->altrootmnt;
  911. olddentry = current->fs->altroot;
  912. current->fs->altrootmnt = mnt;
  913. current->fs->altroot = dentry;
  914. write_unlock(&current->fs->lock);
  915. if (olddentry) {
  916. dput(olddentry);
  917. mntput(oldmnt);
  918. }
  919. }
  920. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  921. int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
  922. {
  923. int retval = 0;
  924. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  925. nd->flags = flags;
  926. nd->depth = 0;
  927. read_lock(&current->fs->lock);
  928. if (*name=='/') {
  929. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  930. nd->mnt = mntget(current->fs->altrootmnt);
  931. nd->dentry = dget(current->fs->altroot);
  932. read_unlock(&current->fs->lock);
  933. if (__emul_lookup_dentry(name,nd))
  934. goto out; /* found in altroot */
  935. read_lock(&current->fs->lock);
  936. }
  937. nd->mnt = mntget(current->fs->rootmnt);
  938. nd->dentry = dget(current->fs->root);
  939. } else {
  940. nd->mnt = mntget(current->fs->pwdmnt);
  941. nd->dentry = dget(current->fs->pwd);
  942. }
  943. read_unlock(&current->fs->lock);
  944. current->total_link_count = 0;
  945. retval = link_path_walk(name, nd);
  946. out:
  947. if (unlikely(current->audit_context
  948. && nd && nd->dentry && nd->dentry->d_inode))
  949. audit_inode(name, nd->dentry->d_inode);
  950. return retval;
  951. }
  952. /*
  953. * Restricted form of lookup. Doesn't follow links, single-component only,
  954. * needs parent already locked. Doesn't follow mounts.
  955. * SMP-safe.
  956. */
  957. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  958. {
  959. struct dentry * dentry;
  960. struct inode *inode;
  961. int err;
  962. inode = base->d_inode;
  963. err = permission(inode, MAY_EXEC, nd);
  964. dentry = ERR_PTR(err);
  965. if (err)
  966. goto out;
  967. /*
  968. * See if the low-level filesystem might want
  969. * to use its own hash..
  970. */
  971. if (base->d_op && base->d_op->d_hash) {
  972. err = base->d_op->d_hash(base, name);
  973. dentry = ERR_PTR(err);
  974. if (err < 0)
  975. goto out;
  976. }
  977. dentry = cached_lookup(base, name, nd);
  978. if (!dentry) {
  979. struct dentry *new = d_alloc(base, name);
  980. dentry = ERR_PTR(-ENOMEM);
  981. if (!new)
  982. goto out;
  983. dentry = inode->i_op->lookup(inode, new, nd);
  984. if (!dentry)
  985. dentry = new;
  986. else
  987. dput(new);
  988. }
  989. out:
  990. return dentry;
  991. }
  992. struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
  993. {
  994. return __lookup_hash(name, base, NULL);
  995. }
  996. /* SMP-safe */
  997. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  998. {
  999. unsigned long hash;
  1000. struct qstr this;
  1001. unsigned int c;
  1002. this.name = name;
  1003. this.len = len;
  1004. if (!len)
  1005. goto access;
  1006. hash = init_name_hash();
  1007. while (len--) {
  1008. c = *(const unsigned char *)name++;
  1009. if (c == '/' || c == '\0')
  1010. goto access;
  1011. hash = partial_name_hash(c, hash);
  1012. }
  1013. this.hash = end_name_hash(hash);
  1014. return lookup_hash(&this, base);
  1015. access:
  1016. return ERR_PTR(-EACCES);
  1017. }
  1018. /*
  1019. * namei()
  1020. *
  1021. * is used by most simple commands to get the inode of a specified name.
  1022. * Open, link etc use their own routines, but this is enough for things
  1023. * like 'chmod' etc.
  1024. *
  1025. * namei exists in two versions: namei/lnamei. The only difference is
  1026. * that namei follows links, while lnamei does not.
  1027. * SMP-safe
  1028. */
  1029. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1030. {
  1031. char *tmp = getname(name);
  1032. int err = PTR_ERR(tmp);
  1033. if (!IS_ERR(tmp)) {
  1034. err = path_lookup(tmp, flags, nd);
  1035. putname(tmp);
  1036. }
  1037. return err;
  1038. }
  1039. /*
  1040. * It's inline, so penalty for filesystems that don't use sticky bit is
  1041. * minimal.
  1042. */
  1043. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1044. {
  1045. if (!(dir->i_mode & S_ISVTX))
  1046. return 0;
  1047. if (inode->i_uid == current->fsuid)
  1048. return 0;
  1049. if (dir->i_uid == current->fsuid)
  1050. return 0;
  1051. return !capable(CAP_FOWNER);
  1052. }
  1053. /*
  1054. * Check whether we can remove a link victim from directory dir, check
  1055. * whether the type of victim is right.
  1056. * 1. We can't do it if dir is read-only (done in permission())
  1057. * 2. We should have write and exec permissions on dir
  1058. * 3. We can't remove anything from append-only dir
  1059. * 4. We can't do anything with immutable dir (done in permission())
  1060. * 5. If the sticky bit on dir is set we should either
  1061. * a. be owner of dir, or
  1062. * b. be owner of victim, or
  1063. * c. have CAP_FOWNER capability
  1064. * 6. If the victim is append-only or immutable we can't do antyhing with
  1065. * links pointing to it.
  1066. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1067. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1068. * 9. We can't remove a root or mountpoint.
  1069. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1070. * nfs_async_unlink().
  1071. */
  1072. static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1073. {
  1074. int error;
  1075. if (!victim->d_inode)
  1076. return -ENOENT;
  1077. BUG_ON(victim->d_parent->d_inode != dir);
  1078. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1079. if (error)
  1080. return error;
  1081. if (IS_APPEND(dir))
  1082. return -EPERM;
  1083. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1084. IS_IMMUTABLE(victim->d_inode))
  1085. return -EPERM;
  1086. if (isdir) {
  1087. if (!S_ISDIR(victim->d_inode->i_mode))
  1088. return -ENOTDIR;
  1089. if (IS_ROOT(victim))
  1090. return -EBUSY;
  1091. } else if (S_ISDIR(victim->d_inode->i_mode))
  1092. return -EISDIR;
  1093. if (IS_DEADDIR(dir))
  1094. return -ENOENT;
  1095. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1096. return -EBUSY;
  1097. return 0;
  1098. }
  1099. /* Check whether we can create an object with dentry child in directory
  1100. * dir.
  1101. * 1. We can't do it if child already exists (open has special treatment for
  1102. * this case, but since we are inlined it's OK)
  1103. * 2. We can't do it if dir is read-only (done in permission())
  1104. * 3. We should have write and exec permissions on dir
  1105. * 4. We can't do it if dir is immutable (done in permission())
  1106. */
  1107. static inline int may_create(struct inode *dir, struct dentry *child,
  1108. struct nameidata *nd)
  1109. {
  1110. if (child->d_inode)
  1111. return -EEXIST;
  1112. if (IS_DEADDIR(dir))
  1113. return -ENOENT;
  1114. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1115. }
  1116. /*
  1117. * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
  1118. * reasons.
  1119. *
  1120. * O_DIRECTORY translates into forcing a directory lookup.
  1121. */
  1122. static inline int lookup_flags(unsigned int f)
  1123. {
  1124. unsigned long retval = LOOKUP_FOLLOW;
  1125. if (f & O_NOFOLLOW)
  1126. retval &= ~LOOKUP_FOLLOW;
  1127. if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1128. retval &= ~LOOKUP_FOLLOW;
  1129. if (f & O_DIRECTORY)
  1130. retval |= LOOKUP_DIRECTORY;
  1131. return retval;
  1132. }
  1133. /*
  1134. * p1 and p2 should be directories on the same fs.
  1135. */
  1136. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1137. {
  1138. struct dentry *p;
  1139. if (p1 == p2) {
  1140. down(&p1->d_inode->i_sem);
  1141. return NULL;
  1142. }
  1143. down(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1144. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1145. if (p->d_parent == p2) {
  1146. down(&p2->d_inode->i_sem);
  1147. down(&p1->d_inode->i_sem);
  1148. return p;
  1149. }
  1150. }
  1151. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1152. if (p->d_parent == p1) {
  1153. down(&p1->d_inode->i_sem);
  1154. down(&p2->d_inode->i_sem);
  1155. return p;
  1156. }
  1157. }
  1158. down(&p1->d_inode->i_sem);
  1159. down(&p2->d_inode->i_sem);
  1160. return NULL;
  1161. }
  1162. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1163. {
  1164. up(&p1->d_inode->i_sem);
  1165. if (p1 != p2) {
  1166. up(&p2->d_inode->i_sem);
  1167. up(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1168. }
  1169. }
  1170. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1171. struct nameidata *nd)
  1172. {
  1173. int error = may_create(dir, dentry, nd);
  1174. if (error)
  1175. return error;
  1176. if (!dir->i_op || !dir->i_op->create)
  1177. return -EACCES; /* shouldn't it be ENOSYS? */
  1178. mode &= S_IALLUGO;
  1179. mode |= S_IFREG;
  1180. error = security_inode_create(dir, dentry, mode);
  1181. if (error)
  1182. return error;
  1183. DQUOT_INIT(dir);
  1184. error = dir->i_op->create(dir, dentry, mode, nd);
  1185. if (!error) {
  1186. inode_dir_notify(dir, DN_CREATE);
  1187. security_inode_post_create(dir, dentry, mode);
  1188. }
  1189. return error;
  1190. }
  1191. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1192. {
  1193. struct dentry *dentry = nd->dentry;
  1194. struct inode *inode = dentry->d_inode;
  1195. int error;
  1196. if (!inode)
  1197. return -ENOENT;
  1198. if (S_ISLNK(inode->i_mode))
  1199. return -ELOOP;
  1200. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1201. return -EISDIR;
  1202. error = permission(inode, acc_mode, nd);
  1203. if (error)
  1204. return error;
  1205. /*
  1206. * FIFO's, sockets and device files are special: they don't
  1207. * actually live on the filesystem itself, and as such you
  1208. * can write to them even if the filesystem is read-only.
  1209. */
  1210. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1211. flag &= ~O_TRUNC;
  1212. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1213. if (nd->mnt->mnt_flags & MNT_NODEV)
  1214. return -EACCES;
  1215. flag &= ~O_TRUNC;
  1216. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1217. return -EROFS;
  1218. /*
  1219. * An append-only file must be opened in append mode for writing.
  1220. */
  1221. if (IS_APPEND(inode)) {
  1222. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1223. return -EPERM;
  1224. if (flag & O_TRUNC)
  1225. return -EPERM;
  1226. }
  1227. /* O_NOATIME can only be set by the owner or superuser */
  1228. if (flag & O_NOATIME)
  1229. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1230. return -EPERM;
  1231. /*
  1232. * Ensure there are no outstanding leases on the file.
  1233. */
  1234. error = break_lease(inode, flag);
  1235. if (error)
  1236. return error;
  1237. if (flag & O_TRUNC) {
  1238. error = get_write_access(inode);
  1239. if (error)
  1240. return error;
  1241. /*
  1242. * Refuse to truncate files with mandatory locks held on them.
  1243. */
  1244. error = locks_verify_locked(inode);
  1245. if (!error) {
  1246. DQUOT_INIT(inode);
  1247. error = do_truncate(dentry, 0);
  1248. }
  1249. put_write_access(inode);
  1250. if (error)
  1251. return error;
  1252. } else
  1253. if (flag & FMODE_WRITE)
  1254. DQUOT_INIT(inode);
  1255. return 0;
  1256. }
  1257. /*
  1258. * open_namei()
  1259. *
  1260. * namei for open - this is in fact almost the whole open-routine.
  1261. *
  1262. * Note that the low bits of "flag" aren't the same as in the open
  1263. * system call - they are 00 - no permissions needed
  1264. * 01 - read permission needed
  1265. * 10 - write permission needed
  1266. * 11 - read/write permissions needed
  1267. * which is a lot more logical, and also allows the "no perm" needed
  1268. * for symlinks (where the permissions are checked later).
  1269. * SMP-safe
  1270. */
  1271. int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
  1272. {
  1273. int acc_mode, error = 0;
  1274. struct path path;
  1275. struct dentry *dir;
  1276. int count = 0;
  1277. acc_mode = ACC_MODE(flag);
  1278. /* Allow the LSM permission hook to distinguish append
  1279. access from general write access. */
  1280. if (flag & O_APPEND)
  1281. acc_mode |= MAY_APPEND;
  1282. /* Fill in the open() intent data */
  1283. nd->intent.open.flags = flag;
  1284. nd->intent.open.create_mode = mode;
  1285. /*
  1286. * The simplest case - just a plain lookup.
  1287. */
  1288. if (!(flag & O_CREAT)) {
  1289. error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
  1290. if (error)
  1291. return error;
  1292. goto ok;
  1293. }
  1294. /*
  1295. * Create - we need to know the parent.
  1296. */
  1297. error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
  1298. if (error)
  1299. return error;
  1300. /*
  1301. * We have the parent and last component. First of all, check
  1302. * that we are not asked to creat(2) an obvious directory - that
  1303. * will not do.
  1304. */
  1305. error = -EISDIR;
  1306. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1307. goto exit;
  1308. dir = nd->dentry;
  1309. nd->flags &= ~LOOKUP_PARENT;
  1310. down(&dir->d_inode->i_sem);
  1311. path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1312. path.mnt = nd->mnt;
  1313. do_last:
  1314. error = PTR_ERR(path.dentry);
  1315. if (IS_ERR(path.dentry)) {
  1316. up(&dir->d_inode->i_sem);
  1317. goto exit;
  1318. }
  1319. /* Negative dentry, just create the file */
  1320. if (!path.dentry->d_inode) {
  1321. if (!IS_POSIXACL(dir->d_inode))
  1322. mode &= ~current->fs->umask;
  1323. error = vfs_create(dir->d_inode, path.dentry, mode, nd);
  1324. up(&dir->d_inode->i_sem);
  1325. dput(nd->dentry);
  1326. nd->dentry = path.dentry;
  1327. if (error)
  1328. goto exit;
  1329. /* Don't check for write permission, don't truncate */
  1330. acc_mode = 0;
  1331. flag &= ~O_TRUNC;
  1332. goto ok;
  1333. }
  1334. /*
  1335. * It already exists.
  1336. */
  1337. up(&dir->d_inode->i_sem);
  1338. error = -EEXIST;
  1339. if (flag & O_EXCL)
  1340. goto exit_dput;
  1341. if (__follow_mount(&path)) {
  1342. error = -ELOOP;
  1343. if (flag & O_NOFOLLOW)
  1344. goto exit_dput;
  1345. }
  1346. error = -ENOENT;
  1347. if (!path.dentry->d_inode)
  1348. goto exit_dput;
  1349. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1350. goto do_link;
  1351. dput(nd->dentry);
  1352. nd->dentry = path.dentry;
  1353. if (nd->mnt != path.mnt)
  1354. mntput(nd->mnt);
  1355. nd->mnt = path.mnt;
  1356. error = -EISDIR;
  1357. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1358. goto exit;
  1359. ok:
  1360. error = may_open(nd, acc_mode, flag);
  1361. if (error)
  1362. goto exit;
  1363. return 0;
  1364. exit_dput:
  1365. dput(path.dentry);
  1366. if (nd->mnt != path.mnt)
  1367. mntput(path.mnt);
  1368. exit:
  1369. path_release(nd);
  1370. return error;
  1371. do_link:
  1372. error = -ELOOP;
  1373. if (flag & O_NOFOLLOW)
  1374. goto exit_dput;
  1375. /*
  1376. * This is subtle. Instead of calling do_follow_link() we do the
  1377. * thing by hands. The reason is that this way we have zero link_count
  1378. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1379. * After that we have the parent and last component, i.e.
  1380. * we are in the same situation as after the first path_walk().
  1381. * Well, almost - if the last component is normal we get its copy
  1382. * stored in nd->last.name and we will have to putname() it when we
  1383. * are done. Procfs-like symlinks just set LAST_BIND.
  1384. */
  1385. nd->flags |= LOOKUP_PARENT;
  1386. error = security_inode_follow_link(path.dentry, nd);
  1387. if (error)
  1388. goto exit_dput;
  1389. error = __do_follow_link(&path, nd);
  1390. if (error)
  1391. return error;
  1392. nd->flags &= ~LOOKUP_PARENT;
  1393. if (nd->last_type == LAST_BIND)
  1394. goto ok;
  1395. error = -EISDIR;
  1396. if (nd->last_type != LAST_NORM)
  1397. goto exit;
  1398. if (nd->last.name[nd->last.len]) {
  1399. putname(nd->last.name);
  1400. goto exit;
  1401. }
  1402. error = -ELOOP;
  1403. if (count++==32) {
  1404. putname(nd->last.name);
  1405. goto exit;
  1406. }
  1407. dir = nd->dentry;
  1408. down(&dir->d_inode->i_sem);
  1409. path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1410. path.mnt = nd->mnt;
  1411. putname(nd->last.name);
  1412. goto do_last;
  1413. }
  1414. /**
  1415. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1416. * @nd: nameidata info
  1417. * @is_dir: directory flag
  1418. *
  1419. * Simple function to lookup and return a dentry and create it
  1420. * if it doesn't exist. Is SMP-safe.
  1421. */
  1422. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1423. {
  1424. struct dentry *dentry;
  1425. down(&nd->dentry->d_inode->i_sem);
  1426. dentry = ERR_PTR(-EEXIST);
  1427. if (nd->last_type != LAST_NORM)
  1428. goto fail;
  1429. nd->flags &= ~LOOKUP_PARENT;
  1430. dentry = lookup_hash(&nd->last, nd->dentry);
  1431. if (IS_ERR(dentry))
  1432. goto fail;
  1433. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1434. goto enoent;
  1435. return dentry;
  1436. enoent:
  1437. dput(dentry);
  1438. dentry = ERR_PTR(-ENOENT);
  1439. fail:
  1440. return dentry;
  1441. }
  1442. EXPORT_SYMBOL_GPL(lookup_create);
  1443. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1444. {
  1445. int error = may_create(dir, dentry, NULL);
  1446. if (error)
  1447. return error;
  1448. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1449. return -EPERM;
  1450. if (!dir->i_op || !dir->i_op->mknod)
  1451. return -EPERM;
  1452. error = security_inode_mknod(dir, dentry, mode, dev);
  1453. if (error)
  1454. return error;
  1455. DQUOT_INIT(dir);
  1456. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1457. if (!error) {
  1458. inode_dir_notify(dir, DN_CREATE);
  1459. security_inode_post_mknod(dir, dentry, mode, dev);
  1460. }
  1461. return error;
  1462. }
  1463. asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
  1464. {
  1465. int error = 0;
  1466. char * tmp;
  1467. struct dentry * dentry;
  1468. struct nameidata nd;
  1469. if (S_ISDIR(mode))
  1470. return -EPERM;
  1471. tmp = getname(filename);
  1472. if (IS_ERR(tmp))
  1473. return PTR_ERR(tmp);
  1474. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1475. if (error)
  1476. goto out;
  1477. dentry = lookup_create(&nd, 0);
  1478. error = PTR_ERR(dentry);
  1479. if (!IS_POSIXACL(nd.dentry->d_inode))
  1480. mode &= ~current->fs->umask;
  1481. if (!IS_ERR(dentry)) {
  1482. switch (mode & S_IFMT) {
  1483. case 0: case S_IFREG:
  1484. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1485. break;
  1486. case S_IFCHR: case S_IFBLK:
  1487. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1488. new_decode_dev(dev));
  1489. break;
  1490. case S_IFIFO: case S_IFSOCK:
  1491. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1492. break;
  1493. case S_IFDIR:
  1494. error = -EPERM;
  1495. break;
  1496. default:
  1497. error = -EINVAL;
  1498. }
  1499. dput(dentry);
  1500. }
  1501. up(&nd.dentry->d_inode->i_sem);
  1502. path_release(&nd);
  1503. out:
  1504. putname(tmp);
  1505. return error;
  1506. }
  1507. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1508. {
  1509. int error = may_create(dir, dentry, NULL);
  1510. if (error)
  1511. return error;
  1512. if (!dir->i_op || !dir->i_op->mkdir)
  1513. return -EPERM;
  1514. mode &= (S_IRWXUGO|S_ISVTX);
  1515. error = security_inode_mkdir(dir, dentry, mode);
  1516. if (error)
  1517. return error;
  1518. DQUOT_INIT(dir);
  1519. error = dir->i_op->mkdir(dir, dentry, mode);
  1520. if (!error) {
  1521. inode_dir_notify(dir, DN_CREATE);
  1522. security_inode_post_mkdir(dir,dentry, mode);
  1523. }
  1524. return error;
  1525. }
  1526. asmlinkage long sys_mkdir(const char __user * pathname, int mode)
  1527. {
  1528. int error = 0;
  1529. char * tmp;
  1530. tmp = getname(pathname);
  1531. error = PTR_ERR(tmp);
  1532. if (!IS_ERR(tmp)) {
  1533. struct dentry *dentry;
  1534. struct nameidata nd;
  1535. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1536. if (error)
  1537. goto out;
  1538. dentry = lookup_create(&nd, 1);
  1539. error = PTR_ERR(dentry);
  1540. if (!IS_ERR(dentry)) {
  1541. if (!IS_POSIXACL(nd.dentry->d_inode))
  1542. mode &= ~current->fs->umask;
  1543. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1544. dput(dentry);
  1545. }
  1546. up(&nd.dentry->d_inode->i_sem);
  1547. path_release(&nd);
  1548. out:
  1549. putname(tmp);
  1550. }
  1551. return error;
  1552. }
  1553. /*
  1554. * We try to drop the dentry early: we should have
  1555. * a usage count of 2 if we're the only user of this
  1556. * dentry, and if that is true (possibly after pruning
  1557. * the dcache), then we drop the dentry now.
  1558. *
  1559. * A low-level filesystem can, if it choses, legally
  1560. * do a
  1561. *
  1562. * if (!d_unhashed(dentry))
  1563. * return -EBUSY;
  1564. *
  1565. * if it cannot handle the case of removing a directory
  1566. * that is still in use by something else..
  1567. */
  1568. void dentry_unhash(struct dentry *dentry)
  1569. {
  1570. dget(dentry);
  1571. if (atomic_read(&dentry->d_count))
  1572. shrink_dcache_parent(dentry);
  1573. spin_lock(&dcache_lock);
  1574. spin_lock(&dentry->d_lock);
  1575. if (atomic_read(&dentry->d_count) == 2)
  1576. __d_drop(dentry);
  1577. spin_unlock(&dentry->d_lock);
  1578. spin_unlock(&dcache_lock);
  1579. }
  1580. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1581. {
  1582. int error = may_delete(dir, dentry, 1);
  1583. if (error)
  1584. return error;
  1585. if (!dir->i_op || !dir->i_op->rmdir)
  1586. return -EPERM;
  1587. DQUOT_INIT(dir);
  1588. down(&dentry->d_inode->i_sem);
  1589. dentry_unhash(dentry);
  1590. if (d_mountpoint(dentry))
  1591. error = -EBUSY;
  1592. else {
  1593. error = security_inode_rmdir(dir, dentry);
  1594. if (!error) {
  1595. error = dir->i_op->rmdir(dir, dentry);
  1596. if (!error)
  1597. dentry->d_inode->i_flags |= S_DEAD;
  1598. }
  1599. }
  1600. up(&dentry->d_inode->i_sem);
  1601. if (!error) {
  1602. inode_dir_notify(dir, DN_DELETE);
  1603. d_delete(dentry);
  1604. }
  1605. dput(dentry);
  1606. return error;
  1607. }
  1608. asmlinkage long sys_rmdir(const char __user * pathname)
  1609. {
  1610. int error = 0;
  1611. char * name;
  1612. struct dentry *dentry;
  1613. struct nameidata nd;
  1614. name = getname(pathname);
  1615. if(IS_ERR(name))
  1616. return PTR_ERR(name);
  1617. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1618. if (error)
  1619. goto exit;
  1620. switch(nd.last_type) {
  1621. case LAST_DOTDOT:
  1622. error = -ENOTEMPTY;
  1623. goto exit1;
  1624. case LAST_DOT:
  1625. error = -EINVAL;
  1626. goto exit1;
  1627. case LAST_ROOT:
  1628. error = -EBUSY;
  1629. goto exit1;
  1630. }
  1631. down(&nd.dentry->d_inode->i_sem);
  1632. dentry = lookup_hash(&nd.last, nd.dentry);
  1633. error = PTR_ERR(dentry);
  1634. if (!IS_ERR(dentry)) {
  1635. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1636. dput(dentry);
  1637. }
  1638. up(&nd.dentry->d_inode->i_sem);
  1639. exit1:
  1640. path_release(&nd);
  1641. exit:
  1642. putname(name);
  1643. return error;
  1644. }
  1645. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1646. {
  1647. int error = may_delete(dir, dentry, 0);
  1648. if (error)
  1649. return error;
  1650. if (!dir->i_op || !dir->i_op->unlink)
  1651. return -EPERM;
  1652. DQUOT_INIT(dir);
  1653. down(&dentry->d_inode->i_sem);
  1654. if (d_mountpoint(dentry))
  1655. error = -EBUSY;
  1656. else {
  1657. error = security_inode_unlink(dir, dentry);
  1658. if (!error)
  1659. error = dir->i_op->unlink(dir, dentry);
  1660. }
  1661. up(&dentry->d_inode->i_sem);
  1662. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1663. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1664. d_delete(dentry);
  1665. inode_dir_notify(dir, DN_DELETE);
  1666. }
  1667. return error;
  1668. }
  1669. /*
  1670. * Make sure that the actual truncation of the file will occur outside its
  1671. * directory's i_sem. Truncate can take a long time if there is a lot of
  1672. * writeout happening, and we don't want to prevent access to the directory
  1673. * while waiting on the I/O.
  1674. */
  1675. asmlinkage long sys_unlink(const char __user * pathname)
  1676. {
  1677. int error = 0;
  1678. char * name;
  1679. struct dentry *dentry;
  1680. struct nameidata nd;
  1681. struct inode *inode = NULL;
  1682. name = getname(pathname);
  1683. if(IS_ERR(name))
  1684. return PTR_ERR(name);
  1685. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1686. if (error)
  1687. goto exit;
  1688. error = -EISDIR;
  1689. if (nd.last_type != LAST_NORM)
  1690. goto exit1;
  1691. down(&nd.dentry->d_inode->i_sem);
  1692. dentry = lookup_hash(&nd.last, nd.dentry);
  1693. error = PTR_ERR(dentry);
  1694. if (!IS_ERR(dentry)) {
  1695. /* Why not before? Because we want correct error value */
  1696. if (nd.last.name[nd.last.len])
  1697. goto slashes;
  1698. inode = dentry->d_inode;
  1699. if (inode)
  1700. atomic_inc(&inode->i_count);
  1701. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1702. exit2:
  1703. dput(dentry);
  1704. }
  1705. up(&nd.dentry->d_inode->i_sem);
  1706. if (inode)
  1707. iput(inode); /* truncate the inode here */
  1708. exit1:
  1709. path_release(&nd);
  1710. exit:
  1711. putname(name);
  1712. return error;
  1713. slashes:
  1714. error = !dentry->d_inode ? -ENOENT :
  1715. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1716. goto exit2;
  1717. }
  1718. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1719. {
  1720. int error = may_create(dir, dentry, NULL);
  1721. if (error)
  1722. return error;
  1723. if (!dir->i_op || !dir->i_op->symlink)
  1724. return -EPERM;
  1725. error = security_inode_symlink(dir, dentry, oldname);
  1726. if (error)
  1727. return error;
  1728. DQUOT_INIT(dir);
  1729. error = dir->i_op->symlink(dir, dentry, oldname);
  1730. if (!error) {
  1731. inode_dir_notify(dir, DN_CREATE);
  1732. security_inode_post_symlink(dir, dentry, oldname);
  1733. }
  1734. return error;
  1735. }
  1736. asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
  1737. {
  1738. int error = 0;
  1739. char * from;
  1740. char * to;
  1741. from = getname(oldname);
  1742. if(IS_ERR(from))
  1743. return PTR_ERR(from);
  1744. to = getname(newname);
  1745. error = PTR_ERR(to);
  1746. if (!IS_ERR(to)) {
  1747. struct dentry *dentry;
  1748. struct nameidata nd;
  1749. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1750. if (error)
  1751. goto out;
  1752. dentry = lookup_create(&nd, 0);
  1753. error = PTR_ERR(dentry);
  1754. if (!IS_ERR(dentry)) {
  1755. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1756. dput(dentry);
  1757. }
  1758. up(&nd.dentry->d_inode->i_sem);
  1759. path_release(&nd);
  1760. out:
  1761. putname(to);
  1762. }
  1763. putname(from);
  1764. return error;
  1765. }
  1766. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1767. {
  1768. struct inode *inode = old_dentry->d_inode;
  1769. int error;
  1770. if (!inode)
  1771. return -ENOENT;
  1772. error = may_create(dir, new_dentry, NULL);
  1773. if (error)
  1774. return error;
  1775. if (dir->i_sb != inode->i_sb)
  1776. return -EXDEV;
  1777. /*
  1778. * A link to an append-only or immutable file cannot be created.
  1779. */
  1780. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1781. return -EPERM;
  1782. if (!dir->i_op || !dir->i_op->link)
  1783. return -EPERM;
  1784. if (S_ISDIR(old_dentry->d_inode->i_mode))
  1785. return -EPERM;
  1786. error = security_inode_link(old_dentry, dir, new_dentry);
  1787. if (error)
  1788. return error;
  1789. down(&old_dentry->d_inode->i_sem);
  1790. DQUOT_INIT(dir);
  1791. error = dir->i_op->link(old_dentry, dir, new_dentry);
  1792. up(&old_dentry->d_inode->i_sem);
  1793. if (!error) {
  1794. inode_dir_notify(dir, DN_CREATE);
  1795. security_inode_post_link(old_dentry, dir, new_dentry);
  1796. }
  1797. return error;
  1798. }
  1799. /*
  1800. * Hardlinks are often used in delicate situations. We avoid
  1801. * security-related surprises by not following symlinks on the
  1802. * newname. --KAB
  1803. *
  1804. * We don't follow them on the oldname either to be compatible
  1805. * with linux 2.0, and to avoid hard-linking to directories
  1806. * and other special files. --ADM
  1807. */
  1808. asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
  1809. {
  1810. struct dentry *new_dentry;
  1811. struct nameidata nd, old_nd;
  1812. int error;
  1813. char * to;
  1814. to = getname(newname);
  1815. if (IS_ERR(to))
  1816. return PTR_ERR(to);
  1817. error = __user_walk(oldname, 0, &old_nd);
  1818. if (error)
  1819. goto exit;
  1820. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1821. if (error)
  1822. goto out;
  1823. error = -EXDEV;
  1824. if (old_nd.mnt != nd.mnt)
  1825. goto out_release;
  1826. new_dentry = lookup_create(&nd, 0);
  1827. error = PTR_ERR(new_dentry);
  1828. if (!IS_ERR(new_dentry)) {
  1829. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  1830. dput(new_dentry);
  1831. }
  1832. up(&nd.dentry->d_inode->i_sem);
  1833. out_release:
  1834. path_release(&nd);
  1835. out:
  1836. path_release(&old_nd);
  1837. exit:
  1838. putname(to);
  1839. return error;
  1840. }
  1841. /*
  1842. * The worst of all namespace operations - renaming directory. "Perverted"
  1843. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  1844. * Problems:
  1845. * a) we can get into loop creation. Check is done in is_subdir().
  1846. * b) race potential - two innocent renames can create a loop together.
  1847. * That's where 4.4 screws up. Current fix: serialization on
  1848. * sb->s_vfs_rename_sem. We might be more accurate, but that's another
  1849. * story.
  1850. * c) we have to lock _three_ objects - parents and victim (if it exists).
  1851. * And that - after we got ->i_sem on parents (until then we don't know
  1852. * whether the target exists). Solution: try to be smart with locking
  1853. * order for inodes. We rely on the fact that tree topology may change
  1854. * only under ->s_vfs_rename_sem _and_ that parent of the object we
  1855. * move will be locked. Thus we can rank directories by the tree
  1856. * (ancestors first) and rank all non-directories after them.
  1857. * That works since everybody except rename does "lock parent, lookup,
  1858. * lock child" and rename is under ->s_vfs_rename_sem.
  1859. * HOWEVER, it relies on the assumption that any object with ->lookup()
  1860. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  1861. * we'd better make sure that there's no link(2) for them.
  1862. * d) some filesystems don't support opened-but-unlinked directories,
  1863. * either because of layout or because they are not ready to deal with
  1864. * all cases correctly. The latter will be fixed (taking this sort of
  1865. * stuff into VFS), but the former is not going away. Solution: the same
  1866. * trick as in rmdir().
  1867. * e) conversion from fhandle to dentry may come in the wrong moment - when
  1868. * we are removing the target. Solution: we will have to grab ->i_sem
  1869. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  1870. * ->i_sem on parents, which works but leads to some truely excessive
  1871. * locking].
  1872. */
  1873. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  1874. struct inode *new_dir, struct dentry *new_dentry)
  1875. {
  1876. int error = 0;
  1877. struct inode *target;
  1878. /*
  1879. * If we are going to change the parent - check write permissions,
  1880. * we'll need to flip '..'.
  1881. */
  1882. if (new_dir != old_dir) {
  1883. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  1884. if (error)
  1885. return error;
  1886. }
  1887. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1888. if (error)
  1889. return error;
  1890. target = new_dentry->d_inode;
  1891. if (target) {
  1892. down(&target->i_sem);
  1893. dentry_unhash(new_dentry);
  1894. }
  1895. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1896. error = -EBUSY;
  1897. else
  1898. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1899. if (target) {
  1900. if (!error)
  1901. target->i_flags |= S_DEAD;
  1902. up(&target->i_sem);
  1903. if (d_unhashed(new_dentry))
  1904. d_rehash(new_dentry);
  1905. dput(new_dentry);
  1906. }
  1907. if (!error) {
  1908. d_move(old_dentry,new_dentry);
  1909. security_inode_post_rename(old_dir, old_dentry,
  1910. new_dir, new_dentry);
  1911. }
  1912. return error;
  1913. }
  1914. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  1915. struct inode *new_dir, struct dentry *new_dentry)
  1916. {
  1917. struct inode *target;
  1918. int error;
  1919. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1920. if (error)
  1921. return error;
  1922. dget(new_dentry);
  1923. target = new_dentry->d_inode;
  1924. if (target)
  1925. down(&target->i_sem);
  1926. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1927. error = -EBUSY;
  1928. else
  1929. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1930. if (!error) {
  1931. /* The following d_move() should become unconditional */
  1932. if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
  1933. d_move(old_dentry, new_dentry);
  1934. security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
  1935. }
  1936. if (target)
  1937. up(&target->i_sem);
  1938. dput(new_dentry);
  1939. return error;
  1940. }
  1941. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1942. struct inode *new_dir, struct dentry *new_dentry)
  1943. {
  1944. int error;
  1945. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  1946. if (old_dentry->d_inode == new_dentry->d_inode)
  1947. return 0;
  1948. error = may_delete(old_dir, old_dentry, is_dir);
  1949. if (error)
  1950. return error;
  1951. if (!new_dentry->d_inode)
  1952. error = may_create(new_dir, new_dentry, NULL);
  1953. else
  1954. error = may_delete(new_dir, new_dentry, is_dir);
  1955. if (error)
  1956. return error;
  1957. if (!old_dir->i_op || !old_dir->i_op->rename)
  1958. return -EPERM;
  1959. DQUOT_INIT(old_dir);
  1960. DQUOT_INIT(new_dir);
  1961. if (is_dir)
  1962. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  1963. else
  1964. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  1965. if (!error) {
  1966. if (old_dir == new_dir)
  1967. inode_dir_notify(old_dir, DN_RENAME);
  1968. else {
  1969. inode_dir_notify(old_dir, DN_DELETE);
  1970. inode_dir_notify(new_dir, DN_CREATE);
  1971. }
  1972. }
  1973. return error;
  1974. }
  1975. static inline int do_rename(const char * oldname, const char * newname)
  1976. {
  1977. int error = 0;
  1978. struct dentry * old_dir, * new_dir;
  1979. struct dentry * old_dentry, *new_dentry;
  1980. struct dentry * trap;
  1981. struct nameidata oldnd, newnd;
  1982. error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
  1983. if (error)
  1984. goto exit;
  1985. error = path_lookup(newname, LOOKUP_PARENT, &newnd);
  1986. if (error)
  1987. goto exit1;
  1988. error = -EXDEV;
  1989. if (oldnd.mnt != newnd.mnt)
  1990. goto exit2;
  1991. old_dir = oldnd.dentry;
  1992. error = -EBUSY;
  1993. if (oldnd.last_type != LAST_NORM)
  1994. goto exit2;
  1995. new_dir = newnd.dentry;
  1996. if (newnd.last_type != LAST_NORM)
  1997. goto exit2;
  1998. trap = lock_rename(new_dir, old_dir);
  1999. old_dentry = lookup_hash(&oldnd.last, old_dir);
  2000. error = PTR_ERR(old_dentry);
  2001. if (IS_ERR(old_dentry))
  2002. goto exit3;
  2003. /* source must exist */
  2004. error = -ENOENT;
  2005. if (!old_dentry->d_inode)
  2006. goto exit4;
  2007. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2008. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2009. error = -ENOTDIR;
  2010. if (oldnd.last.name[oldnd.last.len])
  2011. goto exit4;
  2012. if (newnd.last.name[newnd.last.len])
  2013. goto exit4;
  2014. }
  2015. /* source should not be ancestor of target */
  2016. error = -EINVAL;
  2017. if (old_dentry == trap)
  2018. goto exit4;
  2019. new_dentry = lookup_hash(&newnd.last, new_dir);
  2020. error = PTR_ERR(new_dentry);
  2021. if (IS_ERR(new_dentry))
  2022. goto exit4;
  2023. /* target should not be an ancestor of source */
  2024. error = -ENOTEMPTY;
  2025. if (new_dentry == trap)
  2026. goto exit5;
  2027. error = vfs_rename(old_dir->d_inode, old_dentry,
  2028. new_dir->d_inode, new_dentry);
  2029. exit5:
  2030. dput(new_dentry);
  2031. exit4:
  2032. dput(old_dentry);
  2033. exit3:
  2034. unlock_rename(new_dir, old_dir);
  2035. exit2:
  2036. path_release(&newnd);
  2037. exit1:
  2038. path_release(&oldnd);
  2039. exit:
  2040. return error;
  2041. }
  2042. asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
  2043. {
  2044. int error;
  2045. char * from;
  2046. char * to;
  2047. from = getname(oldname);
  2048. if(IS_ERR(from))
  2049. return PTR_ERR(from);
  2050. to = getname(newname);
  2051. error = PTR_ERR(to);
  2052. if (!IS_ERR(to)) {
  2053. error = do_rename(from,to);
  2054. putname(to);
  2055. }
  2056. putname(from);
  2057. return error;
  2058. }
  2059. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2060. {
  2061. int len;
  2062. len = PTR_ERR(link);
  2063. if (IS_ERR(link))
  2064. goto out;
  2065. len = strlen(link);
  2066. if (len > (unsigned) buflen)
  2067. len = buflen;
  2068. if (copy_to_user(buffer, link, len))
  2069. len = -EFAULT;
  2070. out:
  2071. return len;
  2072. }
  2073. /*
  2074. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2075. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2076. * using) it for any given inode is up to filesystem.
  2077. */
  2078. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2079. {
  2080. struct nameidata nd;
  2081. int res;
  2082. nd.depth = 0;
  2083. res = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2084. if (!res) {
  2085. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2086. if (dentry->d_inode->i_op->put_link)
  2087. dentry->d_inode->i_op->put_link(dentry, &nd);
  2088. }
  2089. return res;
  2090. }
  2091. int vfs_follow_link(struct nameidata *nd, const char *link)
  2092. {
  2093. return __vfs_follow_link(nd, link);
  2094. }
  2095. /* get the link contents into pagecache */
  2096. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2097. {
  2098. struct page * page;
  2099. struct address_space *mapping = dentry->d_inode->i_mapping;
  2100. page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
  2101. NULL);
  2102. if (IS_ERR(page))
  2103. goto sync_fail;
  2104. wait_on_page_locked(page);
  2105. if (!PageUptodate(page))
  2106. goto async_fail;
  2107. *ppage = page;
  2108. return kmap(page);
  2109. async_fail:
  2110. page_cache_release(page);
  2111. return ERR_PTR(-EIO);
  2112. sync_fail:
  2113. return (char*)page;
  2114. }
  2115. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2116. {
  2117. struct page *page = NULL;
  2118. char *s = page_getlink(dentry, &page);
  2119. int res = vfs_readlink(dentry,buffer,buflen,s);
  2120. if (page) {
  2121. kunmap(page);
  2122. page_cache_release(page);
  2123. }
  2124. return res;
  2125. }
  2126. int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2127. {
  2128. struct page *page;
  2129. nd_set_link(nd, page_getlink(dentry, &page));
  2130. return 0;
  2131. }
  2132. void page_put_link(struct dentry *dentry, struct nameidata *nd)
  2133. {
  2134. if (!IS_ERR(nd_get_link(nd))) {
  2135. struct page *page;
  2136. page = find_get_page(dentry->d_inode->i_mapping, 0);
  2137. if (!page)
  2138. BUG();
  2139. kunmap(page);
  2140. page_cache_release(page);
  2141. page_cache_release(page);
  2142. }
  2143. }
  2144. int page_symlink(struct inode *inode, const char *symname, int len)
  2145. {
  2146. struct address_space *mapping = inode->i_mapping;
  2147. struct page *page = grab_cache_page(mapping, 0);
  2148. int err = -ENOMEM;
  2149. char *kaddr;
  2150. if (!page)
  2151. goto fail;
  2152. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2153. if (err)
  2154. goto fail_map;
  2155. kaddr = kmap_atomic(page, KM_USER0);
  2156. memcpy(kaddr, symname, len-1);
  2157. kunmap_atomic(kaddr, KM_USER0);
  2158. mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2159. /*
  2160. * Notice that we are _not_ going to block here - end of page is
  2161. * unmapped, so this will only try to map the rest of page, see
  2162. * that it is unmapped (typically even will not look into inode -
  2163. * ->i_size will be enough for everything) and zero it out.
  2164. * OTOH it's obviously correct and should make the page up-to-date.
  2165. */
  2166. if (!PageUptodate(page)) {
  2167. err = mapping->a_ops->readpage(NULL, page);
  2168. wait_on_page_locked(page);
  2169. } else {
  2170. unlock_page(page);
  2171. }
  2172. page_cache_release(page);
  2173. if (err < 0)
  2174. goto fail;
  2175. mark_inode_dirty(inode);
  2176. return 0;
  2177. fail_map:
  2178. unlock_page(page);
  2179. page_cache_release(page);
  2180. fail:
  2181. return err;
  2182. }
  2183. struct inode_operations page_symlink_inode_operations = {
  2184. .readlink = generic_readlink,
  2185. .follow_link = page_follow_link_light,
  2186. .put_link = page_put_link,
  2187. };
  2188. EXPORT_SYMBOL(__user_walk);
  2189. EXPORT_SYMBOL(follow_down);
  2190. EXPORT_SYMBOL(follow_up);
  2191. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2192. EXPORT_SYMBOL(getname);
  2193. EXPORT_SYMBOL(lock_rename);
  2194. EXPORT_SYMBOL(lookup_hash);
  2195. EXPORT_SYMBOL(lookup_one_len);
  2196. EXPORT_SYMBOL(page_follow_link_light);
  2197. EXPORT_SYMBOL(page_put_link);
  2198. EXPORT_SYMBOL(page_readlink);
  2199. EXPORT_SYMBOL(page_symlink);
  2200. EXPORT_SYMBOL(page_symlink_inode_operations);
  2201. EXPORT_SYMBOL(path_lookup);
  2202. EXPORT_SYMBOL(path_release);
  2203. EXPORT_SYMBOL(path_walk);
  2204. EXPORT_SYMBOL(permission);
  2205. EXPORT_SYMBOL(unlock_rename);
  2206. EXPORT_SYMBOL(vfs_create);
  2207. EXPORT_SYMBOL(vfs_follow_link);
  2208. EXPORT_SYMBOL(vfs_link);
  2209. EXPORT_SYMBOL(vfs_mkdir);
  2210. EXPORT_SYMBOL(vfs_mknod);
  2211. EXPORT_SYMBOL(generic_permission);
  2212. EXPORT_SYMBOL(vfs_readlink);
  2213. EXPORT_SYMBOL(vfs_rename);
  2214. EXPORT_SYMBOL(vfs_rmdir);
  2215. EXPORT_SYMBOL(vfs_symlink);
  2216. EXPORT_SYMBOL(vfs_unlink);
  2217. EXPORT_SYMBOL(dentry_unhash);
  2218. EXPORT_SYMBOL(generic_readlink);