namei.c 73 KB

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