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