namei.c 84 KB

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