namei.c 69 KB

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