namei.c 86 KB

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