namei.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422
  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/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.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 <linux/fs_struct.h>
  32. #include <asm/uaccess.h>
  33. #include "internal.h"
  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. /* In order to reduce some races, while at the same time doing additional
  102. * checking and hopefully speeding things up, we copy filenames to the
  103. * kernel data space before using them..
  104. *
  105. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  106. * PATH_MAX includes the nul terminator --RR.
  107. */
  108. static int do_getname(const char __user *filename, char *page)
  109. {
  110. int retval;
  111. unsigned long len = PATH_MAX;
  112. if (!segment_eq(get_fs(), KERNEL_DS)) {
  113. if ((unsigned long) filename >= TASK_SIZE)
  114. return -EFAULT;
  115. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  116. len = TASK_SIZE - (unsigned long) filename;
  117. }
  118. retval = strncpy_from_user(page, filename, len);
  119. if (retval > 0) {
  120. if (retval < len)
  121. return 0;
  122. return -ENAMETOOLONG;
  123. } else if (!retval)
  124. retval = -ENOENT;
  125. return retval;
  126. }
  127. static char *getname_flags(const char __user * filename, int flags)
  128. {
  129. char *tmp, *result;
  130. result = ERR_PTR(-ENOMEM);
  131. tmp = __getname();
  132. if (tmp) {
  133. int retval = do_getname(filename, tmp);
  134. result = tmp;
  135. if (retval < 0) {
  136. if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. }
  142. audit_getname(result);
  143. return result;
  144. }
  145. char *getname(const char __user * filename)
  146. {
  147. return getname_flags(filename, 0);
  148. }
  149. #ifdef CONFIG_AUDITSYSCALL
  150. void putname(const char *name)
  151. {
  152. if (unlikely(!audit_dummy_context()))
  153. audit_putname(name);
  154. else
  155. __putname(name);
  156. }
  157. EXPORT_SYMBOL(putname);
  158. #endif
  159. /*
  160. * This does basic POSIX ACL permission checking
  161. */
  162. static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
  163. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  164. {
  165. umode_t mode = inode->i_mode;
  166. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  167. if (current_fsuid() == inode->i_uid)
  168. mode >>= 6;
  169. else {
  170. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  171. int error = check_acl(inode, mask, flags);
  172. if (error != -EAGAIN)
  173. return error;
  174. }
  175. if (in_group_p(inode->i_gid))
  176. mode >>= 3;
  177. }
  178. /*
  179. * If the DACs are ok we don't need any capability check.
  180. */
  181. if ((mask & ~mode) == 0)
  182. return 0;
  183. return -EACCES;
  184. }
  185. /**
  186. * generic_permission - check for access rights on a Posix-like filesystem
  187. * @inode: inode to check access rights for
  188. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  189. * @check_acl: optional callback to check for Posix ACLs
  190. * @flags: IPERM_FLAG_ flags.
  191. *
  192. * Used to check for read/write/execute permissions on a file.
  193. * We use "fsuid" for this, letting us set arbitrary permissions
  194. * for filesystem access without changing the "normal" uids which
  195. * are used for other things.
  196. *
  197. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  198. * request cannot be satisfied (eg. requires blocking or too much complexity).
  199. * It would then be called again in ref-walk mode.
  200. */
  201. int generic_permission(struct inode *inode, int mask, unsigned int flags,
  202. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  203. {
  204. int ret;
  205. /*
  206. * Do the basic POSIX ACL permission checks.
  207. */
  208. ret = acl_permission_check(inode, mask, flags, check_acl);
  209. if (ret != -EACCES)
  210. return ret;
  211. /*
  212. * Read/write DACs are always overridable.
  213. * Executable DACs are overridable if at least one exec bit is set.
  214. */
  215. if (!(mask & MAY_EXEC) || execute_ok(inode))
  216. if (capable(CAP_DAC_OVERRIDE))
  217. return 0;
  218. /*
  219. * Searching includes executable on directories, else just read.
  220. */
  221. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  222. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  223. if (capable(CAP_DAC_READ_SEARCH))
  224. return 0;
  225. return -EACCES;
  226. }
  227. /**
  228. * inode_permission - check for access rights to a given inode
  229. * @inode: inode to check permission on
  230. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  231. *
  232. * Used to check for read/write/execute permissions on an inode.
  233. * We use "fsuid" for this, letting us set arbitrary permissions
  234. * for filesystem access without changing the "normal" uids which
  235. * are used for other things.
  236. */
  237. int inode_permission(struct inode *inode, int mask)
  238. {
  239. int retval;
  240. if (mask & MAY_WRITE) {
  241. umode_t mode = inode->i_mode;
  242. /*
  243. * Nobody gets write access to a read-only fs.
  244. */
  245. if (IS_RDONLY(inode) &&
  246. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  247. return -EROFS;
  248. /*
  249. * Nobody gets write access to an immutable file.
  250. */
  251. if (IS_IMMUTABLE(inode))
  252. return -EACCES;
  253. }
  254. if (inode->i_op->permission)
  255. retval = inode->i_op->permission(inode, mask, 0);
  256. else
  257. retval = generic_permission(inode, mask, 0,
  258. inode->i_op->check_acl);
  259. if (retval)
  260. return retval;
  261. retval = devcgroup_inode_permission(inode, mask);
  262. if (retval)
  263. return retval;
  264. return security_inode_permission(inode, mask);
  265. }
  266. /**
  267. * file_permission - check for additional access rights to a given file
  268. * @file: file to check access rights for
  269. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  270. *
  271. * Used to check for read/write/execute permissions on an already opened
  272. * file.
  273. *
  274. * Note:
  275. * Do not use this function in new code. All access checks should
  276. * be done using inode_permission().
  277. */
  278. int file_permission(struct file *file, int mask)
  279. {
  280. return inode_permission(file->f_path.dentry->d_inode, mask);
  281. }
  282. /*
  283. * get_write_access() gets write permission for a file.
  284. * put_write_access() releases this write permission.
  285. * This is used for regular files.
  286. * We cannot support write (and maybe mmap read-write shared) accesses and
  287. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  288. * can have the following values:
  289. * 0: no writers, no VM_DENYWRITE mappings
  290. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  291. * > 0: (i_writecount) users are writing to the file.
  292. *
  293. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  294. * except for the cases where we don't hold i_writecount yet. Then we need to
  295. * use {get,deny}_write_access() - these functions check the sign and refuse
  296. * to do the change if sign is wrong. Exclusion between them is provided by
  297. * the inode->i_lock spinlock.
  298. */
  299. int get_write_access(struct inode * inode)
  300. {
  301. spin_lock(&inode->i_lock);
  302. if (atomic_read(&inode->i_writecount) < 0) {
  303. spin_unlock(&inode->i_lock);
  304. return -ETXTBSY;
  305. }
  306. atomic_inc(&inode->i_writecount);
  307. spin_unlock(&inode->i_lock);
  308. return 0;
  309. }
  310. int deny_write_access(struct file * file)
  311. {
  312. struct inode *inode = file->f_path.dentry->d_inode;
  313. spin_lock(&inode->i_lock);
  314. if (atomic_read(&inode->i_writecount) > 0) {
  315. spin_unlock(&inode->i_lock);
  316. return -ETXTBSY;
  317. }
  318. atomic_dec(&inode->i_writecount);
  319. spin_unlock(&inode->i_lock);
  320. return 0;
  321. }
  322. /**
  323. * path_get - get a reference to a path
  324. * @path: path to get the reference to
  325. *
  326. * Given a path increment the reference count to the dentry and the vfsmount.
  327. */
  328. void path_get(struct path *path)
  329. {
  330. mntget(path->mnt);
  331. dget(path->dentry);
  332. }
  333. EXPORT_SYMBOL(path_get);
  334. /**
  335. * path_put - put a reference to a path
  336. * @path: path to put the reference to
  337. *
  338. * Given a path decrement the reference count to the dentry and the vfsmount.
  339. */
  340. void path_put(struct path *path)
  341. {
  342. dput(path->dentry);
  343. mntput(path->mnt);
  344. }
  345. EXPORT_SYMBOL(path_put);
  346. /**
  347. * nameidata_drop_rcu - drop this nameidata out of rcu-walk
  348. * @nd: nameidata pathwalk data to drop
  349. * Returns: 0 on success, -ECHILD on failure
  350. *
  351. * Path walking has 2 modes, rcu-walk and ref-walk (see
  352. * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
  353. * to drop out of rcu-walk mode and take normal reference counts on dentries
  354. * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
  355. * refcounts at the last known good point before rcu-walk got stuck, so
  356. * ref-walk may continue from there. If this is not successful (eg. a seqcount
  357. * has changed), then failure is returned and path walk restarts from the
  358. * beginning in ref-walk mode.
  359. *
  360. * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
  361. * ref-walk. Must be called from rcu-walk context.
  362. */
  363. static int nameidata_drop_rcu(struct nameidata *nd)
  364. {
  365. struct fs_struct *fs = current->fs;
  366. struct dentry *dentry = nd->path.dentry;
  367. int want_root = 0;
  368. BUG_ON(!(nd->flags & LOOKUP_RCU));
  369. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  370. want_root = 1;
  371. spin_lock(&fs->lock);
  372. if (nd->root.mnt != fs->root.mnt ||
  373. nd->root.dentry != fs->root.dentry)
  374. goto err_root;
  375. }
  376. spin_lock(&dentry->d_lock);
  377. if (!__d_rcu_to_refcount(dentry, nd->seq))
  378. goto err;
  379. BUG_ON(nd->inode != dentry->d_inode);
  380. spin_unlock(&dentry->d_lock);
  381. if (want_root) {
  382. path_get(&nd->root);
  383. spin_unlock(&fs->lock);
  384. }
  385. mntget(nd->path.mnt);
  386. rcu_read_unlock();
  387. br_read_unlock(vfsmount_lock);
  388. nd->flags &= ~LOOKUP_RCU;
  389. return 0;
  390. err:
  391. spin_unlock(&dentry->d_lock);
  392. err_root:
  393. if (want_root)
  394. spin_unlock(&fs->lock);
  395. return -ECHILD;
  396. }
  397. /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
  398. static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
  399. {
  400. if (nd->flags & LOOKUP_RCU)
  401. return nameidata_drop_rcu(nd);
  402. return 0;
  403. }
  404. /**
  405. * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
  406. * @nd: nameidata pathwalk data to drop
  407. * @dentry: dentry to drop
  408. * Returns: 0 on success, -ECHILD on failure
  409. *
  410. * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
  411. * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
  412. * @nd. Must be called from rcu-walk context.
  413. */
  414. static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
  415. {
  416. struct fs_struct *fs = current->fs;
  417. struct dentry *parent = nd->path.dentry;
  418. int want_root = 0;
  419. BUG_ON(!(nd->flags & LOOKUP_RCU));
  420. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  421. want_root = 1;
  422. spin_lock(&fs->lock);
  423. if (nd->root.mnt != fs->root.mnt ||
  424. nd->root.dentry != fs->root.dentry)
  425. goto err_root;
  426. }
  427. spin_lock(&parent->d_lock);
  428. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  429. if (!__d_rcu_to_refcount(dentry, nd->seq))
  430. goto err;
  431. /*
  432. * If the sequence check on the child dentry passed, then the child has
  433. * not been removed from its parent. This means the parent dentry must
  434. * be valid and able to take a reference at this point.
  435. */
  436. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  437. BUG_ON(!parent->d_count);
  438. parent->d_count++;
  439. spin_unlock(&dentry->d_lock);
  440. spin_unlock(&parent->d_lock);
  441. if (want_root) {
  442. path_get(&nd->root);
  443. spin_unlock(&fs->lock);
  444. }
  445. mntget(nd->path.mnt);
  446. rcu_read_unlock();
  447. br_read_unlock(vfsmount_lock);
  448. nd->flags &= ~LOOKUP_RCU;
  449. return 0;
  450. err:
  451. spin_unlock(&dentry->d_lock);
  452. spin_unlock(&parent->d_lock);
  453. err_root:
  454. if (want_root)
  455. spin_unlock(&fs->lock);
  456. return -ECHILD;
  457. }
  458. /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
  459. static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
  460. {
  461. if (nd->flags & LOOKUP_RCU) {
  462. if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
  463. nd->flags &= ~LOOKUP_RCU;
  464. if (!(nd->flags & LOOKUP_ROOT))
  465. nd->root.mnt = NULL;
  466. rcu_read_unlock();
  467. br_read_unlock(vfsmount_lock);
  468. return -ECHILD;
  469. }
  470. }
  471. return 0;
  472. }
  473. /**
  474. * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
  475. * @nd: nameidata pathwalk data to drop
  476. * Returns: 0 on success, -ECHILD on failure
  477. *
  478. * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
  479. * nd->path should be the final element of the lookup, so nd->root is discarded.
  480. * Must be called from rcu-walk context.
  481. */
  482. static int nameidata_drop_rcu_last(struct nameidata *nd)
  483. {
  484. struct dentry *dentry = nd->path.dentry;
  485. BUG_ON(!(nd->flags & LOOKUP_RCU));
  486. nd->flags &= ~LOOKUP_RCU;
  487. if (!(nd->flags & LOOKUP_ROOT))
  488. nd->root.mnt = NULL;
  489. spin_lock(&dentry->d_lock);
  490. if (!__d_rcu_to_refcount(dentry, nd->seq))
  491. goto err_unlock;
  492. BUG_ON(nd->inode != dentry->d_inode);
  493. spin_unlock(&dentry->d_lock);
  494. mntget(nd->path.mnt);
  495. rcu_read_unlock();
  496. br_read_unlock(vfsmount_lock);
  497. return 0;
  498. err_unlock:
  499. spin_unlock(&dentry->d_lock);
  500. rcu_read_unlock();
  501. br_read_unlock(vfsmount_lock);
  502. return -ECHILD;
  503. }
  504. /**
  505. * release_open_intent - free up open intent resources
  506. * @nd: pointer to nameidata
  507. */
  508. void release_open_intent(struct nameidata *nd)
  509. {
  510. struct file *file = nd->intent.open.file;
  511. if (file && !IS_ERR(file)) {
  512. if (file->f_path.dentry == NULL)
  513. put_filp(file);
  514. else
  515. fput(file);
  516. }
  517. }
  518. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  519. {
  520. return dentry->d_op->d_revalidate(dentry, nd);
  521. }
  522. static struct dentry *
  523. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  524. {
  525. int status = d_revalidate(dentry, nd);
  526. if (unlikely(status <= 0)) {
  527. /*
  528. * The dentry failed validation.
  529. * If d_revalidate returned 0 attempt to invalidate
  530. * the dentry otherwise d_revalidate is asking us
  531. * to return a fail status.
  532. */
  533. if (status < 0) {
  534. dput(dentry);
  535. dentry = ERR_PTR(status);
  536. } else if (!d_invalidate(dentry)) {
  537. dput(dentry);
  538. dentry = NULL;
  539. }
  540. }
  541. return dentry;
  542. }
  543. /*
  544. * handle_reval_path - force revalidation of a dentry
  545. *
  546. * In some situations the path walking code will trust dentries without
  547. * revalidating them. This causes problems for filesystems that depend on
  548. * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
  549. * (which indicates that it's possible for the dentry to go stale), force
  550. * a d_revalidate call before proceeding.
  551. *
  552. * Returns 0 if the revalidation was successful. If the revalidation fails,
  553. * either return the error returned by d_revalidate or -ESTALE if the
  554. * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
  555. * invalidate the dentry. It's up to the caller to handle putting references
  556. * to the path if necessary.
  557. */
  558. static inline int handle_reval_path(struct nameidata *nd)
  559. {
  560. struct dentry *dentry = nd->path.dentry;
  561. int status;
  562. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  563. return 0;
  564. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  565. return 0;
  566. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  567. return 0;
  568. /* Note: we do not d_invalidate() */
  569. status = d_revalidate(dentry, nd);
  570. if (status > 0)
  571. return 0;
  572. if (!status)
  573. status = -ESTALE;
  574. return status;
  575. }
  576. /*
  577. * Short-cut version of permission(), for calling on directories
  578. * during pathname resolution. Combines parts of permission()
  579. * and generic_permission(), and tests ONLY for MAY_EXEC permission.
  580. *
  581. * If appropriate, check DAC only. If not appropriate, or
  582. * short-cut DAC fails, then call ->permission() to do more
  583. * complete permission check.
  584. */
  585. static inline int exec_permission(struct inode *inode, unsigned int flags)
  586. {
  587. int ret;
  588. if (inode->i_op->permission) {
  589. ret = inode->i_op->permission(inode, MAY_EXEC, flags);
  590. } else {
  591. ret = acl_permission_check(inode, MAY_EXEC, flags,
  592. inode->i_op->check_acl);
  593. }
  594. if (likely(!ret))
  595. goto ok;
  596. if (ret == -ECHILD)
  597. return ret;
  598. if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
  599. goto ok;
  600. return ret;
  601. ok:
  602. return security_inode_exec_permission(inode, flags);
  603. }
  604. static __always_inline void set_root(struct nameidata *nd)
  605. {
  606. if (!nd->root.mnt)
  607. get_fs_root(current->fs, &nd->root);
  608. }
  609. static int link_path_walk(const char *, struct nameidata *);
  610. static __always_inline void set_root_rcu(struct nameidata *nd)
  611. {
  612. if (!nd->root.mnt) {
  613. struct fs_struct *fs = current->fs;
  614. unsigned seq;
  615. do {
  616. seq = read_seqcount_begin(&fs->seq);
  617. nd->root = fs->root;
  618. } while (read_seqcount_retry(&fs->seq, seq));
  619. }
  620. }
  621. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  622. {
  623. int ret;
  624. if (IS_ERR(link))
  625. goto fail;
  626. if (*link == '/') {
  627. set_root(nd);
  628. path_put(&nd->path);
  629. nd->path = nd->root;
  630. path_get(&nd->root);
  631. nd->flags |= LOOKUP_JUMPED;
  632. }
  633. nd->inode = nd->path.dentry->d_inode;
  634. ret = link_path_walk(link, nd);
  635. return ret;
  636. fail:
  637. path_put(&nd->path);
  638. return PTR_ERR(link);
  639. }
  640. static void path_put_conditional(struct path *path, struct nameidata *nd)
  641. {
  642. dput(path->dentry);
  643. if (path->mnt != nd->path.mnt)
  644. mntput(path->mnt);
  645. }
  646. static inline void path_to_nameidata(const struct path *path,
  647. struct nameidata *nd)
  648. {
  649. if (!(nd->flags & LOOKUP_RCU)) {
  650. dput(nd->path.dentry);
  651. if (nd->path.mnt != path->mnt)
  652. mntput(nd->path.mnt);
  653. }
  654. nd->path.mnt = path->mnt;
  655. nd->path.dentry = path->dentry;
  656. }
  657. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  658. {
  659. struct inode *inode = link->dentry->d_inode;
  660. if (!IS_ERR(cookie) && inode->i_op->put_link)
  661. inode->i_op->put_link(link->dentry, nd, cookie);
  662. path_put(link);
  663. }
  664. static __always_inline int
  665. follow_link(struct path *link, struct nameidata *nd, void **p)
  666. {
  667. int error;
  668. struct dentry *dentry = link->dentry;
  669. BUG_ON(nd->flags & LOOKUP_RCU);
  670. if (link->mnt == nd->path.mnt)
  671. mntget(link->mnt);
  672. if (unlikely(current->total_link_count >= 40)) {
  673. *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
  674. path_put(&nd->path);
  675. return -ELOOP;
  676. }
  677. cond_resched();
  678. current->total_link_count++;
  679. touch_atime(link->mnt, dentry);
  680. nd_set_link(nd, NULL);
  681. error = security_inode_follow_link(link->dentry, nd);
  682. if (error) {
  683. *p = ERR_PTR(error); /* no ->put_link(), please */
  684. path_put(&nd->path);
  685. return error;
  686. }
  687. nd->last_type = LAST_BIND;
  688. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  689. error = PTR_ERR(*p);
  690. if (!IS_ERR(*p)) {
  691. char *s = nd_get_link(nd);
  692. error = 0;
  693. if (s)
  694. error = __vfs_follow_link(nd, s);
  695. else if (nd->last_type == LAST_BIND) {
  696. nd->flags |= LOOKUP_JUMPED;
  697. nd->inode = nd->path.dentry->d_inode;
  698. if (nd->inode->i_op->follow_link) {
  699. /* stepped on a _really_ weird one */
  700. path_put(&nd->path);
  701. error = -ELOOP;
  702. }
  703. }
  704. }
  705. return error;
  706. }
  707. static int follow_up_rcu(struct path *path)
  708. {
  709. struct vfsmount *parent;
  710. struct dentry *mountpoint;
  711. parent = path->mnt->mnt_parent;
  712. if (parent == path->mnt)
  713. return 0;
  714. mountpoint = path->mnt->mnt_mountpoint;
  715. path->dentry = mountpoint;
  716. path->mnt = parent;
  717. return 1;
  718. }
  719. int follow_up(struct path *path)
  720. {
  721. struct vfsmount *parent;
  722. struct dentry *mountpoint;
  723. br_read_lock(vfsmount_lock);
  724. parent = path->mnt->mnt_parent;
  725. if (parent == path->mnt) {
  726. br_read_unlock(vfsmount_lock);
  727. return 0;
  728. }
  729. mntget(parent);
  730. mountpoint = dget(path->mnt->mnt_mountpoint);
  731. br_read_unlock(vfsmount_lock);
  732. dput(path->dentry);
  733. path->dentry = mountpoint;
  734. mntput(path->mnt);
  735. path->mnt = parent;
  736. return 1;
  737. }
  738. /*
  739. * Perform an automount
  740. * - return -EISDIR to tell follow_managed() to stop and return the path we
  741. * were called with.
  742. */
  743. static int follow_automount(struct path *path, unsigned flags,
  744. bool *need_mntput)
  745. {
  746. struct vfsmount *mnt;
  747. int err;
  748. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  749. return -EREMOTE;
  750. /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
  751. * and this is the terminal part of the path.
  752. */
  753. if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
  754. return -EISDIR; /* we actually want to stop here */
  755. /* We want to mount if someone is trying to open/create a file of any
  756. * type under the mountpoint, wants to traverse through the mountpoint
  757. * or wants to open the mounted directory.
  758. *
  759. * We don't want to mount if someone's just doing a stat and they've
  760. * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
  761. * appended a '/' to the name.
  762. */
  763. if (!(flags & LOOKUP_FOLLOW) &&
  764. !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
  765. LOOKUP_OPEN | LOOKUP_CREATE)))
  766. return -EISDIR;
  767. current->total_link_count++;
  768. if (current->total_link_count >= 40)
  769. return -ELOOP;
  770. mnt = path->dentry->d_op->d_automount(path);
  771. if (IS_ERR(mnt)) {
  772. /*
  773. * The filesystem is allowed to return -EISDIR here to indicate
  774. * it doesn't want to automount. For instance, autofs would do
  775. * this so that its userspace daemon can mount on this dentry.
  776. *
  777. * However, we can only permit this if it's a terminal point in
  778. * the path being looked up; if it wasn't then the remainder of
  779. * the path is inaccessible and we should say so.
  780. */
  781. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
  782. return -EREMOTE;
  783. return PTR_ERR(mnt);
  784. }
  785. if (!mnt) /* mount collision */
  786. return 0;
  787. err = finish_automount(mnt, path);
  788. switch (err) {
  789. case -EBUSY:
  790. /* Someone else made a mount here whilst we were busy */
  791. return 0;
  792. case 0:
  793. dput(path->dentry);
  794. if (*need_mntput)
  795. mntput(path->mnt);
  796. path->mnt = mnt;
  797. path->dentry = dget(mnt->mnt_root);
  798. *need_mntput = true;
  799. return 0;
  800. default:
  801. return err;
  802. }
  803. }
  804. /*
  805. * Handle a dentry that is managed in some way.
  806. * - Flagged for transit management (autofs)
  807. * - Flagged as mountpoint
  808. * - Flagged as automount point
  809. *
  810. * This may only be called in refwalk mode.
  811. *
  812. * Serialization is taken care of in namespace.c
  813. */
  814. static int follow_managed(struct path *path, unsigned flags)
  815. {
  816. unsigned managed;
  817. bool need_mntput = false;
  818. int ret;
  819. /* Given that we're not holding a lock here, we retain the value in a
  820. * local variable for each dentry as we look at it so that we don't see
  821. * the components of that value change under us */
  822. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  823. managed &= DCACHE_MANAGED_DENTRY,
  824. unlikely(managed != 0)) {
  825. /* Allow the filesystem to manage the transit without i_mutex
  826. * being held. */
  827. if (managed & DCACHE_MANAGE_TRANSIT) {
  828. BUG_ON(!path->dentry->d_op);
  829. BUG_ON(!path->dentry->d_op->d_manage);
  830. ret = path->dentry->d_op->d_manage(path->dentry,
  831. false, false);
  832. if (ret < 0)
  833. return ret == -EISDIR ? 0 : ret;
  834. }
  835. /* Transit to a mounted filesystem. */
  836. if (managed & DCACHE_MOUNTED) {
  837. struct vfsmount *mounted = lookup_mnt(path);
  838. if (mounted) {
  839. dput(path->dentry);
  840. if (need_mntput)
  841. mntput(path->mnt);
  842. path->mnt = mounted;
  843. path->dentry = dget(mounted->mnt_root);
  844. need_mntput = true;
  845. continue;
  846. }
  847. /* Something is mounted on this dentry in another
  848. * namespace and/or whatever was mounted there in this
  849. * namespace got unmounted before we managed to get the
  850. * vfsmount_lock */
  851. }
  852. /* Handle an automount point */
  853. if (managed & DCACHE_NEED_AUTOMOUNT) {
  854. ret = follow_automount(path, flags, &need_mntput);
  855. if (ret < 0)
  856. return ret == -EISDIR ? 0 : ret;
  857. continue;
  858. }
  859. /* We didn't change the current path point */
  860. break;
  861. }
  862. return 0;
  863. }
  864. int follow_down_one(struct path *path)
  865. {
  866. struct vfsmount *mounted;
  867. mounted = lookup_mnt(path);
  868. if (mounted) {
  869. dput(path->dentry);
  870. mntput(path->mnt);
  871. path->mnt = mounted;
  872. path->dentry = dget(mounted->mnt_root);
  873. return 1;
  874. }
  875. return 0;
  876. }
  877. /*
  878. * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
  879. * meet a managed dentry and we're not walking to "..". True is returned to
  880. * continue, false to abort.
  881. */
  882. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  883. struct inode **inode, bool reverse_transit)
  884. {
  885. while (d_mountpoint(path->dentry)) {
  886. struct vfsmount *mounted;
  887. if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  888. !reverse_transit &&
  889. path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
  890. return false;
  891. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  892. if (!mounted)
  893. break;
  894. path->mnt = mounted;
  895. path->dentry = mounted->mnt_root;
  896. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  897. *inode = path->dentry->d_inode;
  898. }
  899. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  900. return reverse_transit;
  901. return true;
  902. }
  903. static int follow_dotdot_rcu(struct nameidata *nd)
  904. {
  905. struct inode *inode = nd->inode;
  906. set_root_rcu(nd);
  907. while (1) {
  908. if (nd->path.dentry == nd->root.dentry &&
  909. nd->path.mnt == nd->root.mnt) {
  910. break;
  911. }
  912. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  913. struct dentry *old = nd->path.dentry;
  914. struct dentry *parent = old->d_parent;
  915. unsigned seq;
  916. seq = read_seqcount_begin(&parent->d_seq);
  917. if (read_seqcount_retry(&old->d_seq, nd->seq))
  918. goto failed;
  919. inode = parent->d_inode;
  920. nd->path.dentry = parent;
  921. nd->seq = seq;
  922. break;
  923. }
  924. if (!follow_up_rcu(&nd->path))
  925. break;
  926. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  927. inode = nd->path.dentry->d_inode;
  928. }
  929. __follow_mount_rcu(nd, &nd->path, &inode, true);
  930. nd->inode = inode;
  931. return 0;
  932. failed:
  933. nd->flags &= ~LOOKUP_RCU;
  934. if (!(nd->flags & LOOKUP_ROOT))
  935. nd->root.mnt = NULL;
  936. rcu_read_unlock();
  937. br_read_unlock(vfsmount_lock);
  938. return -ECHILD;
  939. }
  940. /*
  941. * Follow down to the covering mount currently visible to userspace. At each
  942. * point, the filesystem owning that dentry may be queried as to whether the
  943. * caller is permitted to proceed or not.
  944. *
  945. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  946. * being true).
  947. */
  948. int follow_down(struct path *path, bool mounting_here)
  949. {
  950. unsigned managed;
  951. int ret;
  952. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  953. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  954. /* Allow the filesystem to manage the transit without i_mutex
  955. * being held.
  956. *
  957. * We indicate to the filesystem if someone is trying to mount
  958. * something here. This gives autofs the chance to deny anyone
  959. * other than its daemon the right to mount on its
  960. * superstructure.
  961. *
  962. * The filesystem may sleep at this point.
  963. */
  964. if (managed & DCACHE_MANAGE_TRANSIT) {
  965. BUG_ON(!path->dentry->d_op);
  966. BUG_ON(!path->dentry->d_op->d_manage);
  967. ret = path->dentry->d_op->d_manage(
  968. path->dentry, mounting_here, false);
  969. if (ret < 0)
  970. return ret == -EISDIR ? 0 : ret;
  971. }
  972. /* Transit to a mounted filesystem. */
  973. if (managed & DCACHE_MOUNTED) {
  974. struct vfsmount *mounted = lookup_mnt(path);
  975. if (!mounted)
  976. break;
  977. dput(path->dentry);
  978. mntput(path->mnt);
  979. path->mnt = mounted;
  980. path->dentry = dget(mounted->mnt_root);
  981. continue;
  982. }
  983. /* Don't handle automount points here */
  984. break;
  985. }
  986. return 0;
  987. }
  988. /*
  989. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  990. */
  991. static void follow_mount(struct path *path)
  992. {
  993. while (d_mountpoint(path->dentry)) {
  994. struct vfsmount *mounted = lookup_mnt(path);
  995. if (!mounted)
  996. break;
  997. dput(path->dentry);
  998. mntput(path->mnt);
  999. path->mnt = mounted;
  1000. path->dentry = dget(mounted->mnt_root);
  1001. }
  1002. }
  1003. static void follow_dotdot(struct nameidata *nd)
  1004. {
  1005. set_root(nd);
  1006. while(1) {
  1007. struct dentry *old = nd->path.dentry;
  1008. if (nd->path.dentry == nd->root.dentry &&
  1009. nd->path.mnt == nd->root.mnt) {
  1010. break;
  1011. }
  1012. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1013. /* rare case of legitimate dget_parent()... */
  1014. nd->path.dentry = dget_parent(nd->path.dentry);
  1015. dput(old);
  1016. break;
  1017. }
  1018. if (!follow_up(&nd->path))
  1019. break;
  1020. }
  1021. follow_mount(&nd->path);
  1022. nd->inode = nd->path.dentry->d_inode;
  1023. }
  1024. /*
  1025. * Allocate a dentry with name and parent, and perform a parent
  1026. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  1027. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  1028. * have verified that no child exists while under i_mutex.
  1029. */
  1030. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  1031. struct qstr *name, struct nameidata *nd)
  1032. {
  1033. struct inode *inode = parent->d_inode;
  1034. struct dentry *dentry;
  1035. struct dentry *old;
  1036. /* Don't create child dentry for a dead directory. */
  1037. if (unlikely(IS_DEADDIR(inode)))
  1038. return ERR_PTR(-ENOENT);
  1039. dentry = d_alloc(parent, name);
  1040. if (unlikely(!dentry))
  1041. return ERR_PTR(-ENOMEM);
  1042. old = inode->i_op->lookup(inode, dentry, nd);
  1043. if (unlikely(old)) {
  1044. dput(dentry);
  1045. dentry = old;
  1046. }
  1047. return dentry;
  1048. }
  1049. /*
  1050. * It's more convoluted than I'd like it to be, but... it's still fairly
  1051. * small and for now I'd prefer to have fast path as straight as possible.
  1052. * It _is_ time-critical.
  1053. */
  1054. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1055. struct path *path, struct inode **inode)
  1056. {
  1057. struct vfsmount *mnt = nd->path.mnt;
  1058. struct dentry *dentry, *parent = nd->path.dentry;
  1059. int need_reval = 1;
  1060. int status = 1;
  1061. int err;
  1062. /*
  1063. * Rename seqlock is not required here because in the off chance
  1064. * of a false negative due to a concurrent rename, we're going to
  1065. * do the non-racy lookup, below.
  1066. */
  1067. if (nd->flags & LOOKUP_RCU) {
  1068. unsigned seq;
  1069. *inode = nd->inode;
  1070. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1071. if (!dentry)
  1072. goto unlazy;
  1073. /* Memory barrier in read_seqcount_begin of child is enough */
  1074. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1075. return -ECHILD;
  1076. nd->seq = seq;
  1077. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1078. status = d_revalidate(dentry, nd);
  1079. if (unlikely(status <= 0)) {
  1080. if (status != -ECHILD)
  1081. need_reval = 0;
  1082. goto unlazy;
  1083. }
  1084. }
  1085. path->mnt = mnt;
  1086. path->dentry = dentry;
  1087. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1088. return 0;
  1089. unlazy:
  1090. if (dentry) {
  1091. if (nameidata_dentry_drop_rcu(nd, dentry))
  1092. return -ECHILD;
  1093. } else {
  1094. if (nameidata_drop_rcu(nd))
  1095. return -ECHILD;
  1096. }
  1097. } else {
  1098. dentry = __d_lookup(parent, name);
  1099. }
  1100. retry:
  1101. if (unlikely(!dentry)) {
  1102. struct inode *dir = parent->d_inode;
  1103. BUG_ON(nd->inode != dir);
  1104. mutex_lock(&dir->i_mutex);
  1105. dentry = d_lookup(parent, name);
  1106. if (likely(!dentry)) {
  1107. dentry = d_alloc_and_lookup(parent, name, nd);
  1108. if (IS_ERR(dentry)) {
  1109. mutex_unlock(&dir->i_mutex);
  1110. return PTR_ERR(dentry);
  1111. }
  1112. /* known good */
  1113. need_reval = 0;
  1114. status = 1;
  1115. }
  1116. mutex_unlock(&dir->i_mutex);
  1117. }
  1118. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1119. status = d_revalidate(dentry, nd);
  1120. if (unlikely(status <= 0)) {
  1121. if (status < 0) {
  1122. dput(dentry);
  1123. return status;
  1124. }
  1125. if (!d_invalidate(dentry)) {
  1126. dput(dentry);
  1127. dentry = NULL;
  1128. need_reval = 1;
  1129. goto retry;
  1130. }
  1131. }
  1132. path->mnt = mnt;
  1133. path->dentry = dentry;
  1134. err = follow_managed(path, nd->flags);
  1135. if (unlikely(err < 0)) {
  1136. path_put_conditional(path, nd);
  1137. return err;
  1138. }
  1139. *inode = path->dentry->d_inode;
  1140. return 0;
  1141. }
  1142. static inline int may_lookup(struct nameidata *nd)
  1143. {
  1144. if (nd->flags & LOOKUP_RCU) {
  1145. int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1146. if (err != -ECHILD)
  1147. return err;
  1148. if (nameidata_drop_rcu(nd))
  1149. return -ECHILD;
  1150. }
  1151. return exec_permission(nd->inode, 0);
  1152. }
  1153. static inline int handle_dots(struct nameidata *nd, int type)
  1154. {
  1155. if (type == LAST_DOTDOT) {
  1156. if (nd->flags & LOOKUP_RCU) {
  1157. if (follow_dotdot_rcu(nd))
  1158. return -ECHILD;
  1159. } else
  1160. follow_dotdot(nd);
  1161. }
  1162. return 0;
  1163. }
  1164. static void terminate_walk(struct nameidata *nd)
  1165. {
  1166. if (!(nd->flags & LOOKUP_RCU)) {
  1167. path_put(&nd->path);
  1168. } else {
  1169. nd->flags &= ~LOOKUP_RCU;
  1170. if (!(nd->flags & LOOKUP_ROOT))
  1171. nd->root.mnt = NULL;
  1172. rcu_read_unlock();
  1173. br_read_unlock(vfsmount_lock);
  1174. }
  1175. }
  1176. static inline int walk_component(struct nameidata *nd, struct path *path,
  1177. struct qstr *name, int type, int follow)
  1178. {
  1179. struct inode *inode;
  1180. int err;
  1181. /*
  1182. * "." and ".." are special - ".." especially so because it has
  1183. * to be able to know about the current root directory and
  1184. * parent relationships.
  1185. */
  1186. if (unlikely(type != LAST_NORM))
  1187. return handle_dots(nd, type);
  1188. err = do_lookup(nd, name, path, &inode);
  1189. if (unlikely(err)) {
  1190. terminate_walk(nd);
  1191. return err;
  1192. }
  1193. if (!inode) {
  1194. path_to_nameidata(path, nd);
  1195. terminate_walk(nd);
  1196. return -ENOENT;
  1197. }
  1198. if (unlikely(inode->i_op->follow_link) && follow) {
  1199. if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
  1200. return -ECHILD;
  1201. BUG_ON(inode != path->dentry->d_inode);
  1202. return 1;
  1203. }
  1204. path_to_nameidata(path, nd);
  1205. nd->inode = inode;
  1206. return 0;
  1207. }
  1208. /*
  1209. * This limits recursive symlink follows to 8, while
  1210. * limiting consecutive symlinks to 40.
  1211. *
  1212. * Without that kind of total limit, nasty chains of consecutive
  1213. * symlinks can cause almost arbitrarily long lookups.
  1214. */
  1215. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1216. {
  1217. int res;
  1218. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1219. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1220. path_put_conditional(path, nd);
  1221. path_put(&nd->path);
  1222. return -ELOOP;
  1223. }
  1224. nd->depth++;
  1225. current->link_count++;
  1226. do {
  1227. struct path link = *path;
  1228. void *cookie;
  1229. res = follow_link(&link, nd, &cookie);
  1230. if (!res)
  1231. res = walk_component(nd, path, &nd->last,
  1232. nd->last_type, LOOKUP_FOLLOW);
  1233. put_link(nd, &link, cookie);
  1234. } while (res > 0);
  1235. current->link_count--;
  1236. nd->depth--;
  1237. return res;
  1238. }
  1239. /*
  1240. * Name resolution.
  1241. * This is the basic name resolution function, turning a pathname into
  1242. * the final dentry. We expect 'base' to be positive and a directory.
  1243. *
  1244. * Returns 0 and nd will have valid dentry and mnt on success.
  1245. * Returns error and drops reference to input namei data on failure.
  1246. */
  1247. static int link_path_walk(const char *name, struct nameidata *nd)
  1248. {
  1249. struct path next;
  1250. int err;
  1251. unsigned int lookup_flags = nd->flags;
  1252. while (*name=='/')
  1253. name++;
  1254. if (!*name)
  1255. return 0;
  1256. /* At this point we know we have a real path component. */
  1257. for(;;) {
  1258. unsigned long hash;
  1259. struct qstr this;
  1260. unsigned int c;
  1261. int type;
  1262. nd->flags |= LOOKUP_CONTINUE;
  1263. err = may_lookup(nd);
  1264. if (err)
  1265. break;
  1266. this.name = name;
  1267. c = *(const unsigned char *)name;
  1268. hash = init_name_hash();
  1269. do {
  1270. name++;
  1271. hash = partial_name_hash(c, hash);
  1272. c = *(const unsigned char *)name;
  1273. } while (c && (c != '/'));
  1274. this.len = name - (const char *) this.name;
  1275. this.hash = end_name_hash(hash);
  1276. type = LAST_NORM;
  1277. if (this.name[0] == '.') switch (this.len) {
  1278. case 2:
  1279. if (this.name[1] == '.') {
  1280. type = LAST_DOTDOT;
  1281. nd->flags |= LOOKUP_JUMPED;
  1282. }
  1283. break;
  1284. case 1:
  1285. type = LAST_DOT;
  1286. }
  1287. if (likely(type == LAST_NORM)) {
  1288. struct dentry *parent = nd->path.dentry;
  1289. nd->flags &= ~LOOKUP_JUMPED;
  1290. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1291. err = parent->d_op->d_hash(parent, nd->inode,
  1292. &this);
  1293. if (err < 0)
  1294. break;
  1295. }
  1296. }
  1297. /* remove trailing slashes? */
  1298. if (!c)
  1299. goto last_component;
  1300. while (*++name == '/');
  1301. if (!*name)
  1302. goto last_component;
  1303. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1304. if (err < 0)
  1305. return err;
  1306. if (err) {
  1307. err = nested_symlink(&next, nd);
  1308. if (err)
  1309. return err;
  1310. }
  1311. err = -ENOTDIR;
  1312. if (!nd->inode->i_op->lookup)
  1313. break;
  1314. continue;
  1315. /* here ends the main loop */
  1316. last_component:
  1317. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1318. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1319. nd->last = this;
  1320. nd->last_type = type;
  1321. return 0;
  1322. }
  1323. terminate_walk(nd);
  1324. return err;
  1325. }
  1326. static int path_init(int dfd, const char *name, unsigned int flags,
  1327. struct nameidata *nd, struct file **fp)
  1328. {
  1329. int retval = 0;
  1330. int fput_needed;
  1331. struct file *file;
  1332. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1333. nd->flags = flags | LOOKUP_JUMPED;
  1334. nd->depth = 0;
  1335. if (flags & LOOKUP_ROOT) {
  1336. struct inode *inode = nd->root.dentry->d_inode;
  1337. if (*name) {
  1338. if (!inode->i_op->lookup)
  1339. return -ENOTDIR;
  1340. retval = inode_permission(inode, MAY_EXEC);
  1341. if (retval)
  1342. return retval;
  1343. }
  1344. nd->path = nd->root;
  1345. nd->inode = inode;
  1346. if (flags & LOOKUP_RCU) {
  1347. br_read_lock(vfsmount_lock);
  1348. rcu_read_lock();
  1349. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1350. } else {
  1351. path_get(&nd->path);
  1352. }
  1353. return 0;
  1354. }
  1355. nd->root.mnt = NULL;
  1356. if (*name=='/') {
  1357. if (flags & LOOKUP_RCU) {
  1358. br_read_lock(vfsmount_lock);
  1359. rcu_read_lock();
  1360. set_root_rcu(nd);
  1361. } else {
  1362. set_root(nd);
  1363. path_get(&nd->root);
  1364. }
  1365. nd->path = nd->root;
  1366. } else if (dfd == AT_FDCWD) {
  1367. if (flags & LOOKUP_RCU) {
  1368. struct fs_struct *fs = current->fs;
  1369. unsigned seq;
  1370. br_read_lock(vfsmount_lock);
  1371. rcu_read_lock();
  1372. do {
  1373. seq = read_seqcount_begin(&fs->seq);
  1374. nd->path = fs->pwd;
  1375. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1376. } while (read_seqcount_retry(&fs->seq, seq));
  1377. } else {
  1378. get_fs_pwd(current->fs, &nd->path);
  1379. }
  1380. } else {
  1381. struct dentry *dentry;
  1382. file = fget_raw_light(dfd, &fput_needed);
  1383. retval = -EBADF;
  1384. if (!file)
  1385. goto out_fail;
  1386. dentry = file->f_path.dentry;
  1387. if (*name) {
  1388. retval = -ENOTDIR;
  1389. if (!S_ISDIR(dentry->d_inode->i_mode))
  1390. goto fput_fail;
  1391. retval = file_permission(file, MAY_EXEC);
  1392. if (retval)
  1393. goto fput_fail;
  1394. }
  1395. nd->path = file->f_path;
  1396. if (flags & LOOKUP_RCU) {
  1397. if (fput_needed)
  1398. *fp = file;
  1399. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1400. br_read_lock(vfsmount_lock);
  1401. rcu_read_lock();
  1402. } else {
  1403. path_get(&file->f_path);
  1404. fput_light(file, fput_needed);
  1405. }
  1406. }
  1407. nd->inode = nd->path.dentry->d_inode;
  1408. return 0;
  1409. fput_fail:
  1410. fput_light(file, fput_needed);
  1411. out_fail:
  1412. return retval;
  1413. }
  1414. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1415. {
  1416. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1417. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1418. nd->flags &= ~LOOKUP_PARENT;
  1419. return walk_component(nd, path, &nd->last, nd->last_type,
  1420. nd->flags & LOOKUP_FOLLOW);
  1421. }
  1422. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1423. static int path_lookupat(int dfd, const char *name,
  1424. unsigned int flags, struct nameidata *nd)
  1425. {
  1426. struct file *base = NULL;
  1427. struct path path;
  1428. int err;
  1429. /*
  1430. * Path walking is largely split up into 2 different synchronisation
  1431. * schemes, rcu-walk and ref-walk (explained in
  1432. * Documentation/filesystems/path-lookup.txt). These share much of the
  1433. * path walk code, but some things particularly setup, cleanup, and
  1434. * following mounts are sufficiently divergent that functions are
  1435. * duplicated. Typically there is a function foo(), and its RCU
  1436. * analogue, foo_rcu().
  1437. *
  1438. * -ECHILD is the error number of choice (just to avoid clashes) that
  1439. * is returned if some aspect of an rcu-walk fails. Such an error must
  1440. * be handled by restarting a traditional ref-walk (which will always
  1441. * be able to complete).
  1442. */
  1443. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1444. if (unlikely(err))
  1445. return err;
  1446. current->total_link_count = 0;
  1447. err = link_path_walk(name, nd);
  1448. if (!err && !(flags & LOOKUP_PARENT)) {
  1449. err = lookup_last(nd, &path);
  1450. while (err > 0) {
  1451. void *cookie;
  1452. struct path link = path;
  1453. nd->flags |= LOOKUP_PARENT;
  1454. err = follow_link(&link, nd, &cookie);
  1455. if (!err)
  1456. err = lookup_last(nd, &path);
  1457. put_link(nd, &link, cookie);
  1458. }
  1459. }
  1460. if (nd->flags & LOOKUP_RCU) {
  1461. /* went all way through without dropping RCU */
  1462. BUG_ON(err);
  1463. if (nameidata_drop_rcu_last(nd))
  1464. err = -ECHILD;
  1465. }
  1466. if (!err)
  1467. err = handle_reval_path(nd);
  1468. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1469. if (!nd->inode->i_op->lookup) {
  1470. path_put(&nd->path);
  1471. return -ENOTDIR;
  1472. }
  1473. }
  1474. if (base)
  1475. fput(base);
  1476. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1477. path_put(&nd->root);
  1478. nd->root.mnt = NULL;
  1479. }
  1480. return err;
  1481. }
  1482. static int do_path_lookup(int dfd, const char *name,
  1483. unsigned int flags, struct nameidata *nd)
  1484. {
  1485. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1486. if (unlikely(retval == -ECHILD))
  1487. retval = path_lookupat(dfd, name, flags, nd);
  1488. if (unlikely(retval == -ESTALE))
  1489. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1490. if (likely(!retval)) {
  1491. if (unlikely(!audit_dummy_context())) {
  1492. if (nd->path.dentry && nd->inode)
  1493. audit_inode(name, nd->path.dentry);
  1494. }
  1495. }
  1496. return retval;
  1497. }
  1498. int kern_path_parent(const char *name, struct nameidata *nd)
  1499. {
  1500. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1501. }
  1502. int kern_path(const char *name, unsigned int flags, struct path *path)
  1503. {
  1504. struct nameidata nd;
  1505. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1506. if (!res)
  1507. *path = nd.path;
  1508. return res;
  1509. }
  1510. /**
  1511. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1512. * @dentry: pointer to dentry of the base directory
  1513. * @mnt: pointer to vfs mount of the base directory
  1514. * @name: pointer to file name
  1515. * @flags: lookup flags
  1516. * @nd: pointer to nameidata
  1517. */
  1518. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1519. const char *name, unsigned int flags,
  1520. struct nameidata *nd)
  1521. {
  1522. nd->root.dentry = dentry;
  1523. nd->root.mnt = mnt;
  1524. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1525. return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
  1526. }
  1527. static struct dentry *__lookup_hash(struct qstr *name,
  1528. struct dentry *base, struct nameidata *nd)
  1529. {
  1530. struct inode *inode = base->d_inode;
  1531. struct dentry *dentry;
  1532. int err;
  1533. err = exec_permission(inode, 0);
  1534. if (err)
  1535. return ERR_PTR(err);
  1536. /*
  1537. * Don't bother with __d_lookup: callers are for creat as
  1538. * well as unlink, so a lot of the time it would cost
  1539. * a double lookup.
  1540. */
  1541. dentry = d_lookup(base, name);
  1542. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1543. dentry = do_revalidate(dentry, nd);
  1544. if (!dentry)
  1545. dentry = d_alloc_and_lookup(base, name, nd);
  1546. return dentry;
  1547. }
  1548. /*
  1549. * Restricted form of lookup. Doesn't follow links, single-component only,
  1550. * needs parent already locked. Doesn't follow mounts.
  1551. * SMP-safe.
  1552. */
  1553. static struct dentry *lookup_hash(struct nameidata *nd)
  1554. {
  1555. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1556. }
  1557. /**
  1558. * lookup_one_len - filesystem helper to lookup single pathname component
  1559. * @name: pathname component to lookup
  1560. * @base: base directory to lookup from
  1561. * @len: maximum length @len should be interpreted to
  1562. *
  1563. * Note that this routine is purely a helper for filesystem usage and should
  1564. * not be called by generic code. Also note that by using this function the
  1565. * nameidata argument is passed to the filesystem methods and a filesystem
  1566. * using this helper needs to be prepared for that.
  1567. */
  1568. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1569. {
  1570. struct qstr this;
  1571. unsigned long hash;
  1572. unsigned int c;
  1573. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1574. this.name = name;
  1575. this.len = len;
  1576. if (!len)
  1577. return ERR_PTR(-EACCES);
  1578. hash = init_name_hash();
  1579. while (len--) {
  1580. c = *(const unsigned char *)name++;
  1581. if (c == '/' || c == '\0')
  1582. return ERR_PTR(-EACCES);
  1583. hash = partial_name_hash(c, hash);
  1584. }
  1585. this.hash = end_name_hash(hash);
  1586. /*
  1587. * See if the low-level filesystem might want
  1588. * to use its own hash..
  1589. */
  1590. if (base->d_flags & DCACHE_OP_HASH) {
  1591. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1592. if (err < 0)
  1593. return ERR_PTR(err);
  1594. }
  1595. return __lookup_hash(&this, base, NULL);
  1596. }
  1597. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1598. struct path *path)
  1599. {
  1600. struct nameidata nd;
  1601. char *tmp = getname_flags(name, flags);
  1602. int err = PTR_ERR(tmp);
  1603. if (!IS_ERR(tmp)) {
  1604. BUG_ON(flags & LOOKUP_PARENT);
  1605. err = do_path_lookup(dfd, tmp, flags, &nd);
  1606. putname(tmp);
  1607. if (!err)
  1608. *path = nd.path;
  1609. }
  1610. return err;
  1611. }
  1612. static int user_path_parent(int dfd, const char __user *path,
  1613. struct nameidata *nd, char **name)
  1614. {
  1615. char *s = getname(path);
  1616. int error;
  1617. if (IS_ERR(s))
  1618. return PTR_ERR(s);
  1619. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1620. if (error)
  1621. putname(s);
  1622. else
  1623. *name = s;
  1624. return error;
  1625. }
  1626. /*
  1627. * It's inline, so penalty for filesystems that don't use sticky bit is
  1628. * minimal.
  1629. */
  1630. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1631. {
  1632. uid_t fsuid = current_fsuid();
  1633. if (!(dir->i_mode & S_ISVTX))
  1634. return 0;
  1635. if (inode->i_uid == fsuid)
  1636. return 0;
  1637. if (dir->i_uid == fsuid)
  1638. return 0;
  1639. return !capable(CAP_FOWNER);
  1640. }
  1641. /*
  1642. * Check whether we can remove a link victim from directory dir, check
  1643. * whether the type of victim is right.
  1644. * 1. We can't do it if dir is read-only (done in permission())
  1645. * 2. We should have write and exec permissions on dir
  1646. * 3. We can't remove anything from append-only dir
  1647. * 4. We can't do anything with immutable dir (done in permission())
  1648. * 5. If the sticky bit on dir is set we should either
  1649. * a. be owner of dir, or
  1650. * b. be owner of victim, or
  1651. * c. have CAP_FOWNER capability
  1652. * 6. If the victim is append-only or immutable we can't do antyhing with
  1653. * links pointing to it.
  1654. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1655. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1656. * 9. We can't remove a root or mountpoint.
  1657. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1658. * nfs_async_unlink().
  1659. */
  1660. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1661. {
  1662. int error;
  1663. if (!victim->d_inode)
  1664. return -ENOENT;
  1665. BUG_ON(victim->d_parent->d_inode != dir);
  1666. audit_inode_child(victim, dir);
  1667. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1668. if (error)
  1669. return error;
  1670. if (IS_APPEND(dir))
  1671. return -EPERM;
  1672. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1673. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1674. return -EPERM;
  1675. if (isdir) {
  1676. if (!S_ISDIR(victim->d_inode->i_mode))
  1677. return -ENOTDIR;
  1678. if (IS_ROOT(victim))
  1679. return -EBUSY;
  1680. } else if (S_ISDIR(victim->d_inode->i_mode))
  1681. return -EISDIR;
  1682. if (IS_DEADDIR(dir))
  1683. return -ENOENT;
  1684. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1685. return -EBUSY;
  1686. return 0;
  1687. }
  1688. /* Check whether we can create an object with dentry child in directory
  1689. * dir.
  1690. * 1. We can't do it if child already exists (open has special treatment for
  1691. * this case, but since we are inlined it's OK)
  1692. * 2. We can't do it if dir is read-only (done in permission())
  1693. * 3. We should have write and exec permissions on dir
  1694. * 4. We can't do it if dir is immutable (done in permission())
  1695. */
  1696. static inline int may_create(struct inode *dir, struct dentry *child)
  1697. {
  1698. if (child->d_inode)
  1699. return -EEXIST;
  1700. if (IS_DEADDIR(dir))
  1701. return -ENOENT;
  1702. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1703. }
  1704. /*
  1705. * p1 and p2 should be directories on the same fs.
  1706. */
  1707. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1708. {
  1709. struct dentry *p;
  1710. if (p1 == p2) {
  1711. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1712. return NULL;
  1713. }
  1714. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1715. p = d_ancestor(p2, p1);
  1716. if (p) {
  1717. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1718. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1719. return p;
  1720. }
  1721. p = d_ancestor(p1, p2);
  1722. if (p) {
  1723. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1724. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1725. return p;
  1726. }
  1727. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1728. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1729. return NULL;
  1730. }
  1731. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1732. {
  1733. mutex_unlock(&p1->d_inode->i_mutex);
  1734. if (p1 != p2) {
  1735. mutex_unlock(&p2->d_inode->i_mutex);
  1736. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1737. }
  1738. }
  1739. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1740. struct nameidata *nd)
  1741. {
  1742. int error = may_create(dir, dentry);
  1743. if (error)
  1744. return error;
  1745. if (!dir->i_op->create)
  1746. return -EACCES; /* shouldn't it be ENOSYS? */
  1747. mode &= S_IALLUGO;
  1748. mode |= S_IFREG;
  1749. error = security_inode_create(dir, dentry, mode);
  1750. if (error)
  1751. return error;
  1752. error = dir->i_op->create(dir, dentry, mode, nd);
  1753. if (!error)
  1754. fsnotify_create(dir, dentry);
  1755. return error;
  1756. }
  1757. static int may_open(struct path *path, int acc_mode, int flag)
  1758. {
  1759. struct dentry *dentry = path->dentry;
  1760. struct inode *inode = dentry->d_inode;
  1761. int error;
  1762. /* O_PATH? */
  1763. if (!acc_mode)
  1764. return 0;
  1765. if (!inode)
  1766. return -ENOENT;
  1767. switch (inode->i_mode & S_IFMT) {
  1768. case S_IFLNK:
  1769. return -ELOOP;
  1770. case S_IFDIR:
  1771. if (acc_mode & MAY_WRITE)
  1772. return -EISDIR;
  1773. break;
  1774. case S_IFBLK:
  1775. case S_IFCHR:
  1776. if (path->mnt->mnt_flags & MNT_NODEV)
  1777. return -EACCES;
  1778. /*FALLTHRU*/
  1779. case S_IFIFO:
  1780. case S_IFSOCK:
  1781. flag &= ~O_TRUNC;
  1782. break;
  1783. }
  1784. error = inode_permission(inode, acc_mode);
  1785. if (error)
  1786. return error;
  1787. /*
  1788. * An append-only file must be opened in append mode for writing.
  1789. */
  1790. if (IS_APPEND(inode)) {
  1791. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1792. return -EPERM;
  1793. if (flag & O_TRUNC)
  1794. return -EPERM;
  1795. }
  1796. /* O_NOATIME can only be set by the owner or superuser */
  1797. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1798. return -EPERM;
  1799. /*
  1800. * Ensure there are no outstanding leases on the file.
  1801. */
  1802. return break_lease(inode, flag);
  1803. }
  1804. static int handle_truncate(struct file *filp)
  1805. {
  1806. struct path *path = &filp->f_path;
  1807. struct inode *inode = path->dentry->d_inode;
  1808. int error = get_write_access(inode);
  1809. if (error)
  1810. return error;
  1811. /*
  1812. * Refuse to truncate files with mandatory locks held on them.
  1813. */
  1814. error = locks_verify_locked(inode);
  1815. if (!error)
  1816. error = security_path_truncate(path);
  1817. if (!error) {
  1818. error = do_truncate(path->dentry, 0,
  1819. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1820. filp);
  1821. }
  1822. put_write_access(inode);
  1823. return error;
  1824. }
  1825. /*
  1826. * Note that while the flag value (low two bits) for sys_open means:
  1827. * 00 - read-only
  1828. * 01 - write-only
  1829. * 10 - read-write
  1830. * 11 - special
  1831. * it is changed into
  1832. * 00 - no permissions needed
  1833. * 01 - read-permission
  1834. * 10 - write-permission
  1835. * 11 - read-write
  1836. * for the internal routines (ie open_namei()/follow_link() etc)
  1837. * This is more logical, and also allows the 00 "no perm needed"
  1838. * to be used for symlinks (where the permissions are checked
  1839. * later).
  1840. *
  1841. */
  1842. static inline int open_to_namei_flags(int flag)
  1843. {
  1844. if ((flag+1) & O_ACCMODE)
  1845. flag++;
  1846. return flag;
  1847. }
  1848. /*
  1849. * Handle the last step of open()
  1850. */
  1851. static struct file *do_last(struct nameidata *nd, struct path *path,
  1852. const struct open_flags *op, const char *pathname)
  1853. {
  1854. struct dentry *dir = nd->path.dentry;
  1855. struct dentry *dentry;
  1856. int open_flag = op->open_flag;
  1857. int will_truncate = open_flag & O_TRUNC;
  1858. int want_write = 0;
  1859. int acc_mode = op->acc_mode;
  1860. struct file *filp;
  1861. int error;
  1862. nd->flags &= ~LOOKUP_PARENT;
  1863. nd->flags |= op->intent;
  1864. switch (nd->last_type) {
  1865. case LAST_DOTDOT:
  1866. case LAST_DOT:
  1867. error = handle_dots(nd, nd->last_type);
  1868. if (error)
  1869. return ERR_PTR(error);
  1870. /* fallthrough */
  1871. case LAST_ROOT:
  1872. if (nd->flags & LOOKUP_RCU) {
  1873. if (nameidata_drop_rcu_last(nd))
  1874. return ERR_PTR(-ECHILD);
  1875. }
  1876. error = handle_reval_path(nd);
  1877. if (error)
  1878. goto exit;
  1879. audit_inode(pathname, nd->path.dentry);
  1880. if (open_flag & O_CREAT) {
  1881. error = -EISDIR;
  1882. goto exit;
  1883. }
  1884. goto ok;
  1885. case LAST_BIND:
  1886. /* can't be RCU mode here */
  1887. error = handle_reval_path(nd);
  1888. if (error)
  1889. goto exit;
  1890. audit_inode(pathname, dir);
  1891. goto ok;
  1892. }
  1893. if (!(open_flag & O_CREAT)) {
  1894. int symlink_ok = 0;
  1895. if (nd->last.name[nd->last.len])
  1896. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1897. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1898. symlink_ok = 1;
  1899. /* we _can_ be in RCU mode here */
  1900. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1901. !symlink_ok);
  1902. if (error < 0)
  1903. return ERR_PTR(error);
  1904. if (error) /* symlink */
  1905. return NULL;
  1906. /* sayonara */
  1907. if (nd->flags & LOOKUP_RCU) {
  1908. if (nameidata_drop_rcu_last(nd))
  1909. return ERR_PTR(-ECHILD);
  1910. }
  1911. error = -ENOTDIR;
  1912. if (nd->flags & LOOKUP_DIRECTORY) {
  1913. if (!nd->inode->i_op->lookup)
  1914. goto exit;
  1915. }
  1916. audit_inode(pathname, nd->path.dentry);
  1917. goto ok;
  1918. }
  1919. /* create side of things */
  1920. if (nd->flags & LOOKUP_RCU) {
  1921. if (nameidata_drop_rcu_last(nd))
  1922. return ERR_PTR(-ECHILD);
  1923. }
  1924. audit_inode(pathname, dir);
  1925. error = -EISDIR;
  1926. /* trailing slashes? */
  1927. if (nd->last.name[nd->last.len])
  1928. goto exit;
  1929. mutex_lock(&dir->d_inode->i_mutex);
  1930. dentry = lookup_hash(nd);
  1931. error = PTR_ERR(dentry);
  1932. if (IS_ERR(dentry)) {
  1933. mutex_unlock(&dir->d_inode->i_mutex);
  1934. goto exit;
  1935. }
  1936. path->dentry = dentry;
  1937. path->mnt = nd->path.mnt;
  1938. /* Negative dentry, just create the file */
  1939. if (!dentry->d_inode) {
  1940. int mode = op->mode;
  1941. if (!IS_POSIXACL(dir->d_inode))
  1942. mode &= ~current_umask();
  1943. /*
  1944. * This write is needed to ensure that a
  1945. * rw->ro transition does not occur between
  1946. * the time when the file is created and when
  1947. * a permanent write count is taken through
  1948. * the 'struct file' in nameidata_to_filp().
  1949. */
  1950. error = mnt_want_write(nd->path.mnt);
  1951. if (error)
  1952. goto exit_mutex_unlock;
  1953. want_write = 1;
  1954. /* Don't check for write permission, don't truncate */
  1955. open_flag &= ~O_TRUNC;
  1956. will_truncate = 0;
  1957. acc_mode = MAY_OPEN;
  1958. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1959. if (error)
  1960. goto exit_mutex_unlock;
  1961. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1962. if (error)
  1963. goto exit_mutex_unlock;
  1964. mutex_unlock(&dir->d_inode->i_mutex);
  1965. dput(nd->path.dentry);
  1966. nd->path.dentry = dentry;
  1967. goto common;
  1968. }
  1969. /*
  1970. * It already exists.
  1971. */
  1972. mutex_unlock(&dir->d_inode->i_mutex);
  1973. audit_inode(pathname, path->dentry);
  1974. error = -EEXIST;
  1975. if (open_flag & O_EXCL)
  1976. goto exit_dput;
  1977. error = follow_managed(path, nd->flags);
  1978. if (error < 0)
  1979. goto exit_dput;
  1980. error = -ENOENT;
  1981. if (!path->dentry->d_inode)
  1982. goto exit_dput;
  1983. if (path->dentry->d_inode->i_op->follow_link)
  1984. return NULL;
  1985. path_to_nameidata(path, nd);
  1986. nd->inode = path->dentry->d_inode;
  1987. error = -EISDIR;
  1988. if (S_ISDIR(nd->inode->i_mode))
  1989. goto exit;
  1990. ok:
  1991. if (!S_ISREG(nd->inode->i_mode))
  1992. will_truncate = 0;
  1993. if (will_truncate) {
  1994. error = mnt_want_write(nd->path.mnt);
  1995. if (error)
  1996. goto exit;
  1997. want_write = 1;
  1998. }
  1999. common:
  2000. error = may_open(&nd->path, acc_mode, open_flag);
  2001. if (error)
  2002. goto exit;
  2003. filp = nameidata_to_filp(nd);
  2004. if (!IS_ERR(filp)) {
  2005. error = ima_file_check(filp, op->acc_mode);
  2006. if (error) {
  2007. fput(filp);
  2008. filp = ERR_PTR(error);
  2009. }
  2010. }
  2011. if (!IS_ERR(filp)) {
  2012. if (will_truncate) {
  2013. error = handle_truncate(filp);
  2014. if (error) {
  2015. fput(filp);
  2016. filp = ERR_PTR(error);
  2017. }
  2018. }
  2019. }
  2020. out:
  2021. if (want_write)
  2022. mnt_drop_write(nd->path.mnt);
  2023. path_put(&nd->path);
  2024. return filp;
  2025. exit_mutex_unlock:
  2026. mutex_unlock(&dir->d_inode->i_mutex);
  2027. exit_dput:
  2028. path_put_conditional(path, nd);
  2029. exit:
  2030. filp = ERR_PTR(error);
  2031. goto out;
  2032. }
  2033. static struct file *path_openat(int dfd, const char *pathname,
  2034. struct nameidata *nd, const struct open_flags *op, int flags)
  2035. {
  2036. struct file *base = NULL;
  2037. struct file *filp;
  2038. struct path path;
  2039. int error;
  2040. filp = get_empty_filp();
  2041. if (!filp)
  2042. return ERR_PTR(-ENFILE);
  2043. filp->f_flags = op->open_flag;
  2044. nd->intent.open.file = filp;
  2045. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2046. nd->intent.open.create_mode = op->mode;
  2047. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2048. if (unlikely(error))
  2049. goto out_filp;
  2050. current->total_link_count = 0;
  2051. error = link_path_walk(pathname, nd);
  2052. if (unlikely(error))
  2053. goto out_filp;
  2054. filp = do_last(nd, &path, op, pathname);
  2055. while (unlikely(!filp)) { /* trailing symlink */
  2056. struct path link = path;
  2057. void *cookie;
  2058. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2059. path_put_conditional(&path, nd);
  2060. path_put(&nd->path);
  2061. filp = ERR_PTR(-ELOOP);
  2062. break;
  2063. }
  2064. nd->flags |= LOOKUP_PARENT;
  2065. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2066. error = follow_link(&link, nd, &cookie);
  2067. if (unlikely(error))
  2068. filp = ERR_PTR(error);
  2069. else
  2070. filp = do_last(nd, &path, op, pathname);
  2071. put_link(nd, &link, cookie);
  2072. }
  2073. out:
  2074. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2075. path_put(&nd->root);
  2076. if (base)
  2077. fput(base);
  2078. release_open_intent(nd);
  2079. return filp;
  2080. out_filp:
  2081. filp = ERR_PTR(error);
  2082. goto out;
  2083. }
  2084. struct file *do_filp_open(int dfd, const char *pathname,
  2085. const struct open_flags *op, int flags)
  2086. {
  2087. struct nameidata nd;
  2088. struct file *filp;
  2089. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2090. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2091. filp = path_openat(dfd, pathname, &nd, op, flags);
  2092. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2093. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2094. return filp;
  2095. }
  2096. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2097. const char *name, const struct open_flags *op, int flags)
  2098. {
  2099. struct nameidata nd;
  2100. struct file *file;
  2101. nd.root.mnt = mnt;
  2102. nd.root.dentry = dentry;
  2103. flags |= LOOKUP_ROOT;
  2104. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2105. return ERR_PTR(-ELOOP);
  2106. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2107. if (unlikely(file == ERR_PTR(-ECHILD)))
  2108. file = path_openat(-1, name, &nd, op, flags);
  2109. if (unlikely(file == ERR_PTR(-ESTALE)))
  2110. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2111. return file;
  2112. }
  2113. /**
  2114. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2115. * @nd: nameidata info
  2116. * @is_dir: directory flag
  2117. *
  2118. * Simple function to lookup and return a dentry and create it
  2119. * if it doesn't exist. Is SMP-safe.
  2120. *
  2121. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2122. */
  2123. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2124. {
  2125. struct dentry *dentry = ERR_PTR(-EEXIST);
  2126. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2127. /*
  2128. * Yucky last component or no last component at all?
  2129. * (foo/., foo/.., /////)
  2130. */
  2131. if (nd->last_type != LAST_NORM)
  2132. goto fail;
  2133. nd->flags &= ~LOOKUP_PARENT;
  2134. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2135. nd->intent.open.flags = O_EXCL;
  2136. /*
  2137. * Do the final lookup.
  2138. */
  2139. dentry = lookup_hash(nd);
  2140. if (IS_ERR(dentry))
  2141. goto fail;
  2142. if (dentry->d_inode)
  2143. goto eexist;
  2144. /*
  2145. * Special case - lookup gave negative, but... we had foo/bar/
  2146. * From the vfs_mknod() POV we just have a negative dentry -
  2147. * all is fine. Let's be bastards - you had / on the end, you've
  2148. * been asking for (non-existent) directory. -ENOENT for you.
  2149. */
  2150. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2151. dput(dentry);
  2152. dentry = ERR_PTR(-ENOENT);
  2153. }
  2154. return dentry;
  2155. eexist:
  2156. dput(dentry);
  2157. dentry = ERR_PTR(-EEXIST);
  2158. fail:
  2159. return dentry;
  2160. }
  2161. EXPORT_SYMBOL_GPL(lookup_create);
  2162. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2163. {
  2164. int error = may_create(dir, dentry);
  2165. if (error)
  2166. return error;
  2167. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2168. return -EPERM;
  2169. if (!dir->i_op->mknod)
  2170. return -EPERM;
  2171. error = devcgroup_inode_mknod(mode, dev);
  2172. if (error)
  2173. return error;
  2174. error = security_inode_mknod(dir, dentry, mode, dev);
  2175. if (error)
  2176. return error;
  2177. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2178. if (!error)
  2179. fsnotify_create(dir, dentry);
  2180. return error;
  2181. }
  2182. static int may_mknod(mode_t mode)
  2183. {
  2184. switch (mode & S_IFMT) {
  2185. case S_IFREG:
  2186. case S_IFCHR:
  2187. case S_IFBLK:
  2188. case S_IFIFO:
  2189. case S_IFSOCK:
  2190. case 0: /* zero mode translates to S_IFREG */
  2191. return 0;
  2192. case S_IFDIR:
  2193. return -EPERM;
  2194. default:
  2195. return -EINVAL;
  2196. }
  2197. }
  2198. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2199. unsigned, dev)
  2200. {
  2201. int error;
  2202. char *tmp;
  2203. struct dentry *dentry;
  2204. struct nameidata nd;
  2205. if (S_ISDIR(mode))
  2206. return -EPERM;
  2207. error = user_path_parent(dfd, filename, &nd, &tmp);
  2208. if (error)
  2209. return error;
  2210. dentry = lookup_create(&nd, 0);
  2211. if (IS_ERR(dentry)) {
  2212. error = PTR_ERR(dentry);
  2213. goto out_unlock;
  2214. }
  2215. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2216. mode &= ~current_umask();
  2217. error = may_mknod(mode);
  2218. if (error)
  2219. goto out_dput;
  2220. error = mnt_want_write(nd.path.mnt);
  2221. if (error)
  2222. goto out_dput;
  2223. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2224. if (error)
  2225. goto out_drop_write;
  2226. switch (mode & S_IFMT) {
  2227. case 0: case S_IFREG:
  2228. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2229. break;
  2230. case S_IFCHR: case S_IFBLK:
  2231. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2232. new_decode_dev(dev));
  2233. break;
  2234. case S_IFIFO: case S_IFSOCK:
  2235. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2236. break;
  2237. }
  2238. out_drop_write:
  2239. mnt_drop_write(nd.path.mnt);
  2240. out_dput:
  2241. dput(dentry);
  2242. out_unlock:
  2243. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2244. path_put(&nd.path);
  2245. putname(tmp);
  2246. return error;
  2247. }
  2248. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2249. {
  2250. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2251. }
  2252. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2253. {
  2254. int error = may_create(dir, dentry);
  2255. if (error)
  2256. return error;
  2257. if (!dir->i_op->mkdir)
  2258. return -EPERM;
  2259. mode &= (S_IRWXUGO|S_ISVTX);
  2260. error = security_inode_mkdir(dir, dentry, mode);
  2261. if (error)
  2262. return error;
  2263. error = dir->i_op->mkdir(dir, dentry, mode);
  2264. if (!error)
  2265. fsnotify_mkdir(dir, dentry);
  2266. return error;
  2267. }
  2268. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2269. {
  2270. int error = 0;
  2271. char * tmp;
  2272. struct dentry *dentry;
  2273. struct nameidata nd;
  2274. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2275. if (error)
  2276. goto out_err;
  2277. dentry = lookup_create(&nd, 1);
  2278. error = PTR_ERR(dentry);
  2279. if (IS_ERR(dentry))
  2280. goto out_unlock;
  2281. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2282. mode &= ~current_umask();
  2283. error = mnt_want_write(nd.path.mnt);
  2284. if (error)
  2285. goto out_dput;
  2286. error = security_path_mkdir(&nd.path, dentry, mode);
  2287. if (error)
  2288. goto out_drop_write;
  2289. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2290. out_drop_write:
  2291. mnt_drop_write(nd.path.mnt);
  2292. out_dput:
  2293. dput(dentry);
  2294. out_unlock:
  2295. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2296. path_put(&nd.path);
  2297. putname(tmp);
  2298. out_err:
  2299. return error;
  2300. }
  2301. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2302. {
  2303. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2304. }
  2305. /*
  2306. * We try to drop the dentry early: we should have
  2307. * a usage count of 2 if we're the only user of this
  2308. * dentry, and if that is true (possibly after pruning
  2309. * the dcache), then we drop the dentry now.
  2310. *
  2311. * A low-level filesystem can, if it choses, legally
  2312. * do a
  2313. *
  2314. * if (!d_unhashed(dentry))
  2315. * return -EBUSY;
  2316. *
  2317. * if it cannot handle the case of removing a directory
  2318. * that is still in use by something else..
  2319. */
  2320. void dentry_unhash(struct dentry *dentry)
  2321. {
  2322. dget(dentry);
  2323. shrink_dcache_parent(dentry);
  2324. spin_lock(&dentry->d_lock);
  2325. if (dentry->d_count == 2)
  2326. __d_drop(dentry);
  2327. spin_unlock(&dentry->d_lock);
  2328. }
  2329. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2330. {
  2331. int error = may_delete(dir, dentry, 1);
  2332. if (error)
  2333. return error;
  2334. if (!dir->i_op->rmdir)
  2335. return -EPERM;
  2336. mutex_lock(&dentry->d_inode->i_mutex);
  2337. dentry_unhash(dentry);
  2338. if (d_mountpoint(dentry))
  2339. error = -EBUSY;
  2340. else {
  2341. error = security_inode_rmdir(dir, dentry);
  2342. if (!error) {
  2343. error = dir->i_op->rmdir(dir, dentry);
  2344. if (!error) {
  2345. dentry->d_inode->i_flags |= S_DEAD;
  2346. dont_mount(dentry);
  2347. }
  2348. }
  2349. }
  2350. mutex_unlock(&dentry->d_inode->i_mutex);
  2351. if (!error) {
  2352. d_delete(dentry);
  2353. }
  2354. dput(dentry);
  2355. return error;
  2356. }
  2357. static long do_rmdir(int dfd, const char __user *pathname)
  2358. {
  2359. int error = 0;
  2360. char * name;
  2361. struct dentry *dentry;
  2362. struct nameidata nd;
  2363. error = user_path_parent(dfd, pathname, &nd, &name);
  2364. if (error)
  2365. return error;
  2366. switch(nd.last_type) {
  2367. case LAST_DOTDOT:
  2368. error = -ENOTEMPTY;
  2369. goto exit1;
  2370. case LAST_DOT:
  2371. error = -EINVAL;
  2372. goto exit1;
  2373. case LAST_ROOT:
  2374. error = -EBUSY;
  2375. goto exit1;
  2376. }
  2377. nd.flags &= ~LOOKUP_PARENT;
  2378. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2379. dentry = lookup_hash(&nd);
  2380. error = PTR_ERR(dentry);
  2381. if (IS_ERR(dentry))
  2382. goto exit2;
  2383. error = mnt_want_write(nd.path.mnt);
  2384. if (error)
  2385. goto exit3;
  2386. error = security_path_rmdir(&nd.path, dentry);
  2387. if (error)
  2388. goto exit4;
  2389. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2390. exit4:
  2391. mnt_drop_write(nd.path.mnt);
  2392. exit3:
  2393. dput(dentry);
  2394. exit2:
  2395. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2396. exit1:
  2397. path_put(&nd.path);
  2398. putname(name);
  2399. return error;
  2400. }
  2401. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2402. {
  2403. return do_rmdir(AT_FDCWD, pathname);
  2404. }
  2405. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2406. {
  2407. int error = may_delete(dir, dentry, 0);
  2408. if (error)
  2409. return error;
  2410. if (!dir->i_op->unlink)
  2411. return -EPERM;
  2412. mutex_lock(&dentry->d_inode->i_mutex);
  2413. if (d_mountpoint(dentry))
  2414. error = -EBUSY;
  2415. else {
  2416. error = security_inode_unlink(dir, dentry);
  2417. if (!error) {
  2418. error = dir->i_op->unlink(dir, dentry);
  2419. if (!error)
  2420. dont_mount(dentry);
  2421. }
  2422. }
  2423. mutex_unlock(&dentry->d_inode->i_mutex);
  2424. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2425. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2426. fsnotify_link_count(dentry->d_inode);
  2427. d_delete(dentry);
  2428. }
  2429. return error;
  2430. }
  2431. /*
  2432. * Make sure that the actual truncation of the file will occur outside its
  2433. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2434. * writeout happening, and we don't want to prevent access to the directory
  2435. * while waiting on the I/O.
  2436. */
  2437. static long do_unlinkat(int dfd, const char __user *pathname)
  2438. {
  2439. int error;
  2440. char *name;
  2441. struct dentry *dentry;
  2442. struct nameidata nd;
  2443. struct inode *inode = NULL;
  2444. error = user_path_parent(dfd, pathname, &nd, &name);
  2445. if (error)
  2446. return error;
  2447. error = -EISDIR;
  2448. if (nd.last_type != LAST_NORM)
  2449. goto exit1;
  2450. nd.flags &= ~LOOKUP_PARENT;
  2451. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2452. dentry = lookup_hash(&nd);
  2453. error = PTR_ERR(dentry);
  2454. if (!IS_ERR(dentry)) {
  2455. /* Why not before? Because we want correct error value */
  2456. if (nd.last.name[nd.last.len])
  2457. goto slashes;
  2458. inode = dentry->d_inode;
  2459. if (inode)
  2460. ihold(inode);
  2461. error = mnt_want_write(nd.path.mnt);
  2462. if (error)
  2463. goto exit2;
  2464. error = security_path_unlink(&nd.path, dentry);
  2465. if (error)
  2466. goto exit3;
  2467. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2468. exit3:
  2469. mnt_drop_write(nd.path.mnt);
  2470. exit2:
  2471. dput(dentry);
  2472. }
  2473. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2474. if (inode)
  2475. iput(inode); /* truncate the inode here */
  2476. exit1:
  2477. path_put(&nd.path);
  2478. putname(name);
  2479. return error;
  2480. slashes:
  2481. error = !dentry->d_inode ? -ENOENT :
  2482. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2483. goto exit2;
  2484. }
  2485. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2486. {
  2487. if ((flag & ~AT_REMOVEDIR) != 0)
  2488. return -EINVAL;
  2489. if (flag & AT_REMOVEDIR)
  2490. return do_rmdir(dfd, pathname);
  2491. return do_unlinkat(dfd, pathname);
  2492. }
  2493. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2494. {
  2495. return do_unlinkat(AT_FDCWD, pathname);
  2496. }
  2497. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2498. {
  2499. int error = may_create(dir, dentry);
  2500. if (error)
  2501. return error;
  2502. if (!dir->i_op->symlink)
  2503. return -EPERM;
  2504. error = security_inode_symlink(dir, dentry, oldname);
  2505. if (error)
  2506. return error;
  2507. error = dir->i_op->symlink(dir, dentry, oldname);
  2508. if (!error)
  2509. fsnotify_create(dir, dentry);
  2510. return error;
  2511. }
  2512. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2513. int, newdfd, const char __user *, newname)
  2514. {
  2515. int error;
  2516. char *from;
  2517. char *to;
  2518. struct dentry *dentry;
  2519. struct nameidata nd;
  2520. from = getname(oldname);
  2521. if (IS_ERR(from))
  2522. return PTR_ERR(from);
  2523. error = user_path_parent(newdfd, newname, &nd, &to);
  2524. if (error)
  2525. goto out_putname;
  2526. dentry = lookup_create(&nd, 0);
  2527. error = PTR_ERR(dentry);
  2528. if (IS_ERR(dentry))
  2529. goto out_unlock;
  2530. error = mnt_want_write(nd.path.mnt);
  2531. if (error)
  2532. goto out_dput;
  2533. error = security_path_symlink(&nd.path, dentry, from);
  2534. if (error)
  2535. goto out_drop_write;
  2536. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2537. out_drop_write:
  2538. mnt_drop_write(nd.path.mnt);
  2539. out_dput:
  2540. dput(dentry);
  2541. out_unlock:
  2542. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2543. path_put(&nd.path);
  2544. putname(to);
  2545. out_putname:
  2546. putname(from);
  2547. return error;
  2548. }
  2549. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2550. {
  2551. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2552. }
  2553. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2554. {
  2555. struct inode *inode = old_dentry->d_inode;
  2556. int error;
  2557. if (!inode)
  2558. return -ENOENT;
  2559. error = may_create(dir, new_dentry);
  2560. if (error)
  2561. return error;
  2562. if (dir->i_sb != inode->i_sb)
  2563. return -EXDEV;
  2564. /*
  2565. * A link to an append-only or immutable file cannot be created.
  2566. */
  2567. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2568. return -EPERM;
  2569. if (!dir->i_op->link)
  2570. return -EPERM;
  2571. if (S_ISDIR(inode->i_mode))
  2572. return -EPERM;
  2573. error = security_inode_link(old_dentry, dir, new_dentry);
  2574. if (error)
  2575. return error;
  2576. mutex_lock(&inode->i_mutex);
  2577. /* Make sure we don't allow creating hardlink to an unlinked file */
  2578. if (inode->i_nlink == 0)
  2579. error = -ENOENT;
  2580. else
  2581. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2582. mutex_unlock(&inode->i_mutex);
  2583. if (!error)
  2584. fsnotify_link(dir, inode, new_dentry);
  2585. return error;
  2586. }
  2587. /*
  2588. * Hardlinks are often used in delicate situations. We avoid
  2589. * security-related surprises by not following symlinks on the
  2590. * newname. --KAB
  2591. *
  2592. * We don't follow them on the oldname either to be compatible
  2593. * with linux 2.0, and to avoid hard-linking to directories
  2594. * and other special files. --ADM
  2595. */
  2596. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2597. int, newdfd, const char __user *, newname, int, flags)
  2598. {
  2599. struct dentry *new_dentry;
  2600. struct nameidata nd;
  2601. struct path old_path;
  2602. int how = 0;
  2603. int error;
  2604. char *to;
  2605. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2606. return -EINVAL;
  2607. /*
  2608. * To use null names we require CAP_DAC_READ_SEARCH
  2609. * This ensures that not everyone will be able to create
  2610. * handlink using the passed filedescriptor.
  2611. */
  2612. if (flags & AT_EMPTY_PATH) {
  2613. if (!capable(CAP_DAC_READ_SEARCH))
  2614. return -ENOENT;
  2615. how = LOOKUP_EMPTY;
  2616. }
  2617. if (flags & AT_SYMLINK_FOLLOW)
  2618. how |= LOOKUP_FOLLOW;
  2619. error = user_path_at(olddfd, oldname, how, &old_path);
  2620. if (error)
  2621. return error;
  2622. error = user_path_parent(newdfd, newname, &nd, &to);
  2623. if (error)
  2624. goto out;
  2625. error = -EXDEV;
  2626. if (old_path.mnt != nd.path.mnt)
  2627. goto out_release;
  2628. new_dentry = lookup_create(&nd, 0);
  2629. error = PTR_ERR(new_dentry);
  2630. if (IS_ERR(new_dentry))
  2631. goto out_unlock;
  2632. error = mnt_want_write(nd.path.mnt);
  2633. if (error)
  2634. goto out_dput;
  2635. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2636. if (error)
  2637. goto out_drop_write;
  2638. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2639. out_drop_write:
  2640. mnt_drop_write(nd.path.mnt);
  2641. out_dput:
  2642. dput(new_dentry);
  2643. out_unlock:
  2644. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2645. out_release:
  2646. path_put(&nd.path);
  2647. putname(to);
  2648. out:
  2649. path_put(&old_path);
  2650. return error;
  2651. }
  2652. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2653. {
  2654. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2655. }
  2656. /*
  2657. * The worst of all namespace operations - renaming directory. "Perverted"
  2658. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2659. * Problems:
  2660. * a) we can get into loop creation. Check is done in is_subdir().
  2661. * b) race potential - two innocent renames can create a loop together.
  2662. * That's where 4.4 screws up. Current fix: serialization on
  2663. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2664. * story.
  2665. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2666. * And that - after we got ->i_mutex on parents (until then we don't know
  2667. * whether the target exists). Solution: try to be smart with locking
  2668. * order for inodes. We rely on the fact that tree topology may change
  2669. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2670. * move will be locked. Thus we can rank directories by the tree
  2671. * (ancestors first) and rank all non-directories after them.
  2672. * That works since everybody except rename does "lock parent, lookup,
  2673. * lock child" and rename is under ->s_vfs_rename_mutex.
  2674. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2675. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2676. * we'd better make sure that there's no link(2) for them.
  2677. * d) some filesystems don't support opened-but-unlinked directories,
  2678. * either because of layout or because they are not ready to deal with
  2679. * all cases correctly. The latter will be fixed (taking this sort of
  2680. * stuff into VFS), but the former is not going away. Solution: the same
  2681. * trick as in rmdir().
  2682. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2683. * we are removing the target. Solution: we will have to grab ->i_mutex
  2684. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2685. * ->i_mutex on parents, which works but leads to some truly excessive
  2686. * locking].
  2687. */
  2688. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2689. struct inode *new_dir, struct dentry *new_dentry)
  2690. {
  2691. int error = 0;
  2692. struct inode *target;
  2693. /*
  2694. * If we are going to change the parent - check write permissions,
  2695. * we'll need to flip '..'.
  2696. */
  2697. if (new_dir != old_dir) {
  2698. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2699. if (error)
  2700. return error;
  2701. }
  2702. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2703. if (error)
  2704. return error;
  2705. target = new_dentry->d_inode;
  2706. if (target)
  2707. mutex_lock(&target->i_mutex);
  2708. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2709. error = -EBUSY;
  2710. else {
  2711. if (target)
  2712. dentry_unhash(new_dentry);
  2713. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2714. }
  2715. if (target) {
  2716. if (!error) {
  2717. target->i_flags |= S_DEAD;
  2718. dont_mount(new_dentry);
  2719. }
  2720. mutex_unlock(&target->i_mutex);
  2721. if (d_unhashed(new_dentry))
  2722. d_rehash(new_dentry);
  2723. dput(new_dentry);
  2724. }
  2725. if (!error)
  2726. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2727. d_move(old_dentry,new_dentry);
  2728. return error;
  2729. }
  2730. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2731. struct inode *new_dir, struct dentry *new_dentry)
  2732. {
  2733. struct inode *target;
  2734. int error;
  2735. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2736. if (error)
  2737. return error;
  2738. dget(new_dentry);
  2739. target = new_dentry->d_inode;
  2740. if (target)
  2741. mutex_lock(&target->i_mutex);
  2742. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2743. error = -EBUSY;
  2744. else
  2745. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2746. if (!error) {
  2747. if (target)
  2748. dont_mount(new_dentry);
  2749. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2750. d_move(old_dentry, new_dentry);
  2751. }
  2752. if (target)
  2753. mutex_unlock(&target->i_mutex);
  2754. dput(new_dentry);
  2755. return error;
  2756. }
  2757. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2758. struct inode *new_dir, struct dentry *new_dentry)
  2759. {
  2760. int error;
  2761. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2762. const unsigned char *old_name;
  2763. if (old_dentry->d_inode == new_dentry->d_inode)
  2764. return 0;
  2765. error = may_delete(old_dir, old_dentry, is_dir);
  2766. if (error)
  2767. return error;
  2768. if (!new_dentry->d_inode)
  2769. error = may_create(new_dir, new_dentry);
  2770. else
  2771. error = may_delete(new_dir, new_dentry, is_dir);
  2772. if (error)
  2773. return error;
  2774. if (!old_dir->i_op->rename)
  2775. return -EPERM;
  2776. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2777. if (is_dir)
  2778. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2779. else
  2780. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2781. if (!error)
  2782. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2783. new_dentry->d_inode, old_dentry);
  2784. fsnotify_oldname_free(old_name);
  2785. return error;
  2786. }
  2787. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2788. int, newdfd, const char __user *, newname)
  2789. {
  2790. struct dentry *old_dir, *new_dir;
  2791. struct dentry *old_dentry, *new_dentry;
  2792. struct dentry *trap;
  2793. struct nameidata oldnd, newnd;
  2794. char *from;
  2795. char *to;
  2796. int error;
  2797. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2798. if (error)
  2799. goto exit;
  2800. error = user_path_parent(newdfd, newname, &newnd, &to);
  2801. if (error)
  2802. goto exit1;
  2803. error = -EXDEV;
  2804. if (oldnd.path.mnt != newnd.path.mnt)
  2805. goto exit2;
  2806. old_dir = oldnd.path.dentry;
  2807. error = -EBUSY;
  2808. if (oldnd.last_type != LAST_NORM)
  2809. goto exit2;
  2810. new_dir = newnd.path.dentry;
  2811. if (newnd.last_type != LAST_NORM)
  2812. goto exit2;
  2813. oldnd.flags &= ~LOOKUP_PARENT;
  2814. newnd.flags &= ~LOOKUP_PARENT;
  2815. newnd.flags |= LOOKUP_RENAME_TARGET;
  2816. trap = lock_rename(new_dir, old_dir);
  2817. old_dentry = lookup_hash(&oldnd);
  2818. error = PTR_ERR(old_dentry);
  2819. if (IS_ERR(old_dentry))
  2820. goto exit3;
  2821. /* source must exist */
  2822. error = -ENOENT;
  2823. if (!old_dentry->d_inode)
  2824. goto exit4;
  2825. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2826. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2827. error = -ENOTDIR;
  2828. if (oldnd.last.name[oldnd.last.len])
  2829. goto exit4;
  2830. if (newnd.last.name[newnd.last.len])
  2831. goto exit4;
  2832. }
  2833. /* source should not be ancestor of target */
  2834. error = -EINVAL;
  2835. if (old_dentry == trap)
  2836. goto exit4;
  2837. new_dentry = lookup_hash(&newnd);
  2838. error = PTR_ERR(new_dentry);
  2839. if (IS_ERR(new_dentry))
  2840. goto exit4;
  2841. /* target should not be an ancestor of source */
  2842. error = -ENOTEMPTY;
  2843. if (new_dentry == trap)
  2844. goto exit5;
  2845. error = mnt_want_write(oldnd.path.mnt);
  2846. if (error)
  2847. goto exit5;
  2848. error = security_path_rename(&oldnd.path, old_dentry,
  2849. &newnd.path, new_dentry);
  2850. if (error)
  2851. goto exit6;
  2852. error = vfs_rename(old_dir->d_inode, old_dentry,
  2853. new_dir->d_inode, new_dentry);
  2854. exit6:
  2855. mnt_drop_write(oldnd.path.mnt);
  2856. exit5:
  2857. dput(new_dentry);
  2858. exit4:
  2859. dput(old_dentry);
  2860. exit3:
  2861. unlock_rename(new_dir, old_dir);
  2862. exit2:
  2863. path_put(&newnd.path);
  2864. putname(to);
  2865. exit1:
  2866. path_put(&oldnd.path);
  2867. putname(from);
  2868. exit:
  2869. return error;
  2870. }
  2871. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2872. {
  2873. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2874. }
  2875. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2876. {
  2877. int len;
  2878. len = PTR_ERR(link);
  2879. if (IS_ERR(link))
  2880. goto out;
  2881. len = strlen(link);
  2882. if (len > (unsigned) buflen)
  2883. len = buflen;
  2884. if (copy_to_user(buffer, link, len))
  2885. len = -EFAULT;
  2886. out:
  2887. return len;
  2888. }
  2889. /*
  2890. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2891. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2892. * using) it for any given inode is up to filesystem.
  2893. */
  2894. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2895. {
  2896. struct nameidata nd;
  2897. void *cookie;
  2898. int res;
  2899. nd.depth = 0;
  2900. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2901. if (IS_ERR(cookie))
  2902. return PTR_ERR(cookie);
  2903. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2904. if (dentry->d_inode->i_op->put_link)
  2905. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2906. return res;
  2907. }
  2908. int vfs_follow_link(struct nameidata *nd, const char *link)
  2909. {
  2910. return __vfs_follow_link(nd, link);
  2911. }
  2912. /* get the link contents into pagecache */
  2913. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2914. {
  2915. char *kaddr;
  2916. struct page *page;
  2917. struct address_space *mapping = dentry->d_inode->i_mapping;
  2918. page = read_mapping_page(mapping, 0, NULL);
  2919. if (IS_ERR(page))
  2920. return (char*)page;
  2921. *ppage = page;
  2922. kaddr = kmap(page);
  2923. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2924. return kaddr;
  2925. }
  2926. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2927. {
  2928. struct page *page = NULL;
  2929. char *s = page_getlink(dentry, &page);
  2930. int res = vfs_readlink(dentry,buffer,buflen,s);
  2931. if (page) {
  2932. kunmap(page);
  2933. page_cache_release(page);
  2934. }
  2935. return res;
  2936. }
  2937. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2938. {
  2939. struct page *page = NULL;
  2940. nd_set_link(nd, page_getlink(dentry, &page));
  2941. return page;
  2942. }
  2943. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2944. {
  2945. struct page *page = cookie;
  2946. if (page) {
  2947. kunmap(page);
  2948. page_cache_release(page);
  2949. }
  2950. }
  2951. /*
  2952. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2953. */
  2954. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2955. {
  2956. struct address_space *mapping = inode->i_mapping;
  2957. struct page *page;
  2958. void *fsdata;
  2959. int err;
  2960. char *kaddr;
  2961. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2962. if (nofs)
  2963. flags |= AOP_FLAG_NOFS;
  2964. retry:
  2965. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2966. flags, &page, &fsdata);
  2967. if (err)
  2968. goto fail;
  2969. kaddr = kmap_atomic(page, KM_USER0);
  2970. memcpy(kaddr, symname, len-1);
  2971. kunmap_atomic(kaddr, KM_USER0);
  2972. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2973. page, fsdata);
  2974. if (err < 0)
  2975. goto fail;
  2976. if (err < len-1)
  2977. goto retry;
  2978. mark_inode_dirty(inode);
  2979. return 0;
  2980. fail:
  2981. return err;
  2982. }
  2983. int page_symlink(struct inode *inode, const char *symname, int len)
  2984. {
  2985. return __page_symlink(inode, symname, len,
  2986. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2987. }
  2988. const struct inode_operations page_symlink_inode_operations = {
  2989. .readlink = generic_readlink,
  2990. .follow_link = page_follow_link_light,
  2991. .put_link = page_put_link,
  2992. };
  2993. EXPORT_SYMBOL(user_path_at);
  2994. EXPORT_SYMBOL(follow_down_one);
  2995. EXPORT_SYMBOL(follow_down);
  2996. EXPORT_SYMBOL(follow_up);
  2997. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2998. EXPORT_SYMBOL(getname);
  2999. EXPORT_SYMBOL(lock_rename);
  3000. EXPORT_SYMBOL(lookup_one_len);
  3001. EXPORT_SYMBOL(page_follow_link_light);
  3002. EXPORT_SYMBOL(page_put_link);
  3003. EXPORT_SYMBOL(page_readlink);
  3004. EXPORT_SYMBOL(__page_symlink);
  3005. EXPORT_SYMBOL(page_symlink);
  3006. EXPORT_SYMBOL(page_symlink_inode_operations);
  3007. EXPORT_SYMBOL(kern_path_parent);
  3008. EXPORT_SYMBOL(kern_path);
  3009. EXPORT_SYMBOL(vfs_path_lookup);
  3010. EXPORT_SYMBOL(inode_permission);
  3011. EXPORT_SYMBOL(file_permission);
  3012. EXPORT_SYMBOL(unlock_rename);
  3013. EXPORT_SYMBOL(vfs_create);
  3014. EXPORT_SYMBOL(vfs_follow_link);
  3015. EXPORT_SYMBOL(vfs_link);
  3016. EXPORT_SYMBOL(vfs_mkdir);
  3017. EXPORT_SYMBOL(vfs_mknod);
  3018. EXPORT_SYMBOL(generic_permission);
  3019. EXPORT_SYMBOL(vfs_readlink);
  3020. EXPORT_SYMBOL(vfs_rename);
  3021. EXPORT_SYMBOL(vfs_rmdir);
  3022. EXPORT_SYMBOL(vfs_symlink);
  3023. EXPORT_SYMBOL(vfs_unlink);
  3024. EXPORT_SYMBOL(dentry_unhash);
  3025. EXPORT_SYMBOL(generic_readlink);