namei.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  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. * O_DIRECTORY translates into forcing a directory lookup.
  1189. */
  1190. static inline int lookup_flags(unsigned int f)
  1191. {
  1192. unsigned long retval = LOOKUP_FOLLOW;
  1193. if (f & O_NOFOLLOW)
  1194. retval &= ~LOOKUP_FOLLOW;
  1195. if (f & O_DIRECTORY)
  1196. retval |= LOOKUP_DIRECTORY;
  1197. return retval;
  1198. }
  1199. /*
  1200. * p1 and p2 should be directories on the same fs.
  1201. */
  1202. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1203. {
  1204. struct dentry *p;
  1205. if (p1 == p2) {
  1206. down(&p1->d_inode->i_sem);
  1207. return NULL;
  1208. }
  1209. down(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1210. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1211. if (p->d_parent == p2) {
  1212. down(&p2->d_inode->i_sem);
  1213. down(&p1->d_inode->i_sem);
  1214. return p;
  1215. }
  1216. }
  1217. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1218. if (p->d_parent == p1) {
  1219. down(&p1->d_inode->i_sem);
  1220. down(&p2->d_inode->i_sem);
  1221. return p;
  1222. }
  1223. }
  1224. down(&p1->d_inode->i_sem);
  1225. down(&p2->d_inode->i_sem);
  1226. return NULL;
  1227. }
  1228. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1229. {
  1230. up(&p1->d_inode->i_sem);
  1231. if (p1 != p2) {
  1232. up(&p2->d_inode->i_sem);
  1233. up(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1234. }
  1235. }
  1236. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1237. struct nameidata *nd)
  1238. {
  1239. int error = may_create(dir, dentry, nd);
  1240. if (error)
  1241. return error;
  1242. if (!dir->i_op || !dir->i_op->create)
  1243. return -EACCES; /* shouldn't it be ENOSYS? */
  1244. mode &= S_IALLUGO;
  1245. mode |= S_IFREG;
  1246. error = security_inode_create(dir, dentry, mode);
  1247. if (error)
  1248. return error;
  1249. DQUOT_INIT(dir);
  1250. error = dir->i_op->create(dir, dentry, mode, nd);
  1251. if (!error)
  1252. fsnotify_create(dir, dentry->d_name.name);
  1253. return error;
  1254. }
  1255. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1256. {
  1257. struct dentry *dentry = nd->dentry;
  1258. struct inode *inode = dentry->d_inode;
  1259. int error;
  1260. if (!inode)
  1261. return -ENOENT;
  1262. if (S_ISLNK(inode->i_mode))
  1263. return -ELOOP;
  1264. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1265. return -EISDIR;
  1266. error = permission(inode, acc_mode, nd);
  1267. if (error)
  1268. return error;
  1269. /*
  1270. * FIFO's, sockets and device files are special: they don't
  1271. * actually live on the filesystem itself, and as such you
  1272. * can write to them even if the filesystem is read-only.
  1273. */
  1274. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1275. flag &= ~O_TRUNC;
  1276. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1277. if (nd->mnt->mnt_flags & MNT_NODEV)
  1278. return -EACCES;
  1279. flag &= ~O_TRUNC;
  1280. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1281. return -EROFS;
  1282. /*
  1283. * An append-only file must be opened in append mode for writing.
  1284. */
  1285. if (IS_APPEND(inode)) {
  1286. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1287. return -EPERM;
  1288. if (flag & O_TRUNC)
  1289. return -EPERM;
  1290. }
  1291. /* O_NOATIME can only be set by the owner or superuser */
  1292. if (flag & O_NOATIME)
  1293. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1294. return -EPERM;
  1295. /*
  1296. * Ensure there are no outstanding leases on the file.
  1297. */
  1298. error = break_lease(inode, flag);
  1299. if (error)
  1300. return error;
  1301. if (flag & O_TRUNC) {
  1302. error = get_write_access(inode);
  1303. if (error)
  1304. return error;
  1305. /*
  1306. * Refuse to truncate files with mandatory locks held on them.
  1307. */
  1308. error = locks_verify_locked(inode);
  1309. if (!error) {
  1310. DQUOT_INIT(inode);
  1311. error = do_truncate(dentry, 0);
  1312. }
  1313. put_write_access(inode);
  1314. if (error)
  1315. return error;
  1316. } else
  1317. if (flag & FMODE_WRITE)
  1318. DQUOT_INIT(inode);
  1319. return 0;
  1320. }
  1321. /*
  1322. * open_namei()
  1323. *
  1324. * namei for open - this is in fact almost the whole open-routine.
  1325. *
  1326. * Note that the low bits of "flag" aren't the same as in the open
  1327. * system call - they are 00 - no permissions needed
  1328. * 01 - read permission needed
  1329. * 10 - write permission needed
  1330. * 11 - read/write permissions needed
  1331. * which is a lot more logical, and also allows the "no perm" needed
  1332. * for symlinks (where the permissions are checked later).
  1333. * SMP-safe
  1334. */
  1335. int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
  1336. {
  1337. int acc_mode, error;
  1338. struct path path;
  1339. struct dentry *dir;
  1340. int count = 0;
  1341. acc_mode = ACC_MODE(flag);
  1342. /* O_TRUNC implies we need access checks for write permissions */
  1343. if (flag & O_TRUNC)
  1344. acc_mode |= MAY_WRITE;
  1345. /* Allow the LSM permission hook to distinguish append
  1346. access from general write access. */
  1347. if (flag & O_APPEND)
  1348. acc_mode |= MAY_APPEND;
  1349. /*
  1350. * The simplest case - just a plain lookup.
  1351. */
  1352. if (!(flag & O_CREAT)) {
  1353. error = path_lookup_open(pathname, lookup_flags(flag), nd, flag);
  1354. if (error)
  1355. return error;
  1356. goto ok;
  1357. }
  1358. /*
  1359. * Create - we need to know the parent.
  1360. */
  1361. error = path_lookup_create(pathname, LOOKUP_PARENT, nd, flag, mode);
  1362. if (error)
  1363. return error;
  1364. /*
  1365. * We have the parent and last component. First of all, check
  1366. * that we are not asked to creat(2) an obvious directory - that
  1367. * will not do.
  1368. */
  1369. error = -EISDIR;
  1370. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1371. goto exit;
  1372. dir = nd->dentry;
  1373. nd->flags &= ~LOOKUP_PARENT;
  1374. down(&dir->d_inode->i_sem);
  1375. path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1376. path.mnt = nd->mnt;
  1377. do_last:
  1378. error = PTR_ERR(path.dentry);
  1379. if (IS_ERR(path.dentry)) {
  1380. up(&dir->d_inode->i_sem);
  1381. goto exit;
  1382. }
  1383. /* Negative dentry, just create the file */
  1384. if (!path.dentry->d_inode) {
  1385. if (!IS_POSIXACL(dir->d_inode))
  1386. mode &= ~current->fs->umask;
  1387. error = vfs_create(dir->d_inode, path.dentry, mode, nd);
  1388. up(&dir->d_inode->i_sem);
  1389. dput(nd->dentry);
  1390. nd->dentry = path.dentry;
  1391. if (error)
  1392. goto exit;
  1393. /* Don't check for write permission, don't truncate */
  1394. acc_mode = 0;
  1395. flag &= ~O_TRUNC;
  1396. goto ok;
  1397. }
  1398. /*
  1399. * It already exists.
  1400. */
  1401. up(&dir->d_inode->i_sem);
  1402. error = -EEXIST;
  1403. if (flag & O_EXCL)
  1404. goto exit_dput;
  1405. if (__follow_mount(&path)) {
  1406. error = -ELOOP;
  1407. if (flag & O_NOFOLLOW)
  1408. goto exit_dput;
  1409. }
  1410. error = -ENOENT;
  1411. if (!path.dentry->d_inode)
  1412. goto exit_dput;
  1413. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1414. goto do_link;
  1415. path_to_nameidata(&path, nd);
  1416. error = -EISDIR;
  1417. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1418. goto exit;
  1419. ok:
  1420. error = may_open(nd, acc_mode, flag);
  1421. if (error)
  1422. goto exit;
  1423. return 0;
  1424. exit_dput:
  1425. dput_path(&path, nd);
  1426. exit:
  1427. if (!IS_ERR(nd->intent.open.file))
  1428. release_open_intent(nd);
  1429. path_release(nd);
  1430. return error;
  1431. do_link:
  1432. error = -ELOOP;
  1433. if (flag & O_NOFOLLOW)
  1434. goto exit_dput;
  1435. /*
  1436. * This is subtle. Instead of calling do_follow_link() we do the
  1437. * thing by hands. The reason is that this way we have zero link_count
  1438. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1439. * After that we have the parent and last component, i.e.
  1440. * we are in the same situation as after the first path_walk().
  1441. * Well, almost - if the last component is normal we get its copy
  1442. * stored in nd->last.name and we will have to putname() it when we
  1443. * are done. Procfs-like symlinks just set LAST_BIND.
  1444. */
  1445. nd->flags |= LOOKUP_PARENT;
  1446. error = security_inode_follow_link(path.dentry, nd);
  1447. if (error)
  1448. goto exit_dput;
  1449. error = __do_follow_link(&path, nd);
  1450. if (error)
  1451. return error;
  1452. nd->flags &= ~LOOKUP_PARENT;
  1453. if (nd->last_type == LAST_BIND)
  1454. goto ok;
  1455. error = -EISDIR;
  1456. if (nd->last_type != LAST_NORM)
  1457. goto exit;
  1458. if (nd->last.name[nd->last.len]) {
  1459. __putname(nd->last.name);
  1460. goto exit;
  1461. }
  1462. error = -ELOOP;
  1463. if (count++==32) {
  1464. __putname(nd->last.name);
  1465. goto exit;
  1466. }
  1467. dir = nd->dentry;
  1468. down(&dir->d_inode->i_sem);
  1469. path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1470. path.mnt = nd->mnt;
  1471. __putname(nd->last.name);
  1472. goto do_last;
  1473. }
  1474. /**
  1475. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1476. * @nd: nameidata info
  1477. * @is_dir: directory flag
  1478. *
  1479. * Simple function to lookup and return a dentry and create it
  1480. * if it doesn't exist. Is SMP-safe.
  1481. *
  1482. * Returns with nd->dentry->d_inode->i_sem locked.
  1483. */
  1484. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1485. {
  1486. struct dentry *dentry = ERR_PTR(-EEXIST);
  1487. down(&nd->dentry->d_inode->i_sem);
  1488. /*
  1489. * Yucky last component or no last component at all?
  1490. * (foo/., foo/.., /////)
  1491. */
  1492. if (nd->last_type != LAST_NORM)
  1493. goto fail;
  1494. nd->flags &= ~LOOKUP_PARENT;
  1495. /*
  1496. * Do the final lookup.
  1497. */
  1498. dentry = lookup_hash(&nd->last, nd->dentry);
  1499. if (IS_ERR(dentry))
  1500. goto fail;
  1501. /*
  1502. * Special case - lookup gave negative, but... we had foo/bar/
  1503. * From the vfs_mknod() POV we just have a negative dentry -
  1504. * all is fine. Let's be bastards - you had / on the end, you've
  1505. * been asking for (non-existent) directory. -ENOENT for you.
  1506. */
  1507. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1508. goto enoent;
  1509. return dentry;
  1510. enoent:
  1511. dput(dentry);
  1512. dentry = ERR_PTR(-ENOENT);
  1513. fail:
  1514. return dentry;
  1515. }
  1516. EXPORT_SYMBOL_GPL(lookup_create);
  1517. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1518. {
  1519. int error = may_create(dir, dentry, NULL);
  1520. if (error)
  1521. return error;
  1522. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1523. return -EPERM;
  1524. if (!dir->i_op || !dir->i_op->mknod)
  1525. return -EPERM;
  1526. error = security_inode_mknod(dir, dentry, mode, dev);
  1527. if (error)
  1528. return error;
  1529. DQUOT_INIT(dir);
  1530. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1531. if (!error)
  1532. fsnotify_create(dir, dentry->d_name.name);
  1533. return error;
  1534. }
  1535. asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
  1536. {
  1537. int error = 0;
  1538. char * tmp;
  1539. struct dentry * dentry;
  1540. struct nameidata nd;
  1541. if (S_ISDIR(mode))
  1542. return -EPERM;
  1543. tmp = getname(filename);
  1544. if (IS_ERR(tmp))
  1545. return PTR_ERR(tmp);
  1546. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1547. if (error)
  1548. goto out;
  1549. dentry = lookup_create(&nd, 0);
  1550. error = PTR_ERR(dentry);
  1551. if (!IS_POSIXACL(nd.dentry->d_inode))
  1552. mode &= ~current->fs->umask;
  1553. if (!IS_ERR(dentry)) {
  1554. switch (mode & S_IFMT) {
  1555. case 0: case S_IFREG:
  1556. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1557. break;
  1558. case S_IFCHR: case S_IFBLK:
  1559. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1560. new_decode_dev(dev));
  1561. break;
  1562. case S_IFIFO: case S_IFSOCK:
  1563. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1564. break;
  1565. case S_IFDIR:
  1566. error = -EPERM;
  1567. break;
  1568. default:
  1569. error = -EINVAL;
  1570. }
  1571. dput(dentry);
  1572. }
  1573. up(&nd.dentry->d_inode->i_sem);
  1574. path_release(&nd);
  1575. out:
  1576. putname(tmp);
  1577. return error;
  1578. }
  1579. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1580. {
  1581. int error = may_create(dir, dentry, NULL);
  1582. if (error)
  1583. return error;
  1584. if (!dir->i_op || !dir->i_op->mkdir)
  1585. return -EPERM;
  1586. mode &= (S_IRWXUGO|S_ISVTX);
  1587. error = security_inode_mkdir(dir, dentry, mode);
  1588. if (error)
  1589. return error;
  1590. DQUOT_INIT(dir);
  1591. error = dir->i_op->mkdir(dir, dentry, mode);
  1592. if (!error)
  1593. fsnotify_mkdir(dir, dentry->d_name.name);
  1594. return error;
  1595. }
  1596. asmlinkage long sys_mkdir(const char __user * pathname, int mode)
  1597. {
  1598. int error = 0;
  1599. char * tmp;
  1600. tmp = getname(pathname);
  1601. error = PTR_ERR(tmp);
  1602. if (!IS_ERR(tmp)) {
  1603. struct dentry *dentry;
  1604. struct nameidata nd;
  1605. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1606. if (error)
  1607. goto out;
  1608. dentry = lookup_create(&nd, 1);
  1609. error = PTR_ERR(dentry);
  1610. if (!IS_ERR(dentry)) {
  1611. if (!IS_POSIXACL(nd.dentry->d_inode))
  1612. mode &= ~current->fs->umask;
  1613. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1614. dput(dentry);
  1615. }
  1616. up(&nd.dentry->d_inode->i_sem);
  1617. path_release(&nd);
  1618. out:
  1619. putname(tmp);
  1620. }
  1621. return error;
  1622. }
  1623. /*
  1624. * We try to drop the dentry early: we should have
  1625. * a usage count of 2 if we're the only user of this
  1626. * dentry, and if that is true (possibly after pruning
  1627. * the dcache), then we drop the dentry now.
  1628. *
  1629. * A low-level filesystem can, if it choses, legally
  1630. * do a
  1631. *
  1632. * if (!d_unhashed(dentry))
  1633. * return -EBUSY;
  1634. *
  1635. * if it cannot handle the case of removing a directory
  1636. * that is still in use by something else..
  1637. */
  1638. void dentry_unhash(struct dentry *dentry)
  1639. {
  1640. dget(dentry);
  1641. if (atomic_read(&dentry->d_count))
  1642. shrink_dcache_parent(dentry);
  1643. spin_lock(&dcache_lock);
  1644. spin_lock(&dentry->d_lock);
  1645. if (atomic_read(&dentry->d_count) == 2)
  1646. __d_drop(dentry);
  1647. spin_unlock(&dentry->d_lock);
  1648. spin_unlock(&dcache_lock);
  1649. }
  1650. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1651. {
  1652. int error = may_delete(dir, dentry, 1);
  1653. if (error)
  1654. return error;
  1655. if (!dir->i_op || !dir->i_op->rmdir)
  1656. return -EPERM;
  1657. DQUOT_INIT(dir);
  1658. down(&dentry->d_inode->i_sem);
  1659. dentry_unhash(dentry);
  1660. if (d_mountpoint(dentry))
  1661. error = -EBUSY;
  1662. else {
  1663. error = security_inode_rmdir(dir, dentry);
  1664. if (!error) {
  1665. error = dir->i_op->rmdir(dir, dentry);
  1666. if (!error)
  1667. dentry->d_inode->i_flags |= S_DEAD;
  1668. }
  1669. }
  1670. up(&dentry->d_inode->i_sem);
  1671. if (!error) {
  1672. d_delete(dentry);
  1673. }
  1674. dput(dentry);
  1675. return error;
  1676. }
  1677. asmlinkage long sys_rmdir(const char __user * pathname)
  1678. {
  1679. int error = 0;
  1680. char * name;
  1681. struct dentry *dentry;
  1682. struct nameidata nd;
  1683. name = getname(pathname);
  1684. if(IS_ERR(name))
  1685. return PTR_ERR(name);
  1686. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1687. if (error)
  1688. goto exit;
  1689. switch(nd.last_type) {
  1690. case LAST_DOTDOT:
  1691. error = -ENOTEMPTY;
  1692. goto exit1;
  1693. case LAST_DOT:
  1694. error = -EINVAL;
  1695. goto exit1;
  1696. case LAST_ROOT:
  1697. error = -EBUSY;
  1698. goto exit1;
  1699. }
  1700. down(&nd.dentry->d_inode->i_sem);
  1701. dentry = lookup_hash(&nd.last, nd.dentry);
  1702. error = PTR_ERR(dentry);
  1703. if (!IS_ERR(dentry)) {
  1704. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1705. dput(dentry);
  1706. }
  1707. up(&nd.dentry->d_inode->i_sem);
  1708. exit1:
  1709. path_release(&nd);
  1710. exit:
  1711. putname(name);
  1712. return error;
  1713. }
  1714. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1715. {
  1716. int error = may_delete(dir, dentry, 0);
  1717. if (error)
  1718. return error;
  1719. if (!dir->i_op || !dir->i_op->unlink)
  1720. return -EPERM;
  1721. DQUOT_INIT(dir);
  1722. down(&dentry->d_inode->i_sem);
  1723. if (d_mountpoint(dentry))
  1724. error = -EBUSY;
  1725. else {
  1726. error = security_inode_unlink(dir, dentry);
  1727. if (!error)
  1728. error = dir->i_op->unlink(dir, dentry);
  1729. }
  1730. up(&dentry->d_inode->i_sem);
  1731. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1732. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1733. d_delete(dentry);
  1734. }
  1735. return error;
  1736. }
  1737. /*
  1738. * Make sure that the actual truncation of the file will occur outside its
  1739. * directory's i_sem. Truncate can take a long time if there is a lot of
  1740. * writeout happening, and we don't want to prevent access to the directory
  1741. * while waiting on the I/O.
  1742. */
  1743. asmlinkage long sys_unlink(const char __user * pathname)
  1744. {
  1745. int error = 0;
  1746. char * name;
  1747. struct dentry *dentry;
  1748. struct nameidata nd;
  1749. struct inode *inode = NULL;
  1750. name = getname(pathname);
  1751. if(IS_ERR(name))
  1752. return PTR_ERR(name);
  1753. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1754. if (error)
  1755. goto exit;
  1756. error = -EISDIR;
  1757. if (nd.last_type != LAST_NORM)
  1758. goto exit1;
  1759. down(&nd.dentry->d_inode->i_sem);
  1760. dentry = lookup_hash(&nd.last, nd.dentry);
  1761. error = PTR_ERR(dentry);
  1762. if (!IS_ERR(dentry)) {
  1763. /* Why not before? Because we want correct error value */
  1764. if (nd.last.name[nd.last.len])
  1765. goto slashes;
  1766. inode = dentry->d_inode;
  1767. if (inode)
  1768. atomic_inc(&inode->i_count);
  1769. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1770. exit2:
  1771. dput(dentry);
  1772. }
  1773. up(&nd.dentry->d_inode->i_sem);
  1774. if (inode)
  1775. iput(inode); /* truncate the inode here */
  1776. exit1:
  1777. path_release(&nd);
  1778. exit:
  1779. putname(name);
  1780. return error;
  1781. slashes:
  1782. error = !dentry->d_inode ? -ENOENT :
  1783. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1784. goto exit2;
  1785. }
  1786. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1787. {
  1788. int error = may_create(dir, dentry, NULL);
  1789. if (error)
  1790. return error;
  1791. if (!dir->i_op || !dir->i_op->symlink)
  1792. return -EPERM;
  1793. error = security_inode_symlink(dir, dentry, oldname);
  1794. if (error)
  1795. return error;
  1796. DQUOT_INIT(dir);
  1797. error = dir->i_op->symlink(dir, dentry, oldname);
  1798. if (!error)
  1799. fsnotify_create(dir, dentry->d_name.name);
  1800. return error;
  1801. }
  1802. asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
  1803. {
  1804. int error = 0;
  1805. char * from;
  1806. char * to;
  1807. from = getname(oldname);
  1808. if(IS_ERR(from))
  1809. return PTR_ERR(from);
  1810. to = getname(newname);
  1811. error = PTR_ERR(to);
  1812. if (!IS_ERR(to)) {
  1813. struct dentry *dentry;
  1814. struct nameidata nd;
  1815. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1816. if (error)
  1817. goto out;
  1818. dentry = lookup_create(&nd, 0);
  1819. error = PTR_ERR(dentry);
  1820. if (!IS_ERR(dentry)) {
  1821. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1822. dput(dentry);
  1823. }
  1824. up(&nd.dentry->d_inode->i_sem);
  1825. path_release(&nd);
  1826. out:
  1827. putname(to);
  1828. }
  1829. putname(from);
  1830. return error;
  1831. }
  1832. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1833. {
  1834. struct inode *inode = old_dentry->d_inode;
  1835. int error;
  1836. if (!inode)
  1837. return -ENOENT;
  1838. error = may_create(dir, new_dentry, NULL);
  1839. if (error)
  1840. return error;
  1841. if (dir->i_sb != inode->i_sb)
  1842. return -EXDEV;
  1843. /*
  1844. * A link to an append-only or immutable file cannot be created.
  1845. */
  1846. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1847. return -EPERM;
  1848. if (!dir->i_op || !dir->i_op->link)
  1849. return -EPERM;
  1850. if (S_ISDIR(old_dentry->d_inode->i_mode))
  1851. return -EPERM;
  1852. error = security_inode_link(old_dentry, dir, new_dentry);
  1853. if (error)
  1854. return error;
  1855. down(&old_dentry->d_inode->i_sem);
  1856. DQUOT_INIT(dir);
  1857. error = dir->i_op->link(old_dentry, dir, new_dentry);
  1858. up(&old_dentry->d_inode->i_sem);
  1859. if (!error)
  1860. fsnotify_create(dir, new_dentry->d_name.name);
  1861. return error;
  1862. }
  1863. /*
  1864. * Hardlinks are often used in delicate situations. We avoid
  1865. * security-related surprises by not following symlinks on the
  1866. * newname. --KAB
  1867. *
  1868. * We don't follow them on the oldname either to be compatible
  1869. * with linux 2.0, and to avoid hard-linking to directories
  1870. * and other special files. --ADM
  1871. */
  1872. asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
  1873. {
  1874. struct dentry *new_dentry;
  1875. struct nameidata nd, old_nd;
  1876. int error;
  1877. char * to;
  1878. to = getname(newname);
  1879. if (IS_ERR(to))
  1880. return PTR_ERR(to);
  1881. error = __user_walk(oldname, 0, &old_nd);
  1882. if (error)
  1883. goto exit;
  1884. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1885. if (error)
  1886. goto out;
  1887. error = -EXDEV;
  1888. if (old_nd.mnt != nd.mnt)
  1889. goto out_release;
  1890. new_dentry = lookup_create(&nd, 0);
  1891. error = PTR_ERR(new_dentry);
  1892. if (!IS_ERR(new_dentry)) {
  1893. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  1894. dput(new_dentry);
  1895. }
  1896. up(&nd.dentry->d_inode->i_sem);
  1897. out_release:
  1898. path_release(&nd);
  1899. out:
  1900. path_release(&old_nd);
  1901. exit:
  1902. putname(to);
  1903. return error;
  1904. }
  1905. /*
  1906. * The worst of all namespace operations - renaming directory. "Perverted"
  1907. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  1908. * Problems:
  1909. * a) we can get into loop creation. Check is done in is_subdir().
  1910. * b) race potential - two innocent renames can create a loop together.
  1911. * That's where 4.4 screws up. Current fix: serialization on
  1912. * sb->s_vfs_rename_sem. We might be more accurate, but that's another
  1913. * story.
  1914. * c) we have to lock _three_ objects - parents and victim (if it exists).
  1915. * And that - after we got ->i_sem on parents (until then we don't know
  1916. * whether the target exists). Solution: try to be smart with locking
  1917. * order for inodes. We rely on the fact that tree topology may change
  1918. * only under ->s_vfs_rename_sem _and_ that parent of the object we
  1919. * move will be locked. Thus we can rank directories by the tree
  1920. * (ancestors first) and rank all non-directories after them.
  1921. * That works since everybody except rename does "lock parent, lookup,
  1922. * lock child" and rename is under ->s_vfs_rename_sem.
  1923. * HOWEVER, it relies on the assumption that any object with ->lookup()
  1924. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  1925. * we'd better make sure that there's no link(2) for them.
  1926. * d) some filesystems don't support opened-but-unlinked directories,
  1927. * either because of layout or because they are not ready to deal with
  1928. * all cases correctly. The latter will be fixed (taking this sort of
  1929. * stuff into VFS), but the former is not going away. Solution: the same
  1930. * trick as in rmdir().
  1931. * e) conversion from fhandle to dentry may come in the wrong moment - when
  1932. * we are removing the target. Solution: we will have to grab ->i_sem
  1933. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  1934. * ->i_sem on parents, which works but leads to some truely excessive
  1935. * locking].
  1936. */
  1937. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  1938. struct inode *new_dir, struct dentry *new_dentry)
  1939. {
  1940. int error = 0;
  1941. struct inode *target;
  1942. /*
  1943. * If we are going to change the parent - check write permissions,
  1944. * we'll need to flip '..'.
  1945. */
  1946. if (new_dir != old_dir) {
  1947. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  1948. if (error)
  1949. return error;
  1950. }
  1951. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1952. if (error)
  1953. return error;
  1954. target = new_dentry->d_inode;
  1955. if (target) {
  1956. down(&target->i_sem);
  1957. dentry_unhash(new_dentry);
  1958. }
  1959. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1960. error = -EBUSY;
  1961. else
  1962. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1963. if (target) {
  1964. if (!error)
  1965. target->i_flags |= S_DEAD;
  1966. up(&target->i_sem);
  1967. if (d_unhashed(new_dentry))
  1968. d_rehash(new_dentry);
  1969. dput(new_dentry);
  1970. }
  1971. if (!error)
  1972. d_move(old_dentry,new_dentry);
  1973. return error;
  1974. }
  1975. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  1976. struct inode *new_dir, struct dentry *new_dentry)
  1977. {
  1978. struct inode *target;
  1979. int error;
  1980. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1981. if (error)
  1982. return error;
  1983. dget(new_dentry);
  1984. target = new_dentry->d_inode;
  1985. if (target)
  1986. down(&target->i_sem);
  1987. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1988. error = -EBUSY;
  1989. else
  1990. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1991. if (!error) {
  1992. /* The following d_move() should become unconditional */
  1993. if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
  1994. d_move(old_dentry, new_dentry);
  1995. }
  1996. if (target)
  1997. up(&target->i_sem);
  1998. dput(new_dentry);
  1999. return error;
  2000. }
  2001. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2002. struct inode *new_dir, struct dentry *new_dentry)
  2003. {
  2004. int error;
  2005. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2006. const char *old_name;
  2007. if (old_dentry->d_inode == new_dentry->d_inode)
  2008. return 0;
  2009. error = may_delete(old_dir, old_dentry, is_dir);
  2010. if (error)
  2011. return error;
  2012. if (!new_dentry->d_inode)
  2013. error = may_create(new_dir, new_dentry, NULL);
  2014. else
  2015. error = may_delete(new_dir, new_dentry, is_dir);
  2016. if (error)
  2017. return error;
  2018. if (!old_dir->i_op || !old_dir->i_op->rename)
  2019. return -EPERM;
  2020. DQUOT_INIT(old_dir);
  2021. DQUOT_INIT(new_dir);
  2022. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2023. if (is_dir)
  2024. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2025. else
  2026. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2027. if (!error) {
  2028. const char *new_name = old_dentry->d_name.name;
  2029. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2030. new_dentry->d_inode, old_dentry->d_inode);
  2031. }
  2032. fsnotify_oldname_free(old_name);
  2033. return error;
  2034. }
  2035. static inline int do_rename(const char * oldname, const char * newname)
  2036. {
  2037. int error = 0;
  2038. struct dentry * old_dir, * new_dir;
  2039. struct dentry * old_dentry, *new_dentry;
  2040. struct dentry * trap;
  2041. struct nameidata oldnd, newnd;
  2042. error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
  2043. if (error)
  2044. goto exit;
  2045. error = path_lookup(newname, LOOKUP_PARENT, &newnd);
  2046. if (error)
  2047. goto exit1;
  2048. error = -EXDEV;
  2049. if (oldnd.mnt != newnd.mnt)
  2050. goto exit2;
  2051. old_dir = oldnd.dentry;
  2052. error = -EBUSY;
  2053. if (oldnd.last_type != LAST_NORM)
  2054. goto exit2;
  2055. new_dir = newnd.dentry;
  2056. if (newnd.last_type != LAST_NORM)
  2057. goto exit2;
  2058. trap = lock_rename(new_dir, old_dir);
  2059. old_dentry = lookup_hash(&oldnd.last, old_dir);
  2060. error = PTR_ERR(old_dentry);
  2061. if (IS_ERR(old_dentry))
  2062. goto exit3;
  2063. /* source must exist */
  2064. error = -ENOENT;
  2065. if (!old_dentry->d_inode)
  2066. goto exit4;
  2067. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2068. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2069. error = -ENOTDIR;
  2070. if (oldnd.last.name[oldnd.last.len])
  2071. goto exit4;
  2072. if (newnd.last.name[newnd.last.len])
  2073. goto exit4;
  2074. }
  2075. /* source should not be ancestor of target */
  2076. error = -EINVAL;
  2077. if (old_dentry == trap)
  2078. goto exit4;
  2079. new_dentry = lookup_hash(&newnd.last, new_dir);
  2080. error = PTR_ERR(new_dentry);
  2081. if (IS_ERR(new_dentry))
  2082. goto exit4;
  2083. /* target should not be an ancestor of source */
  2084. error = -ENOTEMPTY;
  2085. if (new_dentry == trap)
  2086. goto exit5;
  2087. error = vfs_rename(old_dir->d_inode, old_dentry,
  2088. new_dir->d_inode, new_dentry);
  2089. exit5:
  2090. dput(new_dentry);
  2091. exit4:
  2092. dput(old_dentry);
  2093. exit3:
  2094. unlock_rename(new_dir, old_dir);
  2095. exit2:
  2096. path_release(&newnd);
  2097. exit1:
  2098. path_release(&oldnd);
  2099. exit:
  2100. return error;
  2101. }
  2102. asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
  2103. {
  2104. int error;
  2105. char * from;
  2106. char * to;
  2107. from = getname(oldname);
  2108. if(IS_ERR(from))
  2109. return PTR_ERR(from);
  2110. to = getname(newname);
  2111. error = PTR_ERR(to);
  2112. if (!IS_ERR(to)) {
  2113. error = do_rename(from,to);
  2114. putname(to);
  2115. }
  2116. putname(from);
  2117. return error;
  2118. }
  2119. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2120. {
  2121. int len;
  2122. len = PTR_ERR(link);
  2123. if (IS_ERR(link))
  2124. goto out;
  2125. len = strlen(link);
  2126. if (len > (unsigned) buflen)
  2127. len = buflen;
  2128. if (copy_to_user(buffer, link, len))
  2129. len = -EFAULT;
  2130. out:
  2131. return len;
  2132. }
  2133. /*
  2134. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2135. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2136. * using) it for any given inode is up to filesystem.
  2137. */
  2138. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2139. {
  2140. struct nameidata nd;
  2141. void *cookie;
  2142. nd.depth = 0;
  2143. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2144. if (!IS_ERR(cookie)) {
  2145. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2146. if (dentry->d_inode->i_op->put_link)
  2147. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2148. cookie = ERR_PTR(res);
  2149. }
  2150. return PTR_ERR(cookie);
  2151. }
  2152. int vfs_follow_link(struct nameidata *nd, const char *link)
  2153. {
  2154. return __vfs_follow_link(nd, link);
  2155. }
  2156. /* get the link contents into pagecache */
  2157. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2158. {
  2159. struct page * page;
  2160. struct address_space *mapping = dentry->d_inode->i_mapping;
  2161. page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
  2162. NULL);
  2163. if (IS_ERR(page))
  2164. goto sync_fail;
  2165. wait_on_page_locked(page);
  2166. if (!PageUptodate(page))
  2167. goto async_fail;
  2168. *ppage = page;
  2169. return kmap(page);
  2170. async_fail:
  2171. page_cache_release(page);
  2172. return ERR_PTR(-EIO);
  2173. sync_fail:
  2174. return (char*)page;
  2175. }
  2176. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2177. {
  2178. struct page *page = NULL;
  2179. char *s = page_getlink(dentry, &page);
  2180. int res = vfs_readlink(dentry,buffer,buflen,s);
  2181. if (page) {
  2182. kunmap(page);
  2183. page_cache_release(page);
  2184. }
  2185. return res;
  2186. }
  2187. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2188. {
  2189. struct page *page = NULL;
  2190. nd_set_link(nd, page_getlink(dentry, &page));
  2191. return page;
  2192. }
  2193. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2194. {
  2195. struct page *page = cookie;
  2196. if (page) {
  2197. kunmap(page);
  2198. page_cache_release(page);
  2199. }
  2200. }
  2201. int page_symlink(struct inode *inode, const char *symname, int len)
  2202. {
  2203. struct address_space *mapping = inode->i_mapping;
  2204. struct page *page = grab_cache_page(mapping, 0);
  2205. int err = -ENOMEM;
  2206. char *kaddr;
  2207. if (!page)
  2208. goto fail;
  2209. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2210. if (err)
  2211. goto fail_map;
  2212. kaddr = kmap_atomic(page, KM_USER0);
  2213. memcpy(kaddr, symname, len-1);
  2214. kunmap_atomic(kaddr, KM_USER0);
  2215. mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2216. /*
  2217. * Notice that we are _not_ going to block here - end of page is
  2218. * unmapped, so this will only try to map the rest of page, see
  2219. * that it is unmapped (typically even will not look into inode -
  2220. * ->i_size will be enough for everything) and zero it out.
  2221. * OTOH it's obviously correct and should make the page up-to-date.
  2222. */
  2223. if (!PageUptodate(page)) {
  2224. err = mapping->a_ops->readpage(NULL, page);
  2225. wait_on_page_locked(page);
  2226. } else {
  2227. unlock_page(page);
  2228. }
  2229. page_cache_release(page);
  2230. if (err < 0)
  2231. goto fail;
  2232. mark_inode_dirty(inode);
  2233. return 0;
  2234. fail_map:
  2235. unlock_page(page);
  2236. page_cache_release(page);
  2237. fail:
  2238. return err;
  2239. }
  2240. struct inode_operations page_symlink_inode_operations = {
  2241. .readlink = generic_readlink,
  2242. .follow_link = page_follow_link_light,
  2243. .put_link = page_put_link,
  2244. };
  2245. EXPORT_SYMBOL(__user_walk);
  2246. EXPORT_SYMBOL(follow_down);
  2247. EXPORT_SYMBOL(follow_up);
  2248. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2249. EXPORT_SYMBOL(getname);
  2250. EXPORT_SYMBOL(lock_rename);
  2251. EXPORT_SYMBOL(lookup_hash);
  2252. EXPORT_SYMBOL(lookup_one_len);
  2253. EXPORT_SYMBOL(page_follow_link_light);
  2254. EXPORT_SYMBOL(page_put_link);
  2255. EXPORT_SYMBOL(page_readlink);
  2256. EXPORT_SYMBOL(page_symlink);
  2257. EXPORT_SYMBOL(page_symlink_inode_operations);
  2258. EXPORT_SYMBOL(path_lookup);
  2259. EXPORT_SYMBOL(path_release);
  2260. EXPORT_SYMBOL(path_walk);
  2261. EXPORT_SYMBOL(permission);
  2262. EXPORT_SYMBOL(unlock_rename);
  2263. EXPORT_SYMBOL(vfs_create);
  2264. EXPORT_SYMBOL(vfs_follow_link);
  2265. EXPORT_SYMBOL(vfs_link);
  2266. EXPORT_SYMBOL(vfs_mkdir);
  2267. EXPORT_SYMBOL(vfs_mknod);
  2268. EXPORT_SYMBOL(generic_permission);
  2269. EXPORT_SYMBOL(vfs_readlink);
  2270. EXPORT_SYMBOL(vfs_rename);
  2271. EXPORT_SYMBOL(vfs_rmdir);
  2272. EXPORT_SYMBOL(vfs_symlink);
  2273. EXPORT_SYMBOL(vfs_unlink);
  2274. EXPORT_SYMBOL(dentry_unhash);
  2275. EXPORT_SYMBOL(generic_readlink);