namei.c 84 KB

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