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