namei.c 88 KB

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