namei.c 85 KB

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