namei.c 84 KB

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