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. struct file *file = nd->intent.open.file;
  506. if (file && !IS_ERR(file)) {
  507. if (file->f_path.dentry == NULL)
  508. put_filp(file);
  509. else
  510. fput(file);
  511. }
  512. }
  513. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  514. {
  515. return dentry->d_op->d_revalidate(dentry, nd);
  516. }
  517. static struct dentry *
  518. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  519. {
  520. int status = d_revalidate(dentry, nd);
  521. if (unlikely(status <= 0)) {
  522. /*
  523. * The dentry failed validation.
  524. * If d_revalidate returned 0 attempt to invalidate
  525. * the dentry otherwise d_revalidate is asking us
  526. * to return a fail status.
  527. */
  528. if (status < 0) {
  529. dput(dentry);
  530. dentry = ERR_PTR(status);
  531. } else if (!d_invalidate(dentry)) {
  532. dput(dentry);
  533. dentry = NULL;
  534. }
  535. }
  536. return dentry;
  537. }
  538. static inline struct dentry *
  539. do_revalidate_rcu(struct dentry *dentry, struct nameidata *nd)
  540. {
  541. int status = d_revalidate(dentry, nd);
  542. if (likely(status > 0))
  543. return dentry;
  544. if (status == -ECHILD) {
  545. if (nameidata_dentry_drop_rcu(nd, dentry))
  546. return ERR_PTR(-ECHILD);
  547. return do_revalidate(dentry, nd);
  548. }
  549. if (status < 0)
  550. return ERR_PTR(status);
  551. /* Don't d_invalidate in rcu-walk mode */
  552. if (nameidata_dentry_drop_rcu(nd, dentry))
  553. return ERR_PTR(-ECHILD);
  554. if (!d_invalidate(dentry)) {
  555. dput(dentry);
  556. dentry = NULL;
  557. }
  558. return dentry;
  559. }
  560. static inline int need_reval_dot(struct dentry *dentry)
  561. {
  562. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  563. return 0;
  564. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  565. return 0;
  566. return 1;
  567. }
  568. /*
  569. * force_reval_path - force revalidation of a dentry
  570. *
  571. * In some situations the path walking code will trust dentries without
  572. * revalidating them. This causes problems for filesystems that depend on
  573. * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
  574. * (which indicates that it's possible for the dentry to go stale), force
  575. * a d_revalidate call before proceeding.
  576. *
  577. * Returns 0 if the revalidation was successful. If the revalidation fails,
  578. * either return the error returned by d_revalidate or -ESTALE if the
  579. * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
  580. * invalidate the dentry. It's up to the caller to handle putting references
  581. * to the path if necessary.
  582. */
  583. static int
  584. force_reval_path(struct path *path, struct nameidata *nd)
  585. {
  586. int status;
  587. struct dentry *dentry = path->dentry;
  588. /*
  589. * only check on filesystems where it's possible for the dentry to
  590. * become stale.
  591. */
  592. if (!need_reval_dot(dentry))
  593. return 0;
  594. status = d_revalidate(dentry, nd);
  595. if (status > 0)
  596. return 0;
  597. if (!status) {
  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. BUG_ON(nd->flags & LOOKUP_RCU);
  689. touch_atime(link->mnt, dentry);
  690. nd_set_link(nd, NULL);
  691. if (link->mnt == nd->path.mnt)
  692. mntget(link->mnt);
  693. nd->last_type = LAST_BIND;
  694. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  695. error = PTR_ERR(*p);
  696. if (!IS_ERR(*p)) {
  697. char *s = nd_get_link(nd);
  698. error = 0;
  699. if (s)
  700. error = __vfs_follow_link(nd, s);
  701. else if (nd->last_type == LAST_BIND) {
  702. error = force_reval_path(&nd->path, nd);
  703. if (error)
  704. path_put(&nd->path);
  705. }
  706. }
  707. return error;
  708. }
  709. /*
  710. * This limits recursive symlink follows to 8, while
  711. * limiting consecutive symlinks to 40.
  712. *
  713. * Without that kind of total limit, nasty chains of consecutive
  714. * symlinks can cause almost arbitrarily long lookups.
  715. */
  716. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  717. {
  718. void *cookie;
  719. int err = -ELOOP;
  720. /* We drop rcu-walk here */
  721. if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
  722. return -ECHILD;
  723. if (current->link_count >= MAX_NESTED_LINKS)
  724. goto loop;
  725. if (current->total_link_count >= 40)
  726. goto loop;
  727. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  728. cond_resched();
  729. err = security_inode_follow_link(path->dentry, nd);
  730. if (err)
  731. goto loop;
  732. current->link_count++;
  733. current->total_link_count++;
  734. nd->depth++;
  735. err = __do_follow_link(path, nd, &cookie);
  736. if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
  737. path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
  738. path_put(path);
  739. current->link_count--;
  740. nd->depth--;
  741. return err;
  742. loop:
  743. path_put_conditional(path, nd);
  744. path_put(&nd->path);
  745. return err;
  746. }
  747. static int follow_up_rcu(struct path *path)
  748. {
  749. struct vfsmount *parent;
  750. struct dentry *mountpoint;
  751. parent = path->mnt->mnt_parent;
  752. if (parent == path->mnt)
  753. return 0;
  754. mountpoint = path->mnt->mnt_mountpoint;
  755. path->dentry = mountpoint;
  756. path->mnt = parent;
  757. return 1;
  758. }
  759. int follow_up(struct path *path)
  760. {
  761. struct vfsmount *parent;
  762. struct dentry *mountpoint;
  763. br_read_lock(vfsmount_lock);
  764. parent = path->mnt->mnt_parent;
  765. if (parent == path->mnt) {
  766. br_read_unlock(vfsmount_lock);
  767. return 0;
  768. }
  769. mntget(parent);
  770. mountpoint = dget(path->mnt->mnt_mountpoint);
  771. br_read_unlock(vfsmount_lock);
  772. dput(path->dentry);
  773. path->dentry = mountpoint;
  774. mntput(path->mnt);
  775. path->mnt = parent;
  776. return 1;
  777. }
  778. /*
  779. * Perform an automount
  780. * - return -EISDIR to tell follow_managed() to stop and return the path we
  781. * were called with.
  782. */
  783. static int follow_automount(struct path *path, unsigned flags,
  784. bool *need_mntput)
  785. {
  786. struct vfsmount *mnt;
  787. int err;
  788. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  789. return -EREMOTE;
  790. /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
  791. * and this is the terminal part of the path.
  792. */
  793. if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
  794. return -EISDIR; /* we actually want to stop here */
  795. /* We want to mount if someone is trying to open/create a file of any
  796. * type under the mountpoint, wants to traverse through the mountpoint
  797. * or wants to open the mounted directory.
  798. *
  799. * We don't want to mount if someone's just doing a stat and they've
  800. * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
  801. * appended a '/' to the name.
  802. */
  803. if (!(flags & LOOKUP_FOLLOW) &&
  804. !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
  805. LOOKUP_OPEN | LOOKUP_CREATE)))
  806. return -EISDIR;
  807. current->total_link_count++;
  808. if (current->total_link_count >= 40)
  809. return -ELOOP;
  810. mnt = path->dentry->d_op->d_automount(path);
  811. if (IS_ERR(mnt)) {
  812. /*
  813. * The filesystem is allowed to return -EISDIR here to indicate
  814. * it doesn't want to automount. For instance, autofs would do
  815. * this so that its userspace daemon can mount on this dentry.
  816. *
  817. * However, we can only permit this if it's a terminal point in
  818. * the path being looked up; if it wasn't then the remainder of
  819. * the path is inaccessible and we should say so.
  820. */
  821. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
  822. return -EREMOTE;
  823. return PTR_ERR(mnt);
  824. }
  825. if (!mnt) /* mount collision */
  826. return 0;
  827. err = finish_automount(mnt, path);
  828. switch (err) {
  829. case -EBUSY:
  830. /* Someone else made a mount here whilst we were busy */
  831. return 0;
  832. case 0:
  833. dput(path->dentry);
  834. if (*need_mntput)
  835. mntput(path->mnt);
  836. path->mnt = mnt;
  837. path->dentry = dget(mnt->mnt_root);
  838. *need_mntput = true;
  839. return 0;
  840. default:
  841. return err;
  842. }
  843. }
  844. /*
  845. * Handle a dentry that is managed in some way.
  846. * - Flagged for transit management (autofs)
  847. * - Flagged as mountpoint
  848. * - Flagged as automount point
  849. *
  850. * This may only be called in refwalk mode.
  851. *
  852. * Serialization is taken care of in namespace.c
  853. */
  854. static int follow_managed(struct path *path, unsigned flags)
  855. {
  856. unsigned managed;
  857. bool need_mntput = false;
  858. int ret;
  859. /* Given that we're not holding a lock here, we retain the value in a
  860. * local variable for each dentry as we look at it so that we don't see
  861. * the components of that value change under us */
  862. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  863. managed &= DCACHE_MANAGED_DENTRY,
  864. unlikely(managed != 0)) {
  865. /* Allow the filesystem to manage the transit without i_mutex
  866. * being held. */
  867. if (managed & DCACHE_MANAGE_TRANSIT) {
  868. BUG_ON(!path->dentry->d_op);
  869. BUG_ON(!path->dentry->d_op->d_manage);
  870. ret = path->dentry->d_op->d_manage(path->dentry,
  871. false, false);
  872. if (ret < 0)
  873. return ret == -EISDIR ? 0 : ret;
  874. }
  875. /* Transit to a mounted filesystem. */
  876. if (managed & DCACHE_MOUNTED) {
  877. struct vfsmount *mounted = lookup_mnt(path);
  878. if (mounted) {
  879. dput(path->dentry);
  880. if (need_mntput)
  881. mntput(path->mnt);
  882. path->mnt = mounted;
  883. path->dentry = dget(mounted->mnt_root);
  884. need_mntput = true;
  885. continue;
  886. }
  887. /* Something is mounted on this dentry in another
  888. * namespace and/or whatever was mounted there in this
  889. * namespace got unmounted before we managed to get the
  890. * vfsmount_lock */
  891. }
  892. /* Handle an automount point */
  893. if (managed & DCACHE_NEED_AUTOMOUNT) {
  894. ret = follow_automount(path, flags, &need_mntput);
  895. if (ret < 0)
  896. return ret == -EISDIR ? 0 : ret;
  897. continue;
  898. }
  899. /* We didn't change the current path point */
  900. break;
  901. }
  902. return 0;
  903. }
  904. int follow_down_one(struct path *path)
  905. {
  906. struct vfsmount *mounted;
  907. mounted = lookup_mnt(path);
  908. if (mounted) {
  909. dput(path->dentry);
  910. mntput(path->mnt);
  911. path->mnt = mounted;
  912. path->dentry = dget(mounted->mnt_root);
  913. return 1;
  914. }
  915. return 0;
  916. }
  917. /*
  918. * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
  919. * meet a managed dentry and we're not walking to "..". True is returned to
  920. * continue, false to abort.
  921. */
  922. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  923. struct inode **inode, bool reverse_transit)
  924. {
  925. while (d_mountpoint(path->dentry)) {
  926. struct vfsmount *mounted;
  927. if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  928. !reverse_transit &&
  929. path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
  930. return false;
  931. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  932. if (!mounted)
  933. break;
  934. path->mnt = mounted;
  935. path->dentry = mounted->mnt_root;
  936. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  937. *inode = path->dentry->d_inode;
  938. }
  939. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  940. return reverse_transit;
  941. return true;
  942. }
  943. static int follow_dotdot_rcu(struct nameidata *nd)
  944. {
  945. struct inode *inode = nd->inode;
  946. set_root_rcu(nd);
  947. while (1) {
  948. if (nd->path.dentry == nd->root.dentry &&
  949. nd->path.mnt == nd->root.mnt) {
  950. break;
  951. }
  952. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  953. struct dentry *old = nd->path.dentry;
  954. struct dentry *parent = old->d_parent;
  955. unsigned seq;
  956. seq = read_seqcount_begin(&parent->d_seq);
  957. if (read_seqcount_retry(&old->d_seq, nd->seq))
  958. return -ECHILD;
  959. inode = parent->d_inode;
  960. nd->path.dentry = parent;
  961. nd->seq = seq;
  962. break;
  963. }
  964. if (!follow_up_rcu(&nd->path))
  965. break;
  966. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  967. inode = nd->path.dentry->d_inode;
  968. }
  969. __follow_mount_rcu(nd, &nd->path, &inode, true);
  970. nd->inode = inode;
  971. return 0;
  972. }
  973. /*
  974. * Follow down to the covering mount currently visible to userspace. At each
  975. * point, the filesystem owning that dentry may be queried as to whether the
  976. * caller is permitted to proceed or not.
  977. *
  978. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  979. * being true).
  980. */
  981. int follow_down(struct path *path, bool mounting_here)
  982. {
  983. unsigned managed;
  984. int ret;
  985. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  986. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  987. /* Allow the filesystem to manage the transit without i_mutex
  988. * being held.
  989. *
  990. * We indicate to the filesystem if someone is trying to mount
  991. * something here. This gives autofs the chance to deny anyone
  992. * other than its daemon the right to mount on its
  993. * superstructure.
  994. *
  995. * The filesystem may sleep at this point.
  996. */
  997. if (managed & DCACHE_MANAGE_TRANSIT) {
  998. BUG_ON(!path->dentry->d_op);
  999. BUG_ON(!path->dentry->d_op->d_manage);
  1000. ret = path->dentry->d_op->d_manage(
  1001. path->dentry, mounting_here, false);
  1002. if (ret < 0)
  1003. return ret == -EISDIR ? 0 : ret;
  1004. }
  1005. /* Transit to a mounted filesystem. */
  1006. if (managed & DCACHE_MOUNTED) {
  1007. struct vfsmount *mounted = lookup_mnt(path);
  1008. if (!mounted)
  1009. break;
  1010. dput(path->dentry);
  1011. mntput(path->mnt);
  1012. path->mnt = mounted;
  1013. path->dentry = dget(mounted->mnt_root);
  1014. continue;
  1015. }
  1016. /* Don't handle automount points here */
  1017. break;
  1018. }
  1019. return 0;
  1020. }
  1021. /*
  1022. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1023. */
  1024. static void follow_mount(struct path *path)
  1025. {
  1026. while (d_mountpoint(path->dentry)) {
  1027. struct vfsmount *mounted = lookup_mnt(path);
  1028. if (!mounted)
  1029. break;
  1030. dput(path->dentry);
  1031. mntput(path->mnt);
  1032. path->mnt = mounted;
  1033. path->dentry = dget(mounted->mnt_root);
  1034. }
  1035. }
  1036. static void follow_dotdot(struct nameidata *nd)
  1037. {
  1038. set_root(nd);
  1039. while(1) {
  1040. struct dentry *old = nd->path.dentry;
  1041. if (nd->path.dentry == nd->root.dentry &&
  1042. nd->path.mnt == nd->root.mnt) {
  1043. break;
  1044. }
  1045. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1046. /* rare case of legitimate dget_parent()... */
  1047. nd->path.dentry = dget_parent(nd->path.dentry);
  1048. dput(old);
  1049. break;
  1050. }
  1051. if (!follow_up(&nd->path))
  1052. break;
  1053. }
  1054. follow_mount(&nd->path);
  1055. nd->inode = nd->path.dentry->d_inode;
  1056. }
  1057. /*
  1058. * Allocate a dentry with name and parent, and perform a parent
  1059. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  1060. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  1061. * have verified that no child exists while under i_mutex.
  1062. */
  1063. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  1064. struct qstr *name, struct nameidata *nd)
  1065. {
  1066. struct inode *inode = parent->d_inode;
  1067. struct dentry *dentry;
  1068. struct dentry *old;
  1069. /* Don't create child dentry for a dead directory. */
  1070. if (unlikely(IS_DEADDIR(inode)))
  1071. return ERR_PTR(-ENOENT);
  1072. dentry = d_alloc(parent, name);
  1073. if (unlikely(!dentry))
  1074. return ERR_PTR(-ENOMEM);
  1075. old = inode->i_op->lookup(inode, dentry, nd);
  1076. if (unlikely(old)) {
  1077. dput(dentry);
  1078. dentry = old;
  1079. }
  1080. return dentry;
  1081. }
  1082. /*
  1083. * It's more convoluted than I'd like it to be, but... it's still fairly
  1084. * small and for now I'd prefer to have fast path as straight as possible.
  1085. * It _is_ time-critical.
  1086. */
  1087. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1088. struct path *path, struct inode **inode)
  1089. {
  1090. struct vfsmount *mnt = nd->path.mnt;
  1091. struct dentry *dentry, *parent = nd->path.dentry;
  1092. struct inode *dir;
  1093. int err;
  1094. /*
  1095. * See if the low-level filesystem might want
  1096. * to use its own hash..
  1097. */
  1098. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1099. err = parent->d_op->d_hash(parent, nd->inode, name);
  1100. if (err < 0)
  1101. return err;
  1102. }
  1103. /*
  1104. * Rename seqlock is not required here because in the off chance
  1105. * of a false negative due to a concurrent rename, we're going to
  1106. * do the non-racy lookup, below.
  1107. */
  1108. if (nd->flags & LOOKUP_RCU) {
  1109. unsigned seq;
  1110. *inode = nd->inode;
  1111. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1112. if (!dentry) {
  1113. if (nameidata_drop_rcu(nd))
  1114. return -ECHILD;
  1115. goto need_lookup;
  1116. }
  1117. /* Memory barrier in read_seqcount_begin of child is enough */
  1118. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1119. return -ECHILD;
  1120. nd->seq = seq;
  1121. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1122. dentry = do_revalidate_rcu(dentry, nd);
  1123. if (!dentry)
  1124. goto need_lookup;
  1125. if (IS_ERR(dentry))
  1126. goto fail;
  1127. if (!(nd->flags & LOOKUP_RCU))
  1128. goto done;
  1129. }
  1130. path->mnt = mnt;
  1131. path->dentry = dentry;
  1132. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1133. return 0;
  1134. if (nameidata_drop_rcu(nd))
  1135. return -ECHILD;
  1136. /* fallthru */
  1137. }
  1138. dentry = __d_lookup(parent, name);
  1139. if (!dentry)
  1140. goto need_lookup;
  1141. found:
  1142. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1143. dentry = do_revalidate(dentry, nd);
  1144. if (!dentry)
  1145. goto need_lookup;
  1146. if (IS_ERR(dentry))
  1147. goto fail;
  1148. }
  1149. done:
  1150. path->mnt = mnt;
  1151. path->dentry = dentry;
  1152. err = follow_managed(path, nd->flags);
  1153. if (unlikely(err < 0)) {
  1154. path_put_conditional(path, nd);
  1155. return err;
  1156. }
  1157. *inode = path->dentry->d_inode;
  1158. return 0;
  1159. need_lookup:
  1160. dir = parent->d_inode;
  1161. BUG_ON(nd->inode != dir);
  1162. mutex_lock(&dir->i_mutex);
  1163. /*
  1164. * First re-do the cached lookup just in case it was created
  1165. * while we waited for the directory semaphore, or the first
  1166. * lookup failed due to an unrelated rename.
  1167. *
  1168. * This could use version numbering or similar to avoid unnecessary
  1169. * cache lookups, but then we'd have to do the first lookup in the
  1170. * non-racy way. However in the common case here, everything should
  1171. * be hot in cache, so would it be a big win?
  1172. */
  1173. dentry = d_lookup(parent, name);
  1174. if (likely(!dentry)) {
  1175. dentry = d_alloc_and_lookup(parent, name, nd);
  1176. mutex_unlock(&dir->i_mutex);
  1177. if (IS_ERR(dentry))
  1178. goto fail;
  1179. goto done;
  1180. }
  1181. /*
  1182. * Uhhuh! Nasty case: the cache was re-populated while
  1183. * we waited on the semaphore. Need to revalidate.
  1184. */
  1185. mutex_unlock(&dir->i_mutex);
  1186. goto found;
  1187. fail:
  1188. return PTR_ERR(dentry);
  1189. }
  1190. /*
  1191. * Name resolution.
  1192. * This is the basic name resolution function, turning a pathname into
  1193. * the final dentry. We expect 'base' to be positive and a directory.
  1194. *
  1195. * Returns 0 and nd will have valid dentry and mnt on success.
  1196. * Returns error and drops reference to input namei data on failure.
  1197. */
  1198. static int link_path_walk(const char *name, struct nameidata *nd)
  1199. {
  1200. struct path next;
  1201. int err;
  1202. unsigned int lookup_flags = nd->flags;
  1203. while (*name=='/')
  1204. name++;
  1205. if (!*name)
  1206. goto return_reval;
  1207. if (nd->depth)
  1208. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  1209. /* At this point we know we have a real path component. */
  1210. for(;;) {
  1211. struct inode *inode;
  1212. unsigned long hash;
  1213. struct qstr this;
  1214. unsigned int c;
  1215. nd->flags |= LOOKUP_CONTINUE;
  1216. if (nd->flags & LOOKUP_RCU) {
  1217. err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1218. if (err == -ECHILD) {
  1219. if (nameidata_drop_rcu(nd))
  1220. return -ECHILD;
  1221. goto exec_again;
  1222. }
  1223. } else {
  1224. exec_again:
  1225. err = exec_permission(nd->inode, 0);
  1226. }
  1227. if (err)
  1228. break;
  1229. this.name = name;
  1230. c = *(const unsigned char *)name;
  1231. hash = init_name_hash();
  1232. do {
  1233. name++;
  1234. hash = partial_name_hash(c, hash);
  1235. c = *(const unsigned char *)name;
  1236. } while (c && (c != '/'));
  1237. this.len = name - (const char *) this.name;
  1238. this.hash = end_name_hash(hash);
  1239. /* remove trailing slashes? */
  1240. if (!c)
  1241. goto last_component;
  1242. while (*++name == '/');
  1243. if (!*name)
  1244. goto last_with_slashes;
  1245. /*
  1246. * "." and ".." are special - ".." especially so because it has
  1247. * to be able to know about the current root directory and
  1248. * parent relationships.
  1249. */
  1250. if (this.name[0] == '.') switch (this.len) {
  1251. default:
  1252. break;
  1253. case 2:
  1254. if (this.name[1] != '.')
  1255. break;
  1256. if (nd->flags & LOOKUP_RCU) {
  1257. if (follow_dotdot_rcu(nd))
  1258. return -ECHILD;
  1259. } else
  1260. follow_dotdot(nd);
  1261. /* fallthrough */
  1262. case 1:
  1263. continue;
  1264. }
  1265. /* This does the actual lookups.. */
  1266. err = do_lookup(nd, &this, &next, &inode);
  1267. if (err)
  1268. break;
  1269. err = -ENOENT;
  1270. if (!inode)
  1271. goto out_dput;
  1272. if (inode->i_op->follow_link) {
  1273. BUG_ON(inode != next.dentry->d_inode);
  1274. err = do_follow_link(&next, nd);
  1275. if (err)
  1276. goto return_err;
  1277. nd->inode = nd->path.dentry->d_inode;
  1278. err = -ENOENT;
  1279. if (!nd->inode)
  1280. break;
  1281. } else {
  1282. path_to_nameidata(&next, nd);
  1283. nd->inode = inode;
  1284. }
  1285. err = -ENOTDIR;
  1286. if (!nd->inode->i_op->lookup)
  1287. break;
  1288. continue;
  1289. /* here ends the main loop */
  1290. last_with_slashes:
  1291. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1292. last_component:
  1293. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1294. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1295. if (lookup_flags & LOOKUP_PARENT)
  1296. goto lookup_parent;
  1297. if (this.name[0] == '.') switch (this.len) {
  1298. default:
  1299. break;
  1300. case 2:
  1301. if (this.name[1] != '.')
  1302. break;
  1303. if (nd->flags & LOOKUP_RCU) {
  1304. if (follow_dotdot_rcu(nd))
  1305. return -ECHILD;
  1306. } else
  1307. follow_dotdot(nd);
  1308. /* fallthrough */
  1309. case 1:
  1310. goto return_reval;
  1311. }
  1312. err = do_lookup(nd, &this, &next, &inode);
  1313. if (err)
  1314. break;
  1315. if (inode && unlikely(inode->i_op->follow_link) &&
  1316. (lookup_flags & LOOKUP_FOLLOW)) {
  1317. BUG_ON(inode != next.dentry->d_inode);
  1318. err = do_follow_link(&next, nd);
  1319. if (err)
  1320. goto return_err;
  1321. nd->inode = nd->path.dentry->d_inode;
  1322. } else {
  1323. path_to_nameidata(&next, nd);
  1324. nd->inode = inode;
  1325. }
  1326. err = -ENOENT;
  1327. if (!nd->inode)
  1328. break;
  1329. if (lookup_flags & LOOKUP_DIRECTORY) {
  1330. err = -ENOTDIR;
  1331. if (!nd->inode->i_op->lookup)
  1332. break;
  1333. }
  1334. goto return_base;
  1335. lookup_parent:
  1336. nd->last = this;
  1337. nd->last_type = LAST_NORM;
  1338. if (this.name[0] != '.')
  1339. goto return_base;
  1340. if (this.len == 1)
  1341. nd->last_type = LAST_DOT;
  1342. else if (this.len == 2 && this.name[1] == '.')
  1343. nd->last_type = LAST_DOTDOT;
  1344. else
  1345. goto return_base;
  1346. return_reval:
  1347. /*
  1348. * We bypassed the ordinary revalidation routines.
  1349. * We may need to check the cached dentry for staleness.
  1350. */
  1351. if (need_reval_dot(nd->path.dentry)) {
  1352. if (nameidata_drop_rcu_last_maybe(nd))
  1353. return -ECHILD;
  1354. /* Note: we do not d_invalidate() */
  1355. err = d_revalidate(nd->path.dentry, nd);
  1356. if (!err)
  1357. err = -ESTALE;
  1358. if (err < 0)
  1359. break;
  1360. return 0;
  1361. }
  1362. return_base:
  1363. if (nameidata_drop_rcu_last_maybe(nd))
  1364. return -ECHILD;
  1365. return 0;
  1366. out_dput:
  1367. if (!(nd->flags & LOOKUP_RCU))
  1368. path_put_conditional(&next, nd);
  1369. break;
  1370. }
  1371. if (!(nd->flags & LOOKUP_RCU))
  1372. path_put(&nd->path);
  1373. return_err:
  1374. return err;
  1375. }
  1376. static inline int path_walk_rcu(const char *name, struct nameidata *nd)
  1377. {
  1378. current->total_link_count = 0;
  1379. return link_path_walk(name, nd);
  1380. }
  1381. static inline int path_walk_simple(const char *name, struct nameidata *nd)
  1382. {
  1383. current->total_link_count = 0;
  1384. return link_path_walk(name, nd);
  1385. }
  1386. static int path_walk(const char *name, struct nameidata *nd)
  1387. {
  1388. struct path save = nd->path;
  1389. int result;
  1390. current->total_link_count = 0;
  1391. /* make sure the stuff we saved doesn't go away */
  1392. path_get(&save);
  1393. result = link_path_walk(name, nd);
  1394. if (result == -ESTALE) {
  1395. /* nd->path had been dropped */
  1396. current->total_link_count = 0;
  1397. nd->path = save;
  1398. path_get(&nd->path);
  1399. nd->flags |= LOOKUP_REVAL;
  1400. result = link_path_walk(name, nd);
  1401. }
  1402. path_put(&save);
  1403. return result;
  1404. }
  1405. static void path_finish_rcu(struct nameidata *nd)
  1406. {
  1407. if (nd->flags & LOOKUP_RCU) {
  1408. /* RCU dangling. Cancel it. */
  1409. nd->flags &= ~LOOKUP_RCU;
  1410. nd->root.mnt = NULL;
  1411. rcu_read_unlock();
  1412. br_read_unlock(vfsmount_lock);
  1413. }
  1414. if (nd->file)
  1415. fput(nd->file);
  1416. }
  1417. static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  1418. {
  1419. int retval = 0;
  1420. int fput_needed;
  1421. struct file *file;
  1422. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1423. nd->flags = flags | LOOKUP_RCU;
  1424. nd->depth = 0;
  1425. nd->root.mnt = NULL;
  1426. nd->file = NULL;
  1427. if (*name=='/') {
  1428. struct fs_struct *fs = current->fs;
  1429. unsigned seq;
  1430. br_read_lock(vfsmount_lock);
  1431. rcu_read_lock();
  1432. do {
  1433. seq = read_seqcount_begin(&fs->seq);
  1434. nd->root = fs->root;
  1435. nd->path = nd->root;
  1436. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1437. } while (read_seqcount_retry(&fs->seq, seq));
  1438. } else if (dfd == AT_FDCWD) {
  1439. struct fs_struct *fs = current->fs;
  1440. unsigned seq;
  1441. br_read_lock(vfsmount_lock);
  1442. rcu_read_lock();
  1443. do {
  1444. seq = read_seqcount_begin(&fs->seq);
  1445. nd->path = fs->pwd;
  1446. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1447. } while (read_seqcount_retry(&fs->seq, seq));
  1448. } else {
  1449. struct dentry *dentry;
  1450. file = fget_light(dfd, &fput_needed);
  1451. retval = -EBADF;
  1452. if (!file)
  1453. goto out_fail;
  1454. dentry = file->f_path.dentry;
  1455. retval = -ENOTDIR;
  1456. if (!S_ISDIR(dentry->d_inode->i_mode))
  1457. goto fput_fail;
  1458. retval = file_permission(file, MAY_EXEC);
  1459. if (retval)
  1460. goto fput_fail;
  1461. nd->path = file->f_path;
  1462. if (fput_needed)
  1463. nd->file = file;
  1464. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1465. br_read_lock(vfsmount_lock);
  1466. rcu_read_lock();
  1467. }
  1468. nd->inode = nd->path.dentry->d_inode;
  1469. return 0;
  1470. fput_fail:
  1471. fput_light(file, fput_needed);
  1472. out_fail:
  1473. return retval;
  1474. }
  1475. static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  1476. {
  1477. int retval = 0;
  1478. int fput_needed;
  1479. struct file *file;
  1480. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1481. nd->flags = flags;
  1482. nd->depth = 0;
  1483. nd->root.mnt = NULL;
  1484. if (*name=='/') {
  1485. set_root(nd);
  1486. nd->path = nd->root;
  1487. path_get(&nd->root);
  1488. } else if (dfd == AT_FDCWD) {
  1489. get_fs_pwd(current->fs, &nd->path);
  1490. } else {
  1491. struct dentry *dentry;
  1492. file = fget_light(dfd, &fput_needed);
  1493. retval = -EBADF;
  1494. if (!file)
  1495. goto out_fail;
  1496. dentry = file->f_path.dentry;
  1497. retval = -ENOTDIR;
  1498. if (!S_ISDIR(dentry->d_inode->i_mode))
  1499. goto fput_fail;
  1500. retval = file_permission(file, MAY_EXEC);
  1501. if (retval)
  1502. goto fput_fail;
  1503. nd->path = file->f_path;
  1504. path_get(&file->f_path);
  1505. fput_light(file, fput_needed);
  1506. }
  1507. nd->inode = nd->path.dentry->d_inode;
  1508. return 0;
  1509. fput_fail:
  1510. fput_light(file, fput_needed);
  1511. out_fail:
  1512. return retval;
  1513. }
  1514. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1515. static int do_path_lookup(int dfd, const char *name,
  1516. unsigned int flags, struct nameidata *nd)
  1517. {
  1518. int retval;
  1519. /*
  1520. * Path walking is largely split up into 2 different synchronisation
  1521. * schemes, rcu-walk and ref-walk (explained in
  1522. * Documentation/filesystems/path-lookup.txt). These share much of the
  1523. * path walk code, but some things particularly setup, cleanup, and
  1524. * following mounts are sufficiently divergent that functions are
  1525. * duplicated. Typically there is a function foo(), and its RCU
  1526. * analogue, foo_rcu().
  1527. *
  1528. * -ECHILD is the error number of choice (just to avoid clashes) that
  1529. * is returned if some aspect of an rcu-walk fails. Such an error must
  1530. * be handled by restarting a traditional ref-walk (which will always
  1531. * be able to complete).
  1532. */
  1533. retval = path_init_rcu(dfd, name, flags, nd);
  1534. if (unlikely(retval))
  1535. return retval;
  1536. retval = path_walk_rcu(name, nd);
  1537. path_finish_rcu(nd);
  1538. if (nd->root.mnt) {
  1539. path_put(&nd->root);
  1540. nd->root.mnt = NULL;
  1541. }
  1542. if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
  1543. /* slower, locked walk */
  1544. if (retval == -ESTALE)
  1545. flags |= LOOKUP_REVAL;
  1546. retval = path_init(dfd, name, flags, nd);
  1547. if (unlikely(retval))
  1548. return retval;
  1549. retval = path_walk(name, nd);
  1550. if (nd->root.mnt) {
  1551. path_put(&nd->root);
  1552. nd->root.mnt = NULL;
  1553. }
  1554. }
  1555. if (likely(!retval)) {
  1556. if (unlikely(!audit_dummy_context())) {
  1557. if (nd->path.dentry && nd->inode)
  1558. audit_inode(name, nd->path.dentry);
  1559. }
  1560. }
  1561. return retval;
  1562. }
  1563. int path_lookup(const char *name, unsigned int flags,
  1564. struct nameidata *nd)
  1565. {
  1566. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1567. }
  1568. int kern_path(const char *name, unsigned int flags, struct path *path)
  1569. {
  1570. struct nameidata nd;
  1571. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1572. if (!res)
  1573. *path = nd.path;
  1574. return res;
  1575. }
  1576. /**
  1577. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1578. * @dentry: pointer to dentry of the base directory
  1579. * @mnt: pointer to vfs mount of the base directory
  1580. * @name: pointer to file name
  1581. * @flags: lookup flags
  1582. * @nd: pointer to nameidata
  1583. */
  1584. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1585. const char *name, unsigned int flags,
  1586. struct nameidata *nd)
  1587. {
  1588. int retval;
  1589. /* same as do_path_lookup */
  1590. nd->last_type = LAST_ROOT;
  1591. nd->flags = flags;
  1592. nd->depth = 0;
  1593. nd->path.dentry = dentry;
  1594. nd->path.mnt = mnt;
  1595. path_get(&nd->path);
  1596. nd->root = nd->path;
  1597. path_get(&nd->root);
  1598. nd->inode = nd->path.dentry->d_inode;
  1599. retval = path_walk(name, nd);
  1600. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  1601. nd->inode))
  1602. audit_inode(name, nd->path.dentry);
  1603. path_put(&nd->root);
  1604. nd->root.mnt = NULL;
  1605. return retval;
  1606. }
  1607. static struct dentry *__lookup_hash(struct qstr *name,
  1608. struct dentry *base, struct nameidata *nd)
  1609. {
  1610. struct inode *inode = base->d_inode;
  1611. struct dentry *dentry;
  1612. int err;
  1613. err = exec_permission(inode, 0);
  1614. if (err)
  1615. return ERR_PTR(err);
  1616. /*
  1617. * See if the low-level filesystem might want
  1618. * to use its own hash..
  1619. */
  1620. if (base->d_flags & DCACHE_OP_HASH) {
  1621. err = base->d_op->d_hash(base, inode, name);
  1622. dentry = ERR_PTR(err);
  1623. if (err < 0)
  1624. goto out;
  1625. }
  1626. /*
  1627. * Don't bother with __d_lookup: callers are for creat as
  1628. * well as unlink, so a lot of the time it would cost
  1629. * a double lookup.
  1630. */
  1631. dentry = d_lookup(base, name);
  1632. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1633. dentry = do_revalidate(dentry, nd);
  1634. if (!dentry)
  1635. dentry = d_alloc_and_lookup(base, name, nd);
  1636. out:
  1637. return dentry;
  1638. }
  1639. /*
  1640. * Restricted form of lookup. Doesn't follow links, single-component only,
  1641. * needs parent already locked. Doesn't follow mounts.
  1642. * SMP-safe.
  1643. */
  1644. static struct dentry *lookup_hash(struct nameidata *nd)
  1645. {
  1646. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1647. }
  1648. static int __lookup_one_len(const char *name, struct qstr *this,
  1649. struct dentry *base, int len)
  1650. {
  1651. unsigned long hash;
  1652. unsigned int c;
  1653. this->name = name;
  1654. this->len = len;
  1655. if (!len)
  1656. return -EACCES;
  1657. hash = init_name_hash();
  1658. while (len--) {
  1659. c = *(const unsigned char *)name++;
  1660. if (c == '/' || c == '\0')
  1661. return -EACCES;
  1662. hash = partial_name_hash(c, hash);
  1663. }
  1664. this->hash = end_name_hash(hash);
  1665. return 0;
  1666. }
  1667. /**
  1668. * lookup_one_len - filesystem helper to lookup single pathname component
  1669. * @name: pathname component to lookup
  1670. * @base: base directory to lookup from
  1671. * @len: maximum length @len should be interpreted to
  1672. *
  1673. * Note that this routine is purely a helper for filesystem usage and should
  1674. * not be called by generic code. Also note that by using this function the
  1675. * nameidata argument is passed to the filesystem methods and a filesystem
  1676. * using this helper needs to be prepared for that.
  1677. */
  1678. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1679. {
  1680. int err;
  1681. struct qstr this;
  1682. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1683. err = __lookup_one_len(name, &this, base, len);
  1684. if (err)
  1685. return ERR_PTR(err);
  1686. return __lookup_hash(&this, base, NULL);
  1687. }
  1688. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1689. struct path *path)
  1690. {
  1691. struct nameidata nd;
  1692. char *tmp = getname(name);
  1693. int err = PTR_ERR(tmp);
  1694. if (!IS_ERR(tmp)) {
  1695. BUG_ON(flags & LOOKUP_PARENT);
  1696. err = do_path_lookup(dfd, tmp, flags, &nd);
  1697. putname(tmp);
  1698. if (!err)
  1699. *path = nd.path;
  1700. }
  1701. return err;
  1702. }
  1703. static int user_path_parent(int dfd, const char __user *path,
  1704. struct nameidata *nd, char **name)
  1705. {
  1706. char *s = getname(path);
  1707. int error;
  1708. if (IS_ERR(s))
  1709. return PTR_ERR(s);
  1710. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1711. if (error)
  1712. putname(s);
  1713. else
  1714. *name = s;
  1715. return error;
  1716. }
  1717. /*
  1718. * It's inline, so penalty for filesystems that don't use sticky bit is
  1719. * minimal.
  1720. */
  1721. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1722. {
  1723. uid_t fsuid = current_fsuid();
  1724. if (!(dir->i_mode & S_ISVTX))
  1725. return 0;
  1726. if (inode->i_uid == fsuid)
  1727. return 0;
  1728. if (dir->i_uid == fsuid)
  1729. return 0;
  1730. return !capable(CAP_FOWNER);
  1731. }
  1732. /*
  1733. * Check whether we can remove a link victim from directory dir, check
  1734. * whether the type of victim is right.
  1735. * 1. We can't do it if dir is read-only (done in permission())
  1736. * 2. We should have write and exec permissions on dir
  1737. * 3. We can't remove anything from append-only dir
  1738. * 4. We can't do anything with immutable dir (done in permission())
  1739. * 5. If the sticky bit on dir is set we should either
  1740. * a. be owner of dir, or
  1741. * b. be owner of victim, or
  1742. * c. have CAP_FOWNER capability
  1743. * 6. If the victim is append-only or immutable we can't do antyhing with
  1744. * links pointing to it.
  1745. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1746. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1747. * 9. We can't remove a root or mountpoint.
  1748. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1749. * nfs_async_unlink().
  1750. */
  1751. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1752. {
  1753. int error;
  1754. if (!victim->d_inode)
  1755. return -ENOENT;
  1756. BUG_ON(victim->d_parent->d_inode != dir);
  1757. audit_inode_child(victim, dir);
  1758. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1759. if (error)
  1760. return error;
  1761. if (IS_APPEND(dir))
  1762. return -EPERM;
  1763. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1764. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1765. return -EPERM;
  1766. if (isdir) {
  1767. if (!S_ISDIR(victim->d_inode->i_mode))
  1768. return -ENOTDIR;
  1769. if (IS_ROOT(victim))
  1770. return -EBUSY;
  1771. } else if (S_ISDIR(victim->d_inode->i_mode))
  1772. return -EISDIR;
  1773. if (IS_DEADDIR(dir))
  1774. return -ENOENT;
  1775. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1776. return -EBUSY;
  1777. return 0;
  1778. }
  1779. /* Check whether we can create an object with dentry child in directory
  1780. * dir.
  1781. * 1. We can't do it if child already exists (open has special treatment for
  1782. * this case, but since we are inlined it's OK)
  1783. * 2. We can't do it if dir is read-only (done in permission())
  1784. * 3. We should have write and exec permissions on dir
  1785. * 4. We can't do it if dir is immutable (done in permission())
  1786. */
  1787. static inline int may_create(struct inode *dir, struct dentry *child)
  1788. {
  1789. if (child->d_inode)
  1790. return -EEXIST;
  1791. if (IS_DEADDIR(dir))
  1792. return -ENOENT;
  1793. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1794. }
  1795. /*
  1796. * p1 and p2 should be directories on the same fs.
  1797. */
  1798. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1799. {
  1800. struct dentry *p;
  1801. if (p1 == p2) {
  1802. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1803. return NULL;
  1804. }
  1805. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1806. p = d_ancestor(p2, p1);
  1807. if (p) {
  1808. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1809. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1810. return p;
  1811. }
  1812. p = d_ancestor(p1, p2);
  1813. if (p) {
  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 p;
  1817. }
  1818. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1819. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1820. return NULL;
  1821. }
  1822. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1823. {
  1824. mutex_unlock(&p1->d_inode->i_mutex);
  1825. if (p1 != p2) {
  1826. mutex_unlock(&p2->d_inode->i_mutex);
  1827. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1828. }
  1829. }
  1830. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1831. struct nameidata *nd)
  1832. {
  1833. int error = may_create(dir, dentry);
  1834. if (error)
  1835. return error;
  1836. if (!dir->i_op->create)
  1837. return -EACCES; /* shouldn't it be ENOSYS? */
  1838. mode &= S_IALLUGO;
  1839. mode |= S_IFREG;
  1840. error = security_inode_create(dir, dentry, mode);
  1841. if (error)
  1842. return error;
  1843. error = dir->i_op->create(dir, dentry, mode, nd);
  1844. if (!error)
  1845. fsnotify_create(dir, dentry);
  1846. return error;
  1847. }
  1848. int may_open(struct path *path, int acc_mode, int flag)
  1849. {
  1850. struct dentry *dentry = path->dentry;
  1851. struct inode *inode = dentry->d_inode;
  1852. int error;
  1853. if (!inode)
  1854. return -ENOENT;
  1855. switch (inode->i_mode & S_IFMT) {
  1856. case S_IFLNK:
  1857. return -ELOOP;
  1858. case S_IFDIR:
  1859. if (acc_mode & MAY_WRITE)
  1860. return -EISDIR;
  1861. break;
  1862. case S_IFBLK:
  1863. case S_IFCHR:
  1864. if (path->mnt->mnt_flags & MNT_NODEV)
  1865. return -EACCES;
  1866. /*FALLTHRU*/
  1867. case S_IFIFO:
  1868. case S_IFSOCK:
  1869. flag &= ~O_TRUNC;
  1870. break;
  1871. }
  1872. error = inode_permission(inode, acc_mode);
  1873. if (error)
  1874. return error;
  1875. /*
  1876. * An append-only file must be opened in append mode for writing.
  1877. */
  1878. if (IS_APPEND(inode)) {
  1879. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1880. return -EPERM;
  1881. if (flag & O_TRUNC)
  1882. return -EPERM;
  1883. }
  1884. /* O_NOATIME can only be set by the owner or superuser */
  1885. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1886. return -EPERM;
  1887. /*
  1888. * Ensure there are no outstanding leases on the file.
  1889. */
  1890. return break_lease(inode, flag);
  1891. }
  1892. static int handle_truncate(struct file *filp)
  1893. {
  1894. struct path *path = &filp->f_path;
  1895. struct inode *inode = path->dentry->d_inode;
  1896. int error = get_write_access(inode);
  1897. if (error)
  1898. return error;
  1899. /*
  1900. * Refuse to truncate files with mandatory locks held on them.
  1901. */
  1902. error = locks_verify_locked(inode);
  1903. if (!error)
  1904. error = security_path_truncate(path);
  1905. if (!error) {
  1906. error = do_truncate(path->dentry, 0,
  1907. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1908. filp);
  1909. }
  1910. put_write_access(inode);
  1911. return error;
  1912. }
  1913. /*
  1914. * Be careful about ever adding any more callers of this
  1915. * function. Its flags must be in the namei format, not
  1916. * what get passed to sys_open().
  1917. */
  1918. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1919. int open_flag, int mode)
  1920. {
  1921. int error;
  1922. struct dentry *dir = nd->path.dentry;
  1923. if (!IS_POSIXACL(dir->d_inode))
  1924. mode &= ~current_umask();
  1925. error = security_path_mknod(&nd->path, path->dentry, mode, 0);
  1926. if (error)
  1927. goto out_unlock;
  1928. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1929. out_unlock:
  1930. mutex_unlock(&dir->d_inode->i_mutex);
  1931. dput(nd->path.dentry);
  1932. nd->path.dentry = path->dentry;
  1933. if (error)
  1934. return error;
  1935. /* Don't check for write permission, don't truncate */
  1936. return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
  1937. }
  1938. /*
  1939. * Note that while the flag value (low two bits) for sys_open means:
  1940. * 00 - read-only
  1941. * 01 - write-only
  1942. * 10 - read-write
  1943. * 11 - special
  1944. * it is changed into
  1945. * 00 - no permissions needed
  1946. * 01 - read-permission
  1947. * 10 - write-permission
  1948. * 11 - read-write
  1949. * for the internal routines (ie open_namei()/follow_link() etc)
  1950. * This is more logical, and also allows the 00 "no perm needed"
  1951. * to be used for symlinks (where the permissions are checked
  1952. * later).
  1953. *
  1954. */
  1955. static inline int open_to_namei_flags(int flag)
  1956. {
  1957. if ((flag+1) & O_ACCMODE)
  1958. flag++;
  1959. return flag;
  1960. }
  1961. static int open_will_truncate(int flag, struct inode *inode)
  1962. {
  1963. /*
  1964. * We'll never write to the fs underlying
  1965. * a device file.
  1966. */
  1967. if (special_file(inode->i_mode))
  1968. return 0;
  1969. return (flag & O_TRUNC);
  1970. }
  1971. static struct file *finish_open(struct nameidata *nd,
  1972. int open_flag, int acc_mode)
  1973. {
  1974. struct file *filp;
  1975. int will_truncate;
  1976. int error;
  1977. will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
  1978. if (will_truncate) {
  1979. error = mnt_want_write(nd->path.mnt);
  1980. if (error)
  1981. goto exit;
  1982. }
  1983. error = may_open(&nd->path, acc_mode, open_flag);
  1984. if (error) {
  1985. if (will_truncate)
  1986. mnt_drop_write(nd->path.mnt);
  1987. goto exit;
  1988. }
  1989. filp = nameidata_to_filp(nd);
  1990. if (!IS_ERR(filp)) {
  1991. error = ima_file_check(filp, acc_mode);
  1992. if (error) {
  1993. fput(filp);
  1994. filp = ERR_PTR(error);
  1995. }
  1996. }
  1997. if (!IS_ERR(filp)) {
  1998. if (will_truncate) {
  1999. error = handle_truncate(filp);
  2000. if (error) {
  2001. fput(filp);
  2002. filp = ERR_PTR(error);
  2003. }
  2004. }
  2005. }
  2006. /*
  2007. * It is now safe to drop the mnt write
  2008. * because the filp has had a write taken
  2009. * on its behalf.
  2010. */
  2011. if (will_truncate)
  2012. mnt_drop_write(nd->path.mnt);
  2013. path_put(&nd->path);
  2014. return filp;
  2015. exit:
  2016. path_put(&nd->path);
  2017. return ERR_PTR(error);
  2018. }
  2019. /*
  2020. * Handle O_CREAT case for do_filp_open
  2021. */
  2022. static struct file *do_last(struct nameidata *nd, struct path *path,
  2023. int open_flag, int acc_mode,
  2024. int mode, const char *pathname)
  2025. {
  2026. struct dentry *dir = nd->path.dentry;
  2027. struct file *filp;
  2028. int error = -EISDIR;
  2029. switch (nd->last_type) {
  2030. case LAST_DOTDOT:
  2031. follow_dotdot(nd);
  2032. dir = nd->path.dentry;
  2033. case LAST_DOT:
  2034. if (need_reval_dot(dir)) {
  2035. int status = d_revalidate(nd->path.dentry, nd);
  2036. if (!status)
  2037. status = -ESTALE;
  2038. if (status < 0) {
  2039. error = status;
  2040. goto exit;
  2041. }
  2042. }
  2043. /* fallthrough */
  2044. case LAST_ROOT:
  2045. goto exit;
  2046. case LAST_BIND:
  2047. audit_inode(pathname, dir);
  2048. goto ok;
  2049. }
  2050. /* trailing slashes? */
  2051. if (nd->last.name[nd->last.len])
  2052. goto exit;
  2053. mutex_lock(&dir->d_inode->i_mutex);
  2054. path->dentry = lookup_hash(nd);
  2055. path->mnt = nd->path.mnt;
  2056. error = PTR_ERR(path->dentry);
  2057. if (IS_ERR(path->dentry)) {
  2058. mutex_unlock(&dir->d_inode->i_mutex);
  2059. goto exit;
  2060. }
  2061. if (IS_ERR(nd->intent.open.file)) {
  2062. error = PTR_ERR(nd->intent.open.file);
  2063. goto exit_mutex_unlock;
  2064. }
  2065. /* Negative dentry, just create the file */
  2066. if (!path->dentry->d_inode) {
  2067. /*
  2068. * This write is needed to ensure that a
  2069. * ro->rw transition does not occur between
  2070. * the time when the file is created and when
  2071. * a permanent write count is taken through
  2072. * the 'struct file' in nameidata_to_filp().
  2073. */
  2074. error = mnt_want_write(nd->path.mnt);
  2075. if (error)
  2076. goto exit_mutex_unlock;
  2077. error = __open_namei_create(nd, path, open_flag, mode);
  2078. if (error) {
  2079. mnt_drop_write(nd->path.mnt);
  2080. goto exit;
  2081. }
  2082. filp = nameidata_to_filp(nd);
  2083. mnt_drop_write(nd->path.mnt);
  2084. path_put(&nd->path);
  2085. if (!IS_ERR(filp)) {
  2086. error = ima_file_check(filp, acc_mode);
  2087. if (error) {
  2088. fput(filp);
  2089. filp = ERR_PTR(error);
  2090. }
  2091. }
  2092. return filp;
  2093. }
  2094. /*
  2095. * It already exists.
  2096. */
  2097. mutex_unlock(&dir->d_inode->i_mutex);
  2098. audit_inode(pathname, path->dentry);
  2099. error = -EEXIST;
  2100. if (open_flag & O_EXCL)
  2101. goto exit_dput;
  2102. error = follow_managed(path, nd->flags);
  2103. if (error < 0)
  2104. goto exit_dput;
  2105. error = -ENOENT;
  2106. if (!path->dentry->d_inode)
  2107. goto exit_dput;
  2108. if (path->dentry->d_inode->i_op->follow_link)
  2109. return NULL;
  2110. path_to_nameidata(path, nd);
  2111. nd->inode = path->dentry->d_inode;
  2112. error = -EISDIR;
  2113. if (S_ISDIR(nd->inode->i_mode))
  2114. goto exit;
  2115. ok:
  2116. filp = finish_open(nd, open_flag, acc_mode);
  2117. return filp;
  2118. exit_mutex_unlock:
  2119. mutex_unlock(&dir->d_inode->i_mutex);
  2120. exit_dput:
  2121. path_put_conditional(path, nd);
  2122. exit:
  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. release_open_intent(&nd);
  2198. return filp;
  2199. creat:
  2200. /* OK, have to create the file. Find the parent. */
  2201. error = path_init_rcu(dfd, pathname,
  2202. LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
  2203. if (error)
  2204. goto out_filp;
  2205. error = path_walk_rcu(pathname, &nd);
  2206. path_finish_rcu(&nd);
  2207. if (unlikely(error == -ECHILD || error == -ESTALE)) {
  2208. /* slower, locked walk */
  2209. if (error == -ESTALE) {
  2210. reval:
  2211. flags |= LOOKUP_REVAL;
  2212. }
  2213. error = path_init(dfd, pathname,
  2214. LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
  2215. if (error)
  2216. goto out_filp;
  2217. error = path_walk_simple(pathname, &nd);
  2218. }
  2219. if (unlikely(error))
  2220. goto out_filp;
  2221. if (unlikely(!audit_dummy_context()))
  2222. audit_inode(pathname, nd.path.dentry);
  2223. /*
  2224. * We have the parent and last component.
  2225. */
  2226. nd.flags = flags;
  2227. filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
  2228. while (unlikely(!filp)) { /* trailing symlink */
  2229. struct path link = path;
  2230. struct inode *linki = link.dentry->d_inode;
  2231. void *cookie;
  2232. error = -ELOOP;
  2233. if (!(nd.flags & LOOKUP_FOLLOW))
  2234. goto exit_dput;
  2235. if (count++ == 32)
  2236. goto exit_dput;
  2237. /*
  2238. * This is subtle. Instead of calling do_follow_link() we do
  2239. * the thing by hands. The reason is that this way we have zero
  2240. * link_count and path_walk() (called from ->follow_link)
  2241. * honoring LOOKUP_PARENT. After that we have the parent and
  2242. * last component, i.e. we are in the same situation as after
  2243. * the first path_walk(). Well, almost - if the last component
  2244. * is normal we get its copy stored in nd->last.name and we will
  2245. * have to putname() it when we are done. Procfs-like symlinks
  2246. * just set LAST_BIND.
  2247. */
  2248. nd.flags |= LOOKUP_PARENT;
  2249. error = security_inode_follow_link(link.dentry, &nd);
  2250. if (error)
  2251. goto exit_dput;
  2252. error = __do_follow_link(&link, &nd, &cookie);
  2253. if (unlikely(error)) {
  2254. if (!IS_ERR(cookie) && linki->i_op->put_link)
  2255. linki->i_op->put_link(link.dentry, &nd, cookie);
  2256. /* nd.path had been dropped */
  2257. nd.path = link;
  2258. goto out_path;
  2259. }
  2260. nd.flags &= ~LOOKUP_PARENT;
  2261. filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
  2262. if (linki->i_op->put_link)
  2263. linki->i_op->put_link(link.dentry, &nd, cookie);
  2264. path_put(&link);
  2265. }
  2266. out:
  2267. if (nd.root.mnt)
  2268. path_put(&nd.root);
  2269. if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
  2270. goto reval;
  2271. release_open_intent(&nd);
  2272. return filp;
  2273. exit_dput:
  2274. path_put_conditional(&path, &nd);
  2275. out_path:
  2276. path_put(&nd.path);
  2277. out_filp:
  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);