namei.c 69 KB

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