namei.c 62 KB

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