namei.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  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/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.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 <asm/uaccess.h>
  32. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  33. /* [Feb-1997 T. Schoebel-Theuer]
  34. * Fundamental changes in the pathname lookup mechanisms (namei)
  35. * were necessary because of omirr. The reason is that omirr needs
  36. * to know the _real_ pathname, not the user-supplied one, in case
  37. * of symlinks (and also when transname replacements occur).
  38. *
  39. * The new code replaces the old recursive symlink resolution with
  40. * an iterative one (in case of non-nested symlink chains). It does
  41. * this with calls to <fs>_follow_link().
  42. * As a side effect, dir_namei(), _namei() and follow_link() are now
  43. * replaced with a single function lookup_dentry() that can handle all
  44. * the special cases of the former code.
  45. *
  46. * With the new dcache, the pathname is stored at each inode, at least as
  47. * long as the refcount of the inode is positive. As a side effect, the
  48. * size of the dcache depends on the inode cache and thus is dynamic.
  49. *
  50. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  51. * resolution to correspond with current state of the code.
  52. *
  53. * Note that the symlink resolution is not *completely* iterative.
  54. * There is still a significant amount of tail- and mid- recursion in
  55. * the algorithm. Also, note that <fs>_readlink() is not used in
  56. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  57. * may return different results than <fs>_follow_link(). Many virtual
  58. * filesystems (including /proc) exhibit this behavior.
  59. */
  60. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  61. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  62. * and the name already exists in form of a symlink, try to create the new
  63. * name indicated by the symlink. The old code always complained that the
  64. * name already exists, due to not following the symlink even if its target
  65. * is nonexistent. The new semantics affects also mknod() and link() when
  66. * the name is a symlink pointing to a non-existant name.
  67. *
  68. * I don't know which semantics is the right one, since I have no access
  69. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  70. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  71. * "old" one. Personally, I think the new semantics is much more logical.
  72. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  73. * file does succeed in both HP-UX and SunOs, but not in Solaris
  74. * and in the old Linux semantics.
  75. */
  76. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  77. * semantics. See the comments in "open_namei" and "do_link" below.
  78. *
  79. * [10-Sep-98 Alan Modra] Another symlink change.
  80. */
  81. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  82. * inside the path - always follow.
  83. * in the last component in creation/removal/renaming - never follow.
  84. * if LOOKUP_FOLLOW passed - follow.
  85. * if the pathname has trailing slashes - follow.
  86. * otherwise - don't follow.
  87. * (applied in that order).
  88. *
  89. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  90. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  91. * During the 2.4 we need to fix the userland stuff depending on it -
  92. * hopefully we will be able to get rid of that wart in 2.5. So far only
  93. * XEmacs seems to be relying on it...
  94. */
  95. /*
  96. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  97. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  98. * any extra contention...
  99. */
  100. static int __link_path_walk(const char *name, struct nameidata *nd);
  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. * generic_permission - check for access rights on a Posix-like filesystem
  155. * @inode: inode to check access rights for
  156. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  157. * @check_acl: optional callback to check for Posix ACLs
  158. *
  159. * Used to check for read/write/execute permissions on a file.
  160. * We use "fsuid" for this, letting us set arbitrary permissions
  161. * for filesystem access without changing the "normal" uids which
  162. * are used for other things..
  163. */
  164. int generic_permission(struct inode *inode, int mask,
  165. int (*check_acl)(struct inode *inode, int mask))
  166. {
  167. umode_t mode = inode->i_mode;
  168. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  169. if (current_fsuid() == inode->i_uid)
  170. mode >>= 6;
  171. else {
  172. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  173. int error = check_acl(inode, mask);
  174. if (error == -EACCES)
  175. goto check_capabilities;
  176. else if (error != -EAGAIN)
  177. return error;
  178. }
  179. if (in_group_p(inode->i_gid))
  180. mode >>= 3;
  181. }
  182. /*
  183. * If the DACs are ok we don't need any capability check.
  184. */
  185. if ((mask & ~mode) == 0)
  186. return 0;
  187. check_capabilities:
  188. /*
  189. * Read/write DACs are always overridable.
  190. * Executable DACs are overridable if at least one exec bit is set.
  191. */
  192. if (!(mask & MAY_EXEC) || execute_ok(inode))
  193. if (capable(CAP_DAC_OVERRIDE))
  194. return 0;
  195. /*
  196. * Searching includes executable on directories, else just read.
  197. */
  198. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  199. if (capable(CAP_DAC_READ_SEARCH))
  200. return 0;
  201. return -EACCES;
  202. }
  203. /**
  204. * inode_permission - check for access rights to a given inode
  205. * @inode: inode to check permission on
  206. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  207. *
  208. * Used to check for read/write/execute permissions on an inode.
  209. * We use "fsuid" for this, letting us set arbitrary permissions
  210. * for filesystem access without changing the "normal" uids which
  211. * are used for other things.
  212. */
  213. int inode_permission(struct inode *inode, int mask)
  214. {
  215. int retval;
  216. if (mask & MAY_WRITE) {
  217. umode_t mode = inode->i_mode;
  218. /*
  219. * Nobody gets write access to a read-only fs.
  220. */
  221. if (IS_RDONLY(inode) &&
  222. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  223. return -EROFS;
  224. /*
  225. * Nobody gets write access to an immutable file.
  226. */
  227. if (IS_IMMUTABLE(inode))
  228. return -EACCES;
  229. }
  230. if (inode->i_op && inode->i_op->permission)
  231. retval = inode->i_op->permission(inode, mask);
  232. else
  233. retval = generic_permission(inode, mask, NULL);
  234. if (retval)
  235. return retval;
  236. retval = devcgroup_inode_permission(inode, mask);
  237. if (retval)
  238. return retval;
  239. return security_inode_permission(inode,
  240. mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
  241. }
  242. /**
  243. * file_permission - check for additional access rights to a given file
  244. * @file: file to check access rights for
  245. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  246. *
  247. * Used to check for read/write/execute permissions on an already opened
  248. * file.
  249. *
  250. * Note:
  251. * Do not use this function in new code. All access checks should
  252. * be done using inode_permission().
  253. */
  254. int file_permission(struct file *file, int mask)
  255. {
  256. return inode_permission(file->f_path.dentry->d_inode, mask);
  257. }
  258. /*
  259. * get_write_access() gets write permission for a file.
  260. * put_write_access() releases this write permission.
  261. * This is used for regular files.
  262. * We cannot support write (and maybe mmap read-write shared) accesses and
  263. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  264. * can have the following values:
  265. * 0: no writers, no VM_DENYWRITE mappings
  266. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  267. * > 0: (i_writecount) users are writing to the file.
  268. *
  269. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  270. * except for the cases where we don't hold i_writecount yet. Then we need to
  271. * use {get,deny}_write_access() - these functions check the sign and refuse
  272. * to do the change if sign is wrong. Exclusion between them is provided by
  273. * the inode->i_lock spinlock.
  274. */
  275. int get_write_access(struct inode * inode)
  276. {
  277. spin_lock(&inode->i_lock);
  278. if (atomic_read(&inode->i_writecount) < 0) {
  279. spin_unlock(&inode->i_lock);
  280. return -ETXTBSY;
  281. }
  282. atomic_inc(&inode->i_writecount);
  283. spin_unlock(&inode->i_lock);
  284. return 0;
  285. }
  286. int deny_write_access(struct file * file)
  287. {
  288. struct inode *inode = file->f_path.dentry->d_inode;
  289. spin_lock(&inode->i_lock);
  290. if (atomic_read(&inode->i_writecount) > 0) {
  291. spin_unlock(&inode->i_lock);
  292. return -ETXTBSY;
  293. }
  294. atomic_dec(&inode->i_writecount);
  295. spin_unlock(&inode->i_lock);
  296. return 0;
  297. }
  298. /**
  299. * path_get - get a reference to a path
  300. * @path: path to get the reference to
  301. *
  302. * Given a path increment the reference count to the dentry and the vfsmount.
  303. */
  304. void path_get(struct path *path)
  305. {
  306. mntget(path->mnt);
  307. dget(path->dentry);
  308. }
  309. EXPORT_SYMBOL(path_get);
  310. /**
  311. * path_put - put a reference to a path
  312. * @path: path to put the reference to
  313. *
  314. * Given a path decrement the reference count to the dentry and the vfsmount.
  315. */
  316. void path_put(struct path *path)
  317. {
  318. dput(path->dentry);
  319. mntput(path->mnt);
  320. }
  321. EXPORT_SYMBOL(path_put);
  322. /**
  323. * release_open_intent - free up open intent resources
  324. * @nd: pointer to nameidata
  325. */
  326. void release_open_intent(struct nameidata *nd)
  327. {
  328. if (nd->intent.open.file->f_path.dentry == NULL)
  329. put_filp(nd->intent.open.file);
  330. else
  331. fput(nd->intent.open.file);
  332. }
  333. static inline struct dentry *
  334. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  335. {
  336. int status = dentry->d_op->d_revalidate(dentry, nd);
  337. if (unlikely(status <= 0)) {
  338. /*
  339. * The dentry failed validation.
  340. * If d_revalidate returned 0 attempt to invalidate
  341. * the dentry otherwise d_revalidate is asking us
  342. * to return a fail status.
  343. */
  344. if (!status) {
  345. if (!d_invalidate(dentry)) {
  346. dput(dentry);
  347. dentry = NULL;
  348. }
  349. } else {
  350. dput(dentry);
  351. dentry = ERR_PTR(status);
  352. }
  353. }
  354. return dentry;
  355. }
  356. /*
  357. * Internal lookup() using the new generic dcache.
  358. * SMP-safe
  359. */
  360. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  361. {
  362. struct dentry * dentry = __d_lookup(parent, name);
  363. /* lockess __d_lookup may fail due to concurrent d_move()
  364. * in some unrelated directory, so try with d_lookup
  365. */
  366. if (!dentry)
  367. dentry = d_lookup(parent, name);
  368. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  369. dentry = do_revalidate(dentry, nd);
  370. return dentry;
  371. }
  372. /*
  373. * Short-cut version of permission(), for calling by
  374. * path_walk(), when dcache lock is held. Combines parts
  375. * of permission() and generic_permission(), and tests ONLY for
  376. * MAY_EXEC permission.
  377. *
  378. * If appropriate, check DAC only. If not appropriate, or
  379. * short-cut DAC fails, then call permission() to do more
  380. * complete permission check.
  381. */
  382. static int exec_permission_lite(struct inode *inode)
  383. {
  384. umode_t mode = inode->i_mode;
  385. if (inode->i_op && inode->i_op->permission)
  386. return -EAGAIN;
  387. if (current_fsuid() == inode->i_uid)
  388. mode >>= 6;
  389. else if (in_group_p(inode->i_gid))
  390. mode >>= 3;
  391. if (mode & MAY_EXEC)
  392. goto ok;
  393. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  394. goto ok;
  395. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  396. goto ok;
  397. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  398. goto ok;
  399. return -EACCES;
  400. ok:
  401. return security_inode_permission(inode, MAY_EXEC);
  402. }
  403. /*
  404. * This is called when everything else fails, and we actually have
  405. * to go to the low-level filesystem to find out what we should do..
  406. *
  407. * We get the directory semaphore, and after getting that we also
  408. * make sure that nobody added the entry to the dcache in the meantime..
  409. * SMP-safe
  410. */
  411. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  412. {
  413. struct dentry * result;
  414. struct inode *dir = parent->d_inode;
  415. mutex_lock(&dir->i_mutex);
  416. /*
  417. * First re-do the cached lookup just in case it was created
  418. * while we waited for the directory semaphore..
  419. *
  420. * FIXME! This could use version numbering or similar to
  421. * avoid unnecessary cache lookups.
  422. *
  423. * The "dcache_lock" is purely to protect the RCU list walker
  424. * from concurrent renames at this point (we mustn't get false
  425. * negatives from the RCU list walk here, unlike the optimistic
  426. * fast walk).
  427. *
  428. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  429. */
  430. result = d_lookup(parent, name);
  431. if (!result) {
  432. struct dentry *dentry;
  433. /* Don't create child dentry for a dead directory. */
  434. result = ERR_PTR(-ENOENT);
  435. if (IS_DEADDIR(dir))
  436. goto out_unlock;
  437. dentry = d_alloc(parent, name);
  438. result = ERR_PTR(-ENOMEM);
  439. if (dentry) {
  440. result = dir->i_op->lookup(dir, dentry, nd);
  441. if (result)
  442. dput(dentry);
  443. else
  444. result = dentry;
  445. }
  446. out_unlock:
  447. mutex_unlock(&dir->i_mutex);
  448. return result;
  449. }
  450. /*
  451. * Uhhuh! Nasty case: the cache was re-populated while
  452. * we waited on the semaphore. Need to revalidate.
  453. */
  454. mutex_unlock(&dir->i_mutex);
  455. if (result->d_op && result->d_op->d_revalidate) {
  456. result = do_revalidate(result, nd);
  457. if (!result)
  458. result = ERR_PTR(-ENOENT);
  459. }
  460. return result;
  461. }
  462. /*
  463. * Wrapper to retry pathname resolution whenever the underlying
  464. * file system returns an ESTALE.
  465. *
  466. * Retry the whole path once, forcing real lookup requests
  467. * instead of relying on the dcache.
  468. */
  469. static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
  470. {
  471. struct path save = nd->path;
  472. int result;
  473. /* make sure the stuff we saved doesn't go away */
  474. path_get(&save);
  475. result = __link_path_walk(name, nd);
  476. if (result == -ESTALE) {
  477. /* nd->path had been dropped */
  478. nd->path = save;
  479. path_get(&nd->path);
  480. nd->flags |= LOOKUP_REVAL;
  481. result = __link_path_walk(name, nd);
  482. }
  483. path_put(&save);
  484. return result;
  485. }
  486. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  487. {
  488. int res = 0;
  489. char *name;
  490. if (IS_ERR(link))
  491. goto fail;
  492. if (*link == '/') {
  493. struct fs_struct *fs = current->fs;
  494. path_put(&nd->path);
  495. read_lock(&fs->lock);
  496. nd->path = fs->root;
  497. path_get(&fs->root);
  498. read_unlock(&fs->lock);
  499. }
  500. res = link_path_walk(link, nd);
  501. if (nd->depth || res || nd->last_type!=LAST_NORM)
  502. return res;
  503. /*
  504. * If it is an iterative symlinks resolution in open_namei() we
  505. * have to copy the last component. And all that crap because of
  506. * bloody create() on broken symlinks. Furrfu...
  507. */
  508. name = __getname();
  509. if (unlikely(!name)) {
  510. path_put(&nd->path);
  511. return -ENOMEM;
  512. }
  513. strcpy(name, nd->last.name);
  514. nd->last.name = name;
  515. return 0;
  516. fail:
  517. path_put(&nd->path);
  518. return PTR_ERR(link);
  519. }
  520. static void path_put_conditional(struct path *path, struct nameidata *nd)
  521. {
  522. dput(path->dentry);
  523. if (path->mnt != nd->path.mnt)
  524. mntput(path->mnt);
  525. }
  526. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  527. {
  528. dput(nd->path.dentry);
  529. if (nd->path.mnt != path->mnt)
  530. mntput(nd->path.mnt);
  531. nd->path.mnt = path->mnt;
  532. nd->path.dentry = path->dentry;
  533. }
  534. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  535. {
  536. int error;
  537. void *cookie;
  538. struct dentry *dentry = path->dentry;
  539. touch_atime(path->mnt, dentry);
  540. nd_set_link(nd, NULL);
  541. if (path->mnt != nd->path.mnt) {
  542. path_to_nameidata(path, nd);
  543. dget(dentry);
  544. }
  545. mntget(path->mnt);
  546. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  547. error = PTR_ERR(cookie);
  548. if (!IS_ERR(cookie)) {
  549. char *s = nd_get_link(nd);
  550. error = 0;
  551. if (s)
  552. error = __vfs_follow_link(nd, s);
  553. if (dentry->d_inode->i_op->put_link)
  554. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  555. }
  556. path_put(path);
  557. return error;
  558. }
  559. /*
  560. * This limits recursive symlink follows to 8, while
  561. * limiting consecutive symlinks to 40.
  562. *
  563. * Without that kind of total limit, nasty chains of consecutive
  564. * symlinks can cause almost arbitrarily long lookups.
  565. */
  566. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  567. {
  568. int err = -ELOOP;
  569. if (current->link_count >= MAX_NESTED_LINKS)
  570. goto loop;
  571. if (current->total_link_count >= 40)
  572. goto loop;
  573. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  574. cond_resched();
  575. err = security_inode_follow_link(path->dentry, nd);
  576. if (err)
  577. goto loop;
  578. current->link_count++;
  579. current->total_link_count++;
  580. nd->depth++;
  581. err = __do_follow_link(path, nd);
  582. current->link_count--;
  583. nd->depth--;
  584. return err;
  585. loop:
  586. path_put_conditional(path, nd);
  587. path_put(&nd->path);
  588. return err;
  589. }
  590. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  591. {
  592. struct vfsmount *parent;
  593. struct dentry *mountpoint;
  594. spin_lock(&vfsmount_lock);
  595. parent=(*mnt)->mnt_parent;
  596. if (parent == *mnt) {
  597. spin_unlock(&vfsmount_lock);
  598. return 0;
  599. }
  600. mntget(parent);
  601. mountpoint=dget((*mnt)->mnt_mountpoint);
  602. spin_unlock(&vfsmount_lock);
  603. dput(*dentry);
  604. *dentry = mountpoint;
  605. mntput(*mnt);
  606. *mnt = parent;
  607. return 1;
  608. }
  609. /* no need for dcache_lock, as serialization is taken care in
  610. * namespace.c
  611. */
  612. static int __follow_mount(struct path *path)
  613. {
  614. int res = 0;
  615. while (d_mountpoint(path->dentry)) {
  616. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  617. if (!mounted)
  618. break;
  619. dput(path->dentry);
  620. if (res)
  621. mntput(path->mnt);
  622. path->mnt = mounted;
  623. path->dentry = dget(mounted->mnt_root);
  624. res = 1;
  625. }
  626. return res;
  627. }
  628. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  629. {
  630. while (d_mountpoint(*dentry)) {
  631. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  632. if (!mounted)
  633. break;
  634. dput(*dentry);
  635. mntput(*mnt);
  636. *mnt = mounted;
  637. *dentry = dget(mounted->mnt_root);
  638. }
  639. }
  640. /* no need for dcache_lock, as serialization is taken care in
  641. * namespace.c
  642. */
  643. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  644. {
  645. struct vfsmount *mounted;
  646. mounted = lookup_mnt(*mnt, *dentry);
  647. if (mounted) {
  648. dput(*dentry);
  649. mntput(*mnt);
  650. *mnt = mounted;
  651. *dentry = dget(mounted->mnt_root);
  652. return 1;
  653. }
  654. return 0;
  655. }
  656. static __always_inline void follow_dotdot(struct nameidata *nd)
  657. {
  658. struct fs_struct *fs = current->fs;
  659. while(1) {
  660. struct vfsmount *parent;
  661. struct dentry *old = nd->path.dentry;
  662. read_lock(&fs->lock);
  663. if (nd->path.dentry == fs->root.dentry &&
  664. nd->path.mnt == fs->root.mnt) {
  665. read_unlock(&fs->lock);
  666. break;
  667. }
  668. read_unlock(&fs->lock);
  669. spin_lock(&dcache_lock);
  670. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  671. nd->path.dentry = dget(nd->path.dentry->d_parent);
  672. spin_unlock(&dcache_lock);
  673. dput(old);
  674. break;
  675. }
  676. spin_unlock(&dcache_lock);
  677. spin_lock(&vfsmount_lock);
  678. parent = nd->path.mnt->mnt_parent;
  679. if (parent == nd->path.mnt) {
  680. spin_unlock(&vfsmount_lock);
  681. break;
  682. }
  683. mntget(parent);
  684. nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
  685. spin_unlock(&vfsmount_lock);
  686. dput(old);
  687. mntput(nd->path.mnt);
  688. nd->path.mnt = parent;
  689. }
  690. follow_mount(&nd->path.mnt, &nd->path.dentry);
  691. }
  692. /*
  693. * It's more convoluted than I'd like it to be, but... it's still fairly
  694. * small and for now I'd prefer to have fast path as straight as possible.
  695. * It _is_ time-critical.
  696. */
  697. static int do_lookup(struct nameidata *nd, struct qstr *name,
  698. struct path *path)
  699. {
  700. struct vfsmount *mnt = nd->path.mnt;
  701. struct dentry *dentry = __d_lookup(nd->path.dentry, name);
  702. if (!dentry)
  703. goto need_lookup;
  704. if (dentry->d_op && dentry->d_op->d_revalidate)
  705. goto need_revalidate;
  706. done:
  707. path->mnt = mnt;
  708. path->dentry = dentry;
  709. __follow_mount(path);
  710. return 0;
  711. need_lookup:
  712. dentry = real_lookup(nd->path.dentry, name, nd);
  713. if (IS_ERR(dentry))
  714. goto fail;
  715. goto done;
  716. need_revalidate:
  717. dentry = do_revalidate(dentry, nd);
  718. if (!dentry)
  719. goto need_lookup;
  720. if (IS_ERR(dentry))
  721. goto fail;
  722. goto done;
  723. fail:
  724. return PTR_ERR(dentry);
  725. }
  726. /*
  727. * Name resolution.
  728. * This is the basic name resolution function, turning a pathname into
  729. * the final dentry. We expect 'base' to be positive and a directory.
  730. *
  731. * Returns 0 and nd will have valid dentry and mnt on success.
  732. * Returns error and drops reference to input namei data on failure.
  733. */
  734. static int __link_path_walk(const char *name, struct nameidata *nd)
  735. {
  736. struct path next;
  737. struct inode *inode;
  738. int err;
  739. unsigned int lookup_flags = nd->flags;
  740. while (*name=='/')
  741. name++;
  742. if (!*name)
  743. goto return_reval;
  744. inode = nd->path.dentry->d_inode;
  745. if (nd->depth)
  746. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  747. /* At this point we know we have a real path component. */
  748. for(;;) {
  749. unsigned long hash;
  750. struct qstr this;
  751. unsigned int c;
  752. nd->flags |= LOOKUP_CONTINUE;
  753. err = exec_permission_lite(inode);
  754. if (err == -EAGAIN)
  755. err = inode_permission(nd->path.dentry->d_inode,
  756. MAY_EXEC);
  757. if (err)
  758. break;
  759. this.name = name;
  760. c = *(const unsigned char *)name;
  761. hash = init_name_hash();
  762. do {
  763. name++;
  764. hash = partial_name_hash(c, hash);
  765. c = *(const unsigned char *)name;
  766. } while (c && (c != '/'));
  767. this.len = name - (const char *) this.name;
  768. this.hash = end_name_hash(hash);
  769. /* remove trailing slashes? */
  770. if (!c)
  771. goto last_component;
  772. while (*++name == '/');
  773. if (!*name)
  774. goto last_with_slashes;
  775. /*
  776. * "." and ".." are special - ".." especially so because it has
  777. * to be able to know about the current root directory and
  778. * parent relationships.
  779. */
  780. if (this.name[0] == '.') switch (this.len) {
  781. default:
  782. break;
  783. case 2:
  784. if (this.name[1] != '.')
  785. break;
  786. follow_dotdot(nd);
  787. inode = nd->path.dentry->d_inode;
  788. /* fallthrough */
  789. case 1:
  790. continue;
  791. }
  792. /*
  793. * See if the low-level filesystem might want
  794. * to use its own hash..
  795. */
  796. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  797. err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
  798. &this);
  799. if (err < 0)
  800. break;
  801. }
  802. /* This does the actual lookups.. */
  803. err = do_lookup(nd, &this, &next);
  804. if (err)
  805. break;
  806. err = -ENOENT;
  807. inode = next.dentry->d_inode;
  808. if (!inode)
  809. goto out_dput;
  810. err = -ENOTDIR;
  811. if (!inode->i_op)
  812. goto out_dput;
  813. if (inode->i_op->follow_link) {
  814. err = do_follow_link(&next, nd);
  815. if (err)
  816. goto return_err;
  817. err = -ENOENT;
  818. inode = nd->path.dentry->d_inode;
  819. if (!inode)
  820. break;
  821. err = -ENOTDIR;
  822. if (!inode->i_op)
  823. break;
  824. } else
  825. path_to_nameidata(&next, nd);
  826. err = -ENOTDIR;
  827. if (!inode->i_op->lookup)
  828. break;
  829. continue;
  830. /* here ends the main loop */
  831. last_with_slashes:
  832. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  833. last_component:
  834. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  835. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  836. if (lookup_flags & LOOKUP_PARENT)
  837. goto lookup_parent;
  838. if (this.name[0] == '.') switch (this.len) {
  839. default:
  840. break;
  841. case 2:
  842. if (this.name[1] != '.')
  843. break;
  844. follow_dotdot(nd);
  845. inode = nd->path.dentry->d_inode;
  846. /* fallthrough */
  847. case 1:
  848. goto return_reval;
  849. }
  850. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  851. err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
  852. &this);
  853. if (err < 0)
  854. break;
  855. }
  856. err = do_lookup(nd, &this, &next);
  857. if (err)
  858. break;
  859. inode = next.dentry->d_inode;
  860. if ((lookup_flags & LOOKUP_FOLLOW)
  861. && inode && inode->i_op && inode->i_op->follow_link) {
  862. err = do_follow_link(&next, nd);
  863. if (err)
  864. goto return_err;
  865. inode = nd->path.dentry->d_inode;
  866. } else
  867. path_to_nameidata(&next, nd);
  868. err = -ENOENT;
  869. if (!inode)
  870. break;
  871. if (lookup_flags & LOOKUP_DIRECTORY) {
  872. err = -ENOTDIR;
  873. if (!inode->i_op || !inode->i_op->lookup)
  874. break;
  875. }
  876. goto return_base;
  877. lookup_parent:
  878. nd->last = this;
  879. nd->last_type = LAST_NORM;
  880. if (this.name[0] != '.')
  881. goto return_base;
  882. if (this.len == 1)
  883. nd->last_type = LAST_DOT;
  884. else if (this.len == 2 && this.name[1] == '.')
  885. nd->last_type = LAST_DOTDOT;
  886. else
  887. goto return_base;
  888. return_reval:
  889. /*
  890. * We bypassed the ordinary revalidation routines.
  891. * We may need to check the cached dentry for staleness.
  892. */
  893. if (nd->path.dentry && nd->path.dentry->d_sb &&
  894. (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  895. err = -ESTALE;
  896. /* Note: we do not d_invalidate() */
  897. if (!nd->path.dentry->d_op->d_revalidate(
  898. nd->path.dentry, nd))
  899. break;
  900. }
  901. return_base:
  902. return 0;
  903. out_dput:
  904. path_put_conditional(&next, nd);
  905. break;
  906. }
  907. path_put(&nd->path);
  908. return_err:
  909. return err;
  910. }
  911. static int path_walk(const char *name, struct nameidata *nd)
  912. {
  913. current->total_link_count = 0;
  914. return link_path_walk(name, nd);
  915. }
  916. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  917. static int do_path_lookup(int dfd, const char *name,
  918. unsigned int flags, struct nameidata *nd)
  919. {
  920. int retval = 0;
  921. int fput_needed;
  922. struct file *file;
  923. struct fs_struct *fs = current->fs;
  924. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  925. nd->flags = flags;
  926. nd->depth = 0;
  927. if (*name=='/') {
  928. read_lock(&fs->lock);
  929. nd->path = fs->root;
  930. path_get(&fs->root);
  931. read_unlock(&fs->lock);
  932. } else if (dfd == AT_FDCWD) {
  933. read_lock(&fs->lock);
  934. nd->path = fs->pwd;
  935. path_get(&fs->pwd);
  936. read_unlock(&fs->lock);
  937. } else {
  938. struct dentry *dentry;
  939. file = fget_light(dfd, &fput_needed);
  940. retval = -EBADF;
  941. if (!file)
  942. goto out_fail;
  943. dentry = file->f_path.dentry;
  944. retval = -ENOTDIR;
  945. if (!S_ISDIR(dentry->d_inode->i_mode))
  946. goto fput_fail;
  947. retval = file_permission(file, MAY_EXEC);
  948. if (retval)
  949. goto fput_fail;
  950. nd->path = file->f_path;
  951. path_get(&file->f_path);
  952. fput_light(file, fput_needed);
  953. }
  954. retval = path_walk(name, nd);
  955. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  956. nd->path.dentry->d_inode))
  957. audit_inode(name, nd->path.dentry);
  958. out_fail:
  959. return retval;
  960. fput_fail:
  961. fput_light(file, fput_needed);
  962. goto out_fail;
  963. }
  964. int path_lookup(const char *name, unsigned int flags,
  965. struct nameidata *nd)
  966. {
  967. return do_path_lookup(AT_FDCWD, name, flags, nd);
  968. }
  969. int kern_path(const char *name, unsigned int flags, struct path *path)
  970. {
  971. struct nameidata nd;
  972. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  973. if (!res)
  974. *path = nd.path;
  975. return res;
  976. }
  977. /**
  978. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  979. * @dentry: pointer to dentry of the base directory
  980. * @mnt: pointer to vfs mount of the base directory
  981. * @name: pointer to file name
  982. * @flags: lookup flags
  983. * @nd: pointer to nameidata
  984. */
  985. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  986. const char *name, unsigned int flags,
  987. struct nameidata *nd)
  988. {
  989. int retval;
  990. /* same as do_path_lookup */
  991. nd->last_type = LAST_ROOT;
  992. nd->flags = flags;
  993. nd->depth = 0;
  994. nd->path.dentry = dentry;
  995. nd->path.mnt = mnt;
  996. path_get(&nd->path);
  997. retval = path_walk(name, nd);
  998. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  999. nd->path.dentry->d_inode))
  1000. audit_inode(name, nd->path.dentry);
  1001. return retval;
  1002. }
  1003. /**
  1004. * path_lookup_open - lookup a file path with open intent
  1005. * @dfd: the directory to use as base, or AT_FDCWD
  1006. * @name: pointer to file name
  1007. * @lookup_flags: lookup intent flags
  1008. * @nd: pointer to nameidata
  1009. * @open_flags: open intent flags
  1010. */
  1011. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1012. struct nameidata *nd, int open_flags)
  1013. {
  1014. struct file *filp = get_empty_filp();
  1015. int err;
  1016. if (filp == NULL)
  1017. return -ENFILE;
  1018. nd->intent.open.file = filp;
  1019. nd->intent.open.flags = open_flags;
  1020. nd->intent.open.create_mode = 0;
  1021. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1022. if (IS_ERR(nd->intent.open.file)) {
  1023. if (err == 0) {
  1024. err = PTR_ERR(nd->intent.open.file);
  1025. path_put(&nd->path);
  1026. }
  1027. } else if (err != 0)
  1028. release_open_intent(nd);
  1029. return err;
  1030. }
  1031. static struct dentry *__lookup_hash(struct qstr *name,
  1032. struct dentry *base, struct nameidata *nd)
  1033. {
  1034. struct dentry *dentry;
  1035. struct inode *inode;
  1036. int err;
  1037. inode = base->d_inode;
  1038. /*
  1039. * See if the low-level filesystem might want
  1040. * to use its own hash..
  1041. */
  1042. if (base->d_op && base->d_op->d_hash) {
  1043. err = base->d_op->d_hash(base, name);
  1044. dentry = ERR_PTR(err);
  1045. if (err < 0)
  1046. goto out;
  1047. }
  1048. dentry = cached_lookup(base, name, nd);
  1049. if (!dentry) {
  1050. struct dentry *new;
  1051. /* Don't create child dentry for a dead directory. */
  1052. dentry = ERR_PTR(-ENOENT);
  1053. if (IS_DEADDIR(inode))
  1054. goto out;
  1055. new = d_alloc(base, name);
  1056. dentry = ERR_PTR(-ENOMEM);
  1057. if (!new)
  1058. goto out;
  1059. dentry = inode->i_op->lookup(inode, new, nd);
  1060. if (!dentry)
  1061. dentry = new;
  1062. else
  1063. dput(new);
  1064. }
  1065. out:
  1066. return dentry;
  1067. }
  1068. /*
  1069. * Restricted form of lookup. Doesn't follow links, single-component only,
  1070. * needs parent already locked. Doesn't follow mounts.
  1071. * SMP-safe.
  1072. */
  1073. static struct dentry *lookup_hash(struct nameidata *nd)
  1074. {
  1075. int err;
  1076. err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
  1077. if (err)
  1078. return ERR_PTR(err);
  1079. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1080. }
  1081. static int __lookup_one_len(const char *name, struct qstr *this,
  1082. struct dentry *base, int len)
  1083. {
  1084. unsigned long hash;
  1085. unsigned int c;
  1086. this->name = name;
  1087. this->len = len;
  1088. if (!len)
  1089. return -EACCES;
  1090. hash = init_name_hash();
  1091. while (len--) {
  1092. c = *(const unsigned char *)name++;
  1093. if (c == '/' || c == '\0')
  1094. return -EACCES;
  1095. hash = partial_name_hash(c, hash);
  1096. }
  1097. this->hash = end_name_hash(hash);
  1098. return 0;
  1099. }
  1100. /**
  1101. * lookup_one_len - filesystem helper to lookup single pathname component
  1102. * @name: pathname component to lookup
  1103. * @base: base directory to lookup from
  1104. * @len: maximum length @len should be interpreted to
  1105. *
  1106. * Note that this routine is purely a helper for filesystem usage and should
  1107. * not be called by generic code. Also note that by using this function the
  1108. * nameidata argument is passed to the filesystem methods and a filesystem
  1109. * using this helper needs to be prepared for that.
  1110. */
  1111. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1112. {
  1113. int err;
  1114. struct qstr this;
  1115. err = __lookup_one_len(name, &this, base, len);
  1116. if (err)
  1117. return ERR_PTR(err);
  1118. err = inode_permission(base->d_inode, MAY_EXEC);
  1119. if (err)
  1120. return ERR_PTR(err);
  1121. return __lookup_hash(&this, base, NULL);
  1122. }
  1123. /**
  1124. * lookup_one_noperm - bad hack for sysfs
  1125. * @name: pathname component to lookup
  1126. * @base: base directory to lookup from
  1127. *
  1128. * This is a variant of lookup_one_len that doesn't perform any permission
  1129. * checks. It's a horrible hack to work around the braindead sysfs
  1130. * architecture and should not be used anywhere else.
  1131. *
  1132. * DON'T USE THIS FUNCTION EVER, thanks.
  1133. */
  1134. struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
  1135. {
  1136. int err;
  1137. struct qstr this;
  1138. err = __lookup_one_len(name, &this, base, strlen(name));
  1139. if (err)
  1140. return ERR_PTR(err);
  1141. return __lookup_hash(&this, base, NULL);
  1142. }
  1143. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1144. struct path *path)
  1145. {
  1146. struct nameidata nd;
  1147. char *tmp = getname(name);
  1148. int err = PTR_ERR(tmp);
  1149. if (!IS_ERR(tmp)) {
  1150. BUG_ON(flags & LOOKUP_PARENT);
  1151. err = do_path_lookup(dfd, tmp, flags, &nd);
  1152. putname(tmp);
  1153. if (!err)
  1154. *path = nd.path;
  1155. }
  1156. return err;
  1157. }
  1158. static int user_path_parent(int dfd, const char __user *path,
  1159. struct nameidata *nd, char **name)
  1160. {
  1161. char *s = getname(path);
  1162. int error;
  1163. if (IS_ERR(s))
  1164. return PTR_ERR(s);
  1165. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1166. if (error)
  1167. putname(s);
  1168. else
  1169. *name = s;
  1170. return error;
  1171. }
  1172. /*
  1173. * It's inline, so penalty for filesystems that don't use sticky bit is
  1174. * minimal.
  1175. */
  1176. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1177. {
  1178. uid_t fsuid = current_fsuid();
  1179. if (!(dir->i_mode & S_ISVTX))
  1180. return 0;
  1181. if (inode->i_uid == fsuid)
  1182. return 0;
  1183. if (dir->i_uid == fsuid)
  1184. return 0;
  1185. return !capable(CAP_FOWNER);
  1186. }
  1187. /*
  1188. * Check whether we can remove a link victim from directory dir, check
  1189. * whether the type of victim is right.
  1190. * 1. We can't do it if dir is read-only (done in permission())
  1191. * 2. We should have write and exec permissions on dir
  1192. * 3. We can't remove anything from append-only dir
  1193. * 4. We can't do anything with immutable dir (done in permission())
  1194. * 5. If the sticky bit on dir is set we should either
  1195. * a. be owner of dir, or
  1196. * b. be owner of victim, or
  1197. * c. have CAP_FOWNER capability
  1198. * 6. If the victim is append-only or immutable we can't do antyhing with
  1199. * links pointing to it.
  1200. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1201. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1202. * 9. We can't remove a root or mountpoint.
  1203. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1204. * nfs_async_unlink().
  1205. */
  1206. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1207. {
  1208. int error;
  1209. if (!victim->d_inode)
  1210. return -ENOENT;
  1211. BUG_ON(victim->d_parent->d_inode != dir);
  1212. audit_inode_child(victim->d_name.name, victim, dir);
  1213. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1214. if (error)
  1215. return error;
  1216. if (IS_APPEND(dir))
  1217. return -EPERM;
  1218. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1219. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1220. return -EPERM;
  1221. if (isdir) {
  1222. if (!S_ISDIR(victim->d_inode->i_mode))
  1223. return -ENOTDIR;
  1224. if (IS_ROOT(victim))
  1225. return -EBUSY;
  1226. } else if (S_ISDIR(victim->d_inode->i_mode))
  1227. return -EISDIR;
  1228. if (IS_DEADDIR(dir))
  1229. return -ENOENT;
  1230. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1231. return -EBUSY;
  1232. return 0;
  1233. }
  1234. /* Check whether we can create an object with dentry child in directory
  1235. * dir.
  1236. * 1. We can't do it if child already exists (open has special treatment for
  1237. * this case, but since we are inlined it's OK)
  1238. * 2. We can't do it if dir is read-only (done in permission())
  1239. * 3. We should have write and exec permissions on dir
  1240. * 4. We can't do it if dir is immutable (done in permission())
  1241. */
  1242. static inline int may_create(struct inode *dir, struct dentry *child)
  1243. {
  1244. if (child->d_inode)
  1245. return -EEXIST;
  1246. if (IS_DEADDIR(dir))
  1247. return -ENOENT;
  1248. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1249. }
  1250. /*
  1251. * O_DIRECTORY translates into forcing a directory lookup.
  1252. */
  1253. static inline int lookup_flags(unsigned int f)
  1254. {
  1255. unsigned long retval = LOOKUP_FOLLOW;
  1256. if (f & O_NOFOLLOW)
  1257. retval &= ~LOOKUP_FOLLOW;
  1258. if (f & O_DIRECTORY)
  1259. retval |= LOOKUP_DIRECTORY;
  1260. return retval;
  1261. }
  1262. /*
  1263. * p1 and p2 should be directories on the same fs.
  1264. */
  1265. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1266. {
  1267. struct dentry *p;
  1268. if (p1 == p2) {
  1269. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1270. return NULL;
  1271. }
  1272. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1273. p = d_ancestor(p2, p1);
  1274. if (p) {
  1275. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1276. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1277. return p;
  1278. }
  1279. p = d_ancestor(p1, p2);
  1280. if (p) {
  1281. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1282. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1283. return p;
  1284. }
  1285. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1286. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1287. return NULL;
  1288. }
  1289. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1290. {
  1291. mutex_unlock(&p1->d_inode->i_mutex);
  1292. if (p1 != p2) {
  1293. mutex_unlock(&p2->d_inode->i_mutex);
  1294. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1295. }
  1296. }
  1297. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1298. struct nameidata *nd)
  1299. {
  1300. int error = may_create(dir, dentry);
  1301. if (error)
  1302. return error;
  1303. if (!dir->i_op || !dir->i_op->create)
  1304. return -EACCES; /* shouldn't it be ENOSYS? */
  1305. mode &= S_IALLUGO;
  1306. mode |= S_IFREG;
  1307. error = security_inode_create(dir, dentry, mode);
  1308. if (error)
  1309. return error;
  1310. DQUOT_INIT(dir);
  1311. error = dir->i_op->create(dir, dentry, mode, nd);
  1312. if (!error)
  1313. fsnotify_create(dir, dentry);
  1314. return error;
  1315. }
  1316. int may_open(struct path *path, int acc_mode, int flag)
  1317. {
  1318. struct dentry *dentry = path->dentry;
  1319. struct inode *inode = dentry->d_inode;
  1320. int error;
  1321. if (!inode)
  1322. return -ENOENT;
  1323. if (S_ISLNK(inode->i_mode))
  1324. return -ELOOP;
  1325. if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
  1326. return -EISDIR;
  1327. /*
  1328. * FIFO's, sockets and device files are special: they don't
  1329. * actually live on the filesystem itself, and as such you
  1330. * can write to them even if the filesystem is read-only.
  1331. */
  1332. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1333. flag &= ~O_TRUNC;
  1334. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1335. if (path->mnt->mnt_flags & MNT_NODEV)
  1336. return -EACCES;
  1337. flag &= ~O_TRUNC;
  1338. }
  1339. error = inode_permission(inode, acc_mode);
  1340. if (error)
  1341. return error;
  1342. /*
  1343. * An append-only file must be opened in append mode for writing.
  1344. */
  1345. if (IS_APPEND(inode)) {
  1346. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1347. return -EPERM;
  1348. if (flag & O_TRUNC)
  1349. return -EPERM;
  1350. }
  1351. /* O_NOATIME can only be set by the owner or superuser */
  1352. if (flag & O_NOATIME)
  1353. if (!is_owner_or_cap(inode))
  1354. return -EPERM;
  1355. /*
  1356. * Ensure there are no outstanding leases on the file.
  1357. */
  1358. error = break_lease(inode, flag);
  1359. if (error)
  1360. return error;
  1361. if (flag & O_TRUNC) {
  1362. error = get_write_access(inode);
  1363. if (error)
  1364. return error;
  1365. /*
  1366. * Refuse to truncate files with mandatory locks held on them.
  1367. */
  1368. error = locks_verify_locked(inode);
  1369. if (!error)
  1370. error = security_path_truncate(path, 0,
  1371. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
  1372. if (!error) {
  1373. DQUOT_INIT(inode);
  1374. error = do_truncate(dentry, 0,
  1375. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1376. NULL);
  1377. }
  1378. put_write_access(inode);
  1379. if (error)
  1380. return error;
  1381. } else
  1382. if (flag & FMODE_WRITE)
  1383. DQUOT_INIT(inode);
  1384. return 0;
  1385. }
  1386. /*
  1387. * Be careful about ever adding any more callers of this
  1388. * function. Its flags must be in the namei format, not
  1389. * what get passed to sys_open().
  1390. */
  1391. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1392. int flag, int mode)
  1393. {
  1394. int error;
  1395. struct dentry *dir = nd->path.dentry;
  1396. if (!IS_POSIXACL(dir->d_inode))
  1397. mode &= ~current->fs->umask;
  1398. error = security_path_mknod(&nd->path, path->dentry, mode, 0);
  1399. if (error)
  1400. goto out_unlock;
  1401. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1402. out_unlock:
  1403. mutex_unlock(&dir->d_inode->i_mutex);
  1404. dput(nd->path.dentry);
  1405. nd->path.dentry = path->dentry;
  1406. if (error)
  1407. return error;
  1408. /* Don't check for write permission, don't truncate */
  1409. return may_open(&nd->path, 0, flag & ~O_TRUNC);
  1410. }
  1411. /*
  1412. * Note that while the flag value (low two bits) for sys_open means:
  1413. * 00 - read-only
  1414. * 01 - write-only
  1415. * 10 - read-write
  1416. * 11 - special
  1417. * it is changed into
  1418. * 00 - no permissions needed
  1419. * 01 - read-permission
  1420. * 10 - write-permission
  1421. * 11 - read-write
  1422. * for the internal routines (ie open_namei()/follow_link() etc)
  1423. * This is more logical, and also allows the 00 "no perm needed"
  1424. * to be used for symlinks (where the permissions are checked
  1425. * later).
  1426. *
  1427. */
  1428. static inline int open_to_namei_flags(int flag)
  1429. {
  1430. if ((flag+1) & O_ACCMODE)
  1431. flag++;
  1432. return flag;
  1433. }
  1434. static int open_will_write_to_fs(int flag, struct inode *inode)
  1435. {
  1436. /*
  1437. * We'll never write to the fs underlying
  1438. * a device file.
  1439. */
  1440. if (special_file(inode->i_mode))
  1441. return 0;
  1442. return (flag & O_TRUNC);
  1443. }
  1444. /*
  1445. * Note that the low bits of the passed in "open_flag"
  1446. * are not the same as in the local variable "flag". See
  1447. * open_to_namei_flags() for more details.
  1448. */
  1449. struct file *do_filp_open(int dfd, const char *pathname,
  1450. int open_flag, int mode)
  1451. {
  1452. struct file *filp;
  1453. struct nameidata nd;
  1454. int acc_mode, error;
  1455. struct path path;
  1456. struct dentry *dir;
  1457. int count = 0;
  1458. int will_write;
  1459. int flag = open_to_namei_flags(open_flag);
  1460. acc_mode = MAY_OPEN | ACC_MODE(flag);
  1461. /* O_TRUNC implies we need access checks for write permissions */
  1462. if (flag & O_TRUNC)
  1463. acc_mode |= MAY_WRITE;
  1464. /* Allow the LSM permission hook to distinguish append
  1465. access from general write access. */
  1466. if (flag & O_APPEND)
  1467. acc_mode |= MAY_APPEND;
  1468. /*
  1469. * The simplest case - just a plain lookup.
  1470. */
  1471. if (!(flag & O_CREAT)) {
  1472. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1473. &nd, flag);
  1474. if (error)
  1475. return ERR_PTR(error);
  1476. goto ok;
  1477. }
  1478. /*
  1479. * Create - we need to know the parent.
  1480. */
  1481. error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  1482. if (error)
  1483. return ERR_PTR(error);
  1484. /*
  1485. * We have the parent and last component. First of all, check
  1486. * that we are not asked to creat(2) an obvious directory - that
  1487. * will not do.
  1488. */
  1489. error = -EISDIR;
  1490. if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
  1491. goto exit_parent;
  1492. error = -ENFILE;
  1493. filp = get_empty_filp();
  1494. if (filp == NULL)
  1495. goto exit_parent;
  1496. nd.intent.open.file = filp;
  1497. nd.intent.open.flags = flag;
  1498. nd.intent.open.create_mode = mode;
  1499. dir = nd.path.dentry;
  1500. nd.flags &= ~LOOKUP_PARENT;
  1501. nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
  1502. if (flag & O_EXCL)
  1503. nd.flags |= LOOKUP_EXCL;
  1504. mutex_lock(&dir->d_inode->i_mutex);
  1505. path.dentry = lookup_hash(&nd);
  1506. path.mnt = nd.path.mnt;
  1507. do_last:
  1508. error = PTR_ERR(path.dentry);
  1509. if (IS_ERR(path.dentry)) {
  1510. mutex_unlock(&dir->d_inode->i_mutex);
  1511. goto exit;
  1512. }
  1513. if (IS_ERR(nd.intent.open.file)) {
  1514. error = PTR_ERR(nd.intent.open.file);
  1515. goto exit_mutex_unlock;
  1516. }
  1517. /* Negative dentry, just create the file */
  1518. if (!path.dentry->d_inode) {
  1519. /*
  1520. * This write is needed to ensure that a
  1521. * ro->rw transition does not occur between
  1522. * the time when the file is created and when
  1523. * a permanent write count is taken through
  1524. * the 'struct file' in nameidata_to_filp().
  1525. */
  1526. error = mnt_want_write(nd.path.mnt);
  1527. if (error)
  1528. goto exit_mutex_unlock;
  1529. error = __open_namei_create(&nd, &path, flag, mode);
  1530. if (error) {
  1531. mnt_drop_write(nd.path.mnt);
  1532. goto exit;
  1533. }
  1534. filp = nameidata_to_filp(&nd, open_flag);
  1535. mnt_drop_write(nd.path.mnt);
  1536. return filp;
  1537. }
  1538. /*
  1539. * It already exists.
  1540. */
  1541. mutex_unlock(&dir->d_inode->i_mutex);
  1542. audit_inode(pathname, path.dentry);
  1543. error = -EEXIST;
  1544. if (flag & O_EXCL)
  1545. goto exit_dput;
  1546. if (__follow_mount(&path)) {
  1547. error = -ELOOP;
  1548. if (flag & O_NOFOLLOW)
  1549. goto exit_dput;
  1550. }
  1551. error = -ENOENT;
  1552. if (!path.dentry->d_inode)
  1553. goto exit_dput;
  1554. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1555. goto do_link;
  1556. path_to_nameidata(&path, &nd);
  1557. error = -EISDIR;
  1558. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1559. goto exit;
  1560. ok:
  1561. /*
  1562. * Consider:
  1563. * 1. may_open() truncates a file
  1564. * 2. a rw->ro mount transition occurs
  1565. * 3. nameidata_to_filp() fails due to
  1566. * the ro mount.
  1567. * That would be inconsistent, and should
  1568. * be avoided. Taking this mnt write here
  1569. * ensures that (2) can not occur.
  1570. */
  1571. will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
  1572. if (will_write) {
  1573. error = mnt_want_write(nd.path.mnt);
  1574. if (error)
  1575. goto exit;
  1576. }
  1577. error = may_open(&nd.path, acc_mode, flag);
  1578. if (error) {
  1579. if (will_write)
  1580. mnt_drop_write(nd.path.mnt);
  1581. goto exit;
  1582. }
  1583. filp = nameidata_to_filp(&nd, open_flag);
  1584. /*
  1585. * It is now safe to drop the mnt write
  1586. * because the filp has had a write taken
  1587. * on its behalf.
  1588. */
  1589. if (will_write)
  1590. mnt_drop_write(nd.path.mnt);
  1591. return filp;
  1592. exit_mutex_unlock:
  1593. mutex_unlock(&dir->d_inode->i_mutex);
  1594. exit_dput:
  1595. path_put_conditional(&path, &nd);
  1596. exit:
  1597. if (!IS_ERR(nd.intent.open.file))
  1598. release_open_intent(&nd);
  1599. exit_parent:
  1600. path_put(&nd.path);
  1601. return ERR_PTR(error);
  1602. do_link:
  1603. error = -ELOOP;
  1604. if (flag & O_NOFOLLOW)
  1605. goto exit_dput;
  1606. /*
  1607. * This is subtle. Instead of calling do_follow_link() we do the
  1608. * thing by hands. The reason is that this way we have zero link_count
  1609. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1610. * After that we have the parent and last component, i.e.
  1611. * we are in the same situation as after the first path_walk().
  1612. * Well, almost - if the last component is normal we get its copy
  1613. * stored in nd->last.name and we will have to putname() it when we
  1614. * are done. Procfs-like symlinks just set LAST_BIND.
  1615. */
  1616. nd.flags |= LOOKUP_PARENT;
  1617. error = security_inode_follow_link(path.dentry, &nd);
  1618. if (error)
  1619. goto exit_dput;
  1620. error = __do_follow_link(&path, &nd);
  1621. if (error) {
  1622. /* Does someone understand code flow here? Or it is only
  1623. * me so stupid? Anathema to whoever designed this non-sense
  1624. * with "intent.open".
  1625. */
  1626. release_open_intent(&nd);
  1627. return ERR_PTR(error);
  1628. }
  1629. nd.flags &= ~LOOKUP_PARENT;
  1630. if (nd.last_type == LAST_BIND)
  1631. goto ok;
  1632. error = -EISDIR;
  1633. if (nd.last_type != LAST_NORM)
  1634. goto exit;
  1635. if (nd.last.name[nd.last.len]) {
  1636. __putname(nd.last.name);
  1637. goto exit;
  1638. }
  1639. error = -ELOOP;
  1640. if (count++==32) {
  1641. __putname(nd.last.name);
  1642. goto exit;
  1643. }
  1644. dir = nd.path.dentry;
  1645. mutex_lock(&dir->d_inode->i_mutex);
  1646. path.dentry = lookup_hash(&nd);
  1647. path.mnt = nd.path.mnt;
  1648. __putname(nd.last.name);
  1649. goto do_last;
  1650. }
  1651. /**
  1652. * filp_open - open file and return file pointer
  1653. *
  1654. * @filename: path to open
  1655. * @flags: open flags as per the open(2) second argument
  1656. * @mode: mode for the new file if O_CREAT is set, else ignored
  1657. *
  1658. * This is the helper to open a file from kernelspace if you really
  1659. * have to. But in generally you should not do this, so please move
  1660. * along, nothing to see here..
  1661. */
  1662. struct file *filp_open(const char *filename, int flags, int mode)
  1663. {
  1664. return do_filp_open(AT_FDCWD, filename, flags, mode);
  1665. }
  1666. EXPORT_SYMBOL(filp_open);
  1667. /**
  1668. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1669. * @nd: nameidata info
  1670. * @is_dir: directory flag
  1671. *
  1672. * Simple function to lookup and return a dentry and create it
  1673. * if it doesn't exist. Is SMP-safe.
  1674. *
  1675. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  1676. */
  1677. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1678. {
  1679. struct dentry *dentry = ERR_PTR(-EEXIST);
  1680. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1681. /*
  1682. * Yucky last component or no last component at all?
  1683. * (foo/., foo/.., /////)
  1684. */
  1685. if (nd->last_type != LAST_NORM)
  1686. goto fail;
  1687. nd->flags &= ~LOOKUP_PARENT;
  1688. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  1689. nd->intent.open.flags = O_EXCL;
  1690. /*
  1691. * Do the final lookup.
  1692. */
  1693. dentry = lookup_hash(nd);
  1694. if (IS_ERR(dentry))
  1695. goto fail;
  1696. if (dentry->d_inode)
  1697. goto eexist;
  1698. /*
  1699. * Special case - lookup gave negative, but... we had foo/bar/
  1700. * From the vfs_mknod() POV we just have a negative dentry -
  1701. * all is fine. Let's be bastards - you had / on the end, you've
  1702. * been asking for (non-existent) directory. -ENOENT for you.
  1703. */
  1704. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  1705. dput(dentry);
  1706. dentry = ERR_PTR(-ENOENT);
  1707. }
  1708. return dentry;
  1709. eexist:
  1710. dput(dentry);
  1711. dentry = ERR_PTR(-EEXIST);
  1712. fail:
  1713. return dentry;
  1714. }
  1715. EXPORT_SYMBOL_GPL(lookup_create);
  1716. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1717. {
  1718. int error = may_create(dir, dentry);
  1719. if (error)
  1720. return error;
  1721. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1722. return -EPERM;
  1723. if (!dir->i_op || !dir->i_op->mknod)
  1724. return -EPERM;
  1725. error = devcgroup_inode_mknod(mode, dev);
  1726. if (error)
  1727. return error;
  1728. error = security_inode_mknod(dir, dentry, mode, dev);
  1729. if (error)
  1730. return error;
  1731. DQUOT_INIT(dir);
  1732. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1733. if (!error)
  1734. fsnotify_create(dir, dentry);
  1735. return error;
  1736. }
  1737. static int may_mknod(mode_t mode)
  1738. {
  1739. switch (mode & S_IFMT) {
  1740. case S_IFREG:
  1741. case S_IFCHR:
  1742. case S_IFBLK:
  1743. case S_IFIFO:
  1744. case S_IFSOCK:
  1745. case 0: /* zero mode translates to S_IFREG */
  1746. return 0;
  1747. case S_IFDIR:
  1748. return -EPERM;
  1749. default:
  1750. return -EINVAL;
  1751. }
  1752. }
  1753. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1754. unsigned dev)
  1755. {
  1756. int error;
  1757. char *tmp;
  1758. struct dentry *dentry;
  1759. struct nameidata nd;
  1760. if (S_ISDIR(mode))
  1761. return -EPERM;
  1762. error = user_path_parent(dfd, filename, &nd, &tmp);
  1763. if (error)
  1764. return error;
  1765. dentry = lookup_create(&nd, 0);
  1766. if (IS_ERR(dentry)) {
  1767. error = PTR_ERR(dentry);
  1768. goto out_unlock;
  1769. }
  1770. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1771. mode &= ~current->fs->umask;
  1772. error = may_mknod(mode);
  1773. if (error)
  1774. goto out_dput;
  1775. error = mnt_want_write(nd.path.mnt);
  1776. if (error)
  1777. goto out_dput;
  1778. error = security_path_mknod(&nd.path, dentry, mode, dev);
  1779. if (error)
  1780. goto out_drop_write;
  1781. switch (mode & S_IFMT) {
  1782. case 0: case S_IFREG:
  1783. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  1784. break;
  1785. case S_IFCHR: case S_IFBLK:
  1786. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  1787. new_decode_dev(dev));
  1788. break;
  1789. case S_IFIFO: case S_IFSOCK:
  1790. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  1791. break;
  1792. }
  1793. out_drop_write:
  1794. mnt_drop_write(nd.path.mnt);
  1795. out_dput:
  1796. dput(dentry);
  1797. out_unlock:
  1798. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1799. path_put(&nd.path);
  1800. putname(tmp);
  1801. return error;
  1802. }
  1803. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1804. {
  1805. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1806. }
  1807. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1808. {
  1809. int error = may_create(dir, dentry);
  1810. if (error)
  1811. return error;
  1812. if (!dir->i_op || !dir->i_op->mkdir)
  1813. return -EPERM;
  1814. mode &= (S_IRWXUGO|S_ISVTX);
  1815. error = security_inode_mkdir(dir, dentry, mode);
  1816. if (error)
  1817. return error;
  1818. DQUOT_INIT(dir);
  1819. error = dir->i_op->mkdir(dir, dentry, mode);
  1820. if (!error)
  1821. fsnotify_mkdir(dir, dentry);
  1822. return error;
  1823. }
  1824. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1825. {
  1826. int error = 0;
  1827. char * tmp;
  1828. struct dentry *dentry;
  1829. struct nameidata nd;
  1830. error = user_path_parent(dfd, pathname, &nd, &tmp);
  1831. if (error)
  1832. goto out_err;
  1833. dentry = lookup_create(&nd, 1);
  1834. error = PTR_ERR(dentry);
  1835. if (IS_ERR(dentry))
  1836. goto out_unlock;
  1837. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1838. mode &= ~current->fs->umask;
  1839. error = mnt_want_write(nd.path.mnt);
  1840. if (error)
  1841. goto out_dput;
  1842. error = security_path_mkdir(&nd.path, dentry, mode);
  1843. if (error)
  1844. goto out_drop_write;
  1845. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  1846. out_drop_write:
  1847. mnt_drop_write(nd.path.mnt);
  1848. out_dput:
  1849. dput(dentry);
  1850. out_unlock:
  1851. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1852. path_put(&nd.path);
  1853. putname(tmp);
  1854. out_err:
  1855. return error;
  1856. }
  1857. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1858. {
  1859. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1860. }
  1861. /*
  1862. * We try to drop the dentry early: we should have
  1863. * a usage count of 2 if we're the only user of this
  1864. * dentry, and if that is true (possibly after pruning
  1865. * the dcache), then we drop the dentry now.
  1866. *
  1867. * A low-level filesystem can, if it choses, legally
  1868. * do a
  1869. *
  1870. * if (!d_unhashed(dentry))
  1871. * return -EBUSY;
  1872. *
  1873. * if it cannot handle the case of removing a directory
  1874. * that is still in use by something else..
  1875. */
  1876. void dentry_unhash(struct dentry *dentry)
  1877. {
  1878. dget(dentry);
  1879. shrink_dcache_parent(dentry);
  1880. spin_lock(&dcache_lock);
  1881. spin_lock(&dentry->d_lock);
  1882. if (atomic_read(&dentry->d_count) == 2)
  1883. __d_drop(dentry);
  1884. spin_unlock(&dentry->d_lock);
  1885. spin_unlock(&dcache_lock);
  1886. }
  1887. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1888. {
  1889. int error = may_delete(dir, dentry, 1);
  1890. if (error)
  1891. return error;
  1892. if (!dir->i_op || !dir->i_op->rmdir)
  1893. return -EPERM;
  1894. DQUOT_INIT(dir);
  1895. mutex_lock(&dentry->d_inode->i_mutex);
  1896. dentry_unhash(dentry);
  1897. if (d_mountpoint(dentry))
  1898. error = -EBUSY;
  1899. else {
  1900. error = security_inode_rmdir(dir, dentry);
  1901. if (!error) {
  1902. error = dir->i_op->rmdir(dir, dentry);
  1903. if (!error)
  1904. dentry->d_inode->i_flags |= S_DEAD;
  1905. }
  1906. }
  1907. mutex_unlock(&dentry->d_inode->i_mutex);
  1908. if (!error) {
  1909. d_delete(dentry);
  1910. }
  1911. dput(dentry);
  1912. return error;
  1913. }
  1914. static long do_rmdir(int dfd, const char __user *pathname)
  1915. {
  1916. int error = 0;
  1917. char * name;
  1918. struct dentry *dentry;
  1919. struct nameidata nd;
  1920. error = user_path_parent(dfd, pathname, &nd, &name);
  1921. if (error)
  1922. return error;
  1923. switch(nd.last_type) {
  1924. case LAST_DOTDOT:
  1925. error = -ENOTEMPTY;
  1926. goto exit1;
  1927. case LAST_DOT:
  1928. error = -EINVAL;
  1929. goto exit1;
  1930. case LAST_ROOT:
  1931. error = -EBUSY;
  1932. goto exit1;
  1933. }
  1934. nd.flags &= ~LOOKUP_PARENT;
  1935. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1936. dentry = lookup_hash(&nd);
  1937. error = PTR_ERR(dentry);
  1938. if (IS_ERR(dentry))
  1939. goto exit2;
  1940. error = mnt_want_write(nd.path.mnt);
  1941. if (error)
  1942. goto exit3;
  1943. error = security_path_rmdir(&nd.path, dentry);
  1944. if (error)
  1945. goto exit4;
  1946. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  1947. exit4:
  1948. mnt_drop_write(nd.path.mnt);
  1949. exit3:
  1950. dput(dentry);
  1951. exit2:
  1952. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1953. exit1:
  1954. path_put(&nd.path);
  1955. putname(name);
  1956. return error;
  1957. }
  1958. asmlinkage long sys_rmdir(const char __user *pathname)
  1959. {
  1960. return do_rmdir(AT_FDCWD, pathname);
  1961. }
  1962. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1963. {
  1964. int error = may_delete(dir, dentry, 0);
  1965. if (error)
  1966. return error;
  1967. if (!dir->i_op || !dir->i_op->unlink)
  1968. return -EPERM;
  1969. DQUOT_INIT(dir);
  1970. mutex_lock(&dentry->d_inode->i_mutex);
  1971. if (d_mountpoint(dentry))
  1972. error = -EBUSY;
  1973. else {
  1974. error = security_inode_unlink(dir, dentry);
  1975. if (!error)
  1976. error = dir->i_op->unlink(dir, dentry);
  1977. }
  1978. mutex_unlock(&dentry->d_inode->i_mutex);
  1979. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1980. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1981. fsnotify_link_count(dentry->d_inode);
  1982. d_delete(dentry);
  1983. }
  1984. return error;
  1985. }
  1986. /*
  1987. * Make sure that the actual truncation of the file will occur outside its
  1988. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1989. * writeout happening, and we don't want to prevent access to the directory
  1990. * while waiting on the I/O.
  1991. */
  1992. static long do_unlinkat(int dfd, const char __user *pathname)
  1993. {
  1994. int error;
  1995. char *name;
  1996. struct dentry *dentry;
  1997. struct nameidata nd;
  1998. struct inode *inode = NULL;
  1999. error = user_path_parent(dfd, pathname, &nd, &name);
  2000. if (error)
  2001. return error;
  2002. error = -EISDIR;
  2003. if (nd.last_type != LAST_NORM)
  2004. goto exit1;
  2005. nd.flags &= ~LOOKUP_PARENT;
  2006. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2007. dentry = lookup_hash(&nd);
  2008. error = PTR_ERR(dentry);
  2009. if (!IS_ERR(dentry)) {
  2010. /* Why not before? Because we want correct error value */
  2011. if (nd.last.name[nd.last.len])
  2012. goto slashes;
  2013. inode = dentry->d_inode;
  2014. if (inode)
  2015. atomic_inc(&inode->i_count);
  2016. error = mnt_want_write(nd.path.mnt);
  2017. if (error)
  2018. goto exit2;
  2019. error = security_path_unlink(&nd.path, dentry);
  2020. if (error)
  2021. goto exit3;
  2022. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2023. exit3:
  2024. mnt_drop_write(nd.path.mnt);
  2025. exit2:
  2026. dput(dentry);
  2027. }
  2028. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2029. if (inode)
  2030. iput(inode); /* truncate the inode here */
  2031. exit1:
  2032. path_put(&nd.path);
  2033. putname(name);
  2034. return error;
  2035. slashes:
  2036. error = !dentry->d_inode ? -ENOENT :
  2037. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2038. goto exit2;
  2039. }
  2040. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  2041. {
  2042. if ((flag & ~AT_REMOVEDIR) != 0)
  2043. return -EINVAL;
  2044. if (flag & AT_REMOVEDIR)
  2045. return do_rmdir(dfd, pathname);
  2046. return do_unlinkat(dfd, pathname);
  2047. }
  2048. asmlinkage long sys_unlink(const char __user *pathname)
  2049. {
  2050. return do_unlinkat(AT_FDCWD, pathname);
  2051. }
  2052. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2053. {
  2054. int error = may_create(dir, dentry);
  2055. if (error)
  2056. return error;
  2057. if (!dir->i_op || !dir->i_op->symlink)
  2058. return -EPERM;
  2059. error = security_inode_symlink(dir, dentry, oldname);
  2060. if (error)
  2061. return error;
  2062. DQUOT_INIT(dir);
  2063. error = dir->i_op->symlink(dir, dentry, oldname);
  2064. if (!error)
  2065. fsnotify_create(dir, dentry);
  2066. return error;
  2067. }
  2068. asmlinkage long sys_symlinkat(const char __user *oldname,
  2069. int newdfd, const char __user *newname)
  2070. {
  2071. int error;
  2072. char *from;
  2073. char *to;
  2074. struct dentry *dentry;
  2075. struct nameidata nd;
  2076. from = getname(oldname);
  2077. if (IS_ERR(from))
  2078. return PTR_ERR(from);
  2079. error = user_path_parent(newdfd, newname, &nd, &to);
  2080. if (error)
  2081. goto out_putname;
  2082. dentry = lookup_create(&nd, 0);
  2083. error = PTR_ERR(dentry);
  2084. if (IS_ERR(dentry))
  2085. goto out_unlock;
  2086. error = mnt_want_write(nd.path.mnt);
  2087. if (error)
  2088. goto out_dput;
  2089. error = security_path_symlink(&nd.path, dentry, from);
  2090. if (error)
  2091. goto out_drop_write;
  2092. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2093. out_drop_write:
  2094. mnt_drop_write(nd.path.mnt);
  2095. out_dput:
  2096. dput(dentry);
  2097. out_unlock:
  2098. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2099. path_put(&nd.path);
  2100. putname(to);
  2101. out_putname:
  2102. putname(from);
  2103. return error;
  2104. }
  2105. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  2106. {
  2107. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2108. }
  2109. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2110. {
  2111. struct inode *inode = old_dentry->d_inode;
  2112. int error;
  2113. if (!inode)
  2114. return -ENOENT;
  2115. error = may_create(dir, new_dentry);
  2116. if (error)
  2117. return error;
  2118. if (dir->i_sb != inode->i_sb)
  2119. return -EXDEV;
  2120. /*
  2121. * A link to an append-only or immutable file cannot be created.
  2122. */
  2123. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2124. return -EPERM;
  2125. if (!dir->i_op || !dir->i_op->link)
  2126. return -EPERM;
  2127. if (S_ISDIR(inode->i_mode))
  2128. return -EPERM;
  2129. error = security_inode_link(old_dentry, dir, new_dentry);
  2130. if (error)
  2131. return error;
  2132. mutex_lock(&inode->i_mutex);
  2133. DQUOT_INIT(dir);
  2134. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2135. mutex_unlock(&inode->i_mutex);
  2136. if (!error)
  2137. fsnotify_link(dir, inode, new_dentry);
  2138. return error;
  2139. }
  2140. /*
  2141. * Hardlinks are often used in delicate situations. We avoid
  2142. * security-related surprises by not following symlinks on the
  2143. * newname. --KAB
  2144. *
  2145. * We don't follow them on the oldname either to be compatible
  2146. * with linux 2.0, and to avoid hard-linking to directories
  2147. * and other special files. --ADM
  2148. */
  2149. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  2150. int newdfd, const char __user *newname,
  2151. int flags)
  2152. {
  2153. struct dentry *new_dentry;
  2154. struct nameidata nd;
  2155. struct path old_path;
  2156. int error;
  2157. char *to;
  2158. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2159. return -EINVAL;
  2160. error = user_path_at(olddfd, oldname,
  2161. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2162. &old_path);
  2163. if (error)
  2164. return error;
  2165. error = user_path_parent(newdfd, newname, &nd, &to);
  2166. if (error)
  2167. goto out;
  2168. error = -EXDEV;
  2169. if (old_path.mnt != nd.path.mnt)
  2170. goto out_release;
  2171. new_dentry = lookup_create(&nd, 0);
  2172. error = PTR_ERR(new_dentry);
  2173. if (IS_ERR(new_dentry))
  2174. goto out_unlock;
  2175. error = mnt_want_write(nd.path.mnt);
  2176. if (error)
  2177. goto out_dput;
  2178. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2179. if (error)
  2180. goto out_drop_write;
  2181. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2182. out_drop_write:
  2183. mnt_drop_write(nd.path.mnt);
  2184. out_dput:
  2185. dput(new_dentry);
  2186. out_unlock:
  2187. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2188. out_release:
  2189. path_put(&nd.path);
  2190. putname(to);
  2191. out:
  2192. path_put(&old_path);
  2193. return error;
  2194. }
  2195. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2196. {
  2197. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2198. }
  2199. /*
  2200. * The worst of all namespace operations - renaming directory. "Perverted"
  2201. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2202. * Problems:
  2203. * a) we can get into loop creation. Check is done in is_subdir().
  2204. * b) race potential - two innocent renames can create a loop together.
  2205. * That's where 4.4 screws up. Current fix: serialization on
  2206. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2207. * story.
  2208. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2209. * And that - after we got ->i_mutex on parents (until then we don't know
  2210. * whether the target exists). Solution: try to be smart with locking
  2211. * order for inodes. We rely on the fact that tree topology may change
  2212. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2213. * move will be locked. Thus we can rank directories by the tree
  2214. * (ancestors first) and rank all non-directories after them.
  2215. * That works since everybody except rename does "lock parent, lookup,
  2216. * lock child" and rename is under ->s_vfs_rename_mutex.
  2217. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2218. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2219. * we'd better make sure that there's no link(2) for them.
  2220. * d) some filesystems don't support opened-but-unlinked directories,
  2221. * either because of layout or because they are not ready to deal with
  2222. * all cases correctly. The latter will be fixed (taking this sort of
  2223. * stuff into VFS), but the former is not going away. Solution: the same
  2224. * trick as in rmdir().
  2225. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2226. * we are removing the target. Solution: we will have to grab ->i_mutex
  2227. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2228. * ->i_mutex on parents, which works but leads to some truely excessive
  2229. * locking].
  2230. */
  2231. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2232. struct inode *new_dir, struct dentry *new_dentry)
  2233. {
  2234. int error = 0;
  2235. struct inode *target;
  2236. /*
  2237. * If we are going to change the parent - check write permissions,
  2238. * we'll need to flip '..'.
  2239. */
  2240. if (new_dir != old_dir) {
  2241. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2242. if (error)
  2243. return error;
  2244. }
  2245. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2246. if (error)
  2247. return error;
  2248. target = new_dentry->d_inode;
  2249. if (target) {
  2250. mutex_lock(&target->i_mutex);
  2251. dentry_unhash(new_dentry);
  2252. }
  2253. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2254. error = -EBUSY;
  2255. else
  2256. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2257. if (target) {
  2258. if (!error)
  2259. target->i_flags |= S_DEAD;
  2260. mutex_unlock(&target->i_mutex);
  2261. if (d_unhashed(new_dentry))
  2262. d_rehash(new_dentry);
  2263. dput(new_dentry);
  2264. }
  2265. if (!error)
  2266. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2267. d_move(old_dentry,new_dentry);
  2268. return error;
  2269. }
  2270. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2271. struct inode *new_dir, struct dentry *new_dentry)
  2272. {
  2273. struct inode *target;
  2274. int error;
  2275. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2276. if (error)
  2277. return error;
  2278. dget(new_dentry);
  2279. target = new_dentry->d_inode;
  2280. if (target)
  2281. mutex_lock(&target->i_mutex);
  2282. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2283. error = -EBUSY;
  2284. else
  2285. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2286. if (!error) {
  2287. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2288. d_move(old_dentry, new_dentry);
  2289. }
  2290. if (target)
  2291. mutex_unlock(&target->i_mutex);
  2292. dput(new_dentry);
  2293. return error;
  2294. }
  2295. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2296. struct inode *new_dir, struct dentry *new_dentry)
  2297. {
  2298. int error;
  2299. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2300. const char *old_name;
  2301. if (old_dentry->d_inode == new_dentry->d_inode)
  2302. return 0;
  2303. error = may_delete(old_dir, old_dentry, is_dir);
  2304. if (error)
  2305. return error;
  2306. if (!new_dentry->d_inode)
  2307. error = may_create(new_dir, new_dentry);
  2308. else
  2309. error = may_delete(new_dir, new_dentry, is_dir);
  2310. if (error)
  2311. return error;
  2312. if (!old_dir->i_op || !old_dir->i_op->rename)
  2313. return -EPERM;
  2314. DQUOT_INIT(old_dir);
  2315. DQUOT_INIT(new_dir);
  2316. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2317. if (is_dir)
  2318. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2319. else
  2320. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2321. if (!error) {
  2322. const char *new_name = old_dentry->d_name.name;
  2323. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2324. new_dentry->d_inode, old_dentry);
  2325. }
  2326. fsnotify_oldname_free(old_name);
  2327. return error;
  2328. }
  2329. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2330. int newdfd, const char __user *newname)
  2331. {
  2332. struct dentry *old_dir, *new_dir;
  2333. struct dentry *old_dentry, *new_dentry;
  2334. struct dentry *trap;
  2335. struct nameidata oldnd, newnd;
  2336. char *from;
  2337. char *to;
  2338. int error;
  2339. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2340. if (error)
  2341. goto exit;
  2342. error = user_path_parent(newdfd, newname, &newnd, &to);
  2343. if (error)
  2344. goto exit1;
  2345. error = -EXDEV;
  2346. if (oldnd.path.mnt != newnd.path.mnt)
  2347. goto exit2;
  2348. old_dir = oldnd.path.dentry;
  2349. error = -EBUSY;
  2350. if (oldnd.last_type != LAST_NORM)
  2351. goto exit2;
  2352. new_dir = newnd.path.dentry;
  2353. if (newnd.last_type != LAST_NORM)
  2354. goto exit2;
  2355. oldnd.flags &= ~LOOKUP_PARENT;
  2356. newnd.flags &= ~LOOKUP_PARENT;
  2357. newnd.flags |= LOOKUP_RENAME_TARGET;
  2358. trap = lock_rename(new_dir, old_dir);
  2359. old_dentry = lookup_hash(&oldnd);
  2360. error = PTR_ERR(old_dentry);
  2361. if (IS_ERR(old_dentry))
  2362. goto exit3;
  2363. /* source must exist */
  2364. error = -ENOENT;
  2365. if (!old_dentry->d_inode)
  2366. goto exit4;
  2367. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2368. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2369. error = -ENOTDIR;
  2370. if (oldnd.last.name[oldnd.last.len])
  2371. goto exit4;
  2372. if (newnd.last.name[newnd.last.len])
  2373. goto exit4;
  2374. }
  2375. /* source should not be ancestor of target */
  2376. error = -EINVAL;
  2377. if (old_dentry == trap)
  2378. goto exit4;
  2379. new_dentry = lookup_hash(&newnd);
  2380. error = PTR_ERR(new_dentry);
  2381. if (IS_ERR(new_dentry))
  2382. goto exit4;
  2383. /* target should not be an ancestor of source */
  2384. error = -ENOTEMPTY;
  2385. if (new_dentry == trap)
  2386. goto exit5;
  2387. error = mnt_want_write(oldnd.path.mnt);
  2388. if (error)
  2389. goto exit5;
  2390. error = security_path_rename(&oldnd.path, old_dentry,
  2391. &newnd.path, new_dentry);
  2392. if (error)
  2393. goto exit6;
  2394. error = vfs_rename(old_dir->d_inode, old_dentry,
  2395. new_dir->d_inode, new_dentry);
  2396. exit6:
  2397. mnt_drop_write(oldnd.path.mnt);
  2398. exit5:
  2399. dput(new_dentry);
  2400. exit4:
  2401. dput(old_dentry);
  2402. exit3:
  2403. unlock_rename(new_dir, old_dir);
  2404. exit2:
  2405. path_put(&newnd.path);
  2406. putname(to);
  2407. exit1:
  2408. path_put(&oldnd.path);
  2409. putname(from);
  2410. exit:
  2411. return error;
  2412. }
  2413. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2414. {
  2415. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2416. }
  2417. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2418. {
  2419. int len;
  2420. len = PTR_ERR(link);
  2421. if (IS_ERR(link))
  2422. goto out;
  2423. len = strlen(link);
  2424. if (len > (unsigned) buflen)
  2425. len = buflen;
  2426. if (copy_to_user(buffer, link, len))
  2427. len = -EFAULT;
  2428. out:
  2429. return len;
  2430. }
  2431. /*
  2432. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2433. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2434. * using) it for any given inode is up to filesystem.
  2435. */
  2436. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2437. {
  2438. struct nameidata nd;
  2439. void *cookie;
  2440. int res;
  2441. nd.depth = 0;
  2442. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2443. if (IS_ERR(cookie))
  2444. return PTR_ERR(cookie);
  2445. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2446. if (dentry->d_inode->i_op->put_link)
  2447. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2448. return res;
  2449. }
  2450. int vfs_follow_link(struct nameidata *nd, const char *link)
  2451. {
  2452. return __vfs_follow_link(nd, link);
  2453. }
  2454. /* get the link contents into pagecache */
  2455. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2456. {
  2457. char *kaddr;
  2458. struct page *page;
  2459. struct address_space *mapping = dentry->d_inode->i_mapping;
  2460. page = read_mapping_page(mapping, 0, NULL);
  2461. if (IS_ERR(page))
  2462. return (char*)page;
  2463. *ppage = page;
  2464. kaddr = kmap(page);
  2465. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2466. return kaddr;
  2467. }
  2468. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2469. {
  2470. struct page *page = NULL;
  2471. char *s = page_getlink(dentry, &page);
  2472. int res = vfs_readlink(dentry,buffer,buflen,s);
  2473. if (page) {
  2474. kunmap(page);
  2475. page_cache_release(page);
  2476. }
  2477. return res;
  2478. }
  2479. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2480. {
  2481. struct page *page = NULL;
  2482. nd_set_link(nd, page_getlink(dentry, &page));
  2483. return page;
  2484. }
  2485. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2486. {
  2487. struct page *page = cookie;
  2488. if (page) {
  2489. kunmap(page);
  2490. page_cache_release(page);
  2491. }
  2492. }
  2493. /*
  2494. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2495. */
  2496. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2497. {
  2498. struct address_space *mapping = inode->i_mapping;
  2499. struct page *page;
  2500. void *fsdata;
  2501. int err;
  2502. char *kaddr;
  2503. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2504. if (nofs)
  2505. flags |= AOP_FLAG_NOFS;
  2506. retry:
  2507. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2508. flags, &page, &fsdata);
  2509. if (err)
  2510. goto fail;
  2511. kaddr = kmap_atomic(page, KM_USER0);
  2512. memcpy(kaddr, symname, len-1);
  2513. kunmap_atomic(kaddr, KM_USER0);
  2514. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2515. page, fsdata);
  2516. if (err < 0)
  2517. goto fail;
  2518. if (err < len-1)
  2519. goto retry;
  2520. mark_inode_dirty(inode);
  2521. return 0;
  2522. fail:
  2523. return err;
  2524. }
  2525. int page_symlink(struct inode *inode, const char *symname, int len)
  2526. {
  2527. return __page_symlink(inode, symname, len,
  2528. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2529. }
  2530. const struct inode_operations page_symlink_inode_operations = {
  2531. .readlink = generic_readlink,
  2532. .follow_link = page_follow_link_light,
  2533. .put_link = page_put_link,
  2534. };
  2535. EXPORT_SYMBOL(user_path_at);
  2536. EXPORT_SYMBOL(follow_down);
  2537. EXPORT_SYMBOL(follow_up);
  2538. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2539. EXPORT_SYMBOL(getname);
  2540. EXPORT_SYMBOL(lock_rename);
  2541. EXPORT_SYMBOL(lookup_one_len);
  2542. EXPORT_SYMBOL(page_follow_link_light);
  2543. EXPORT_SYMBOL(page_put_link);
  2544. EXPORT_SYMBOL(page_readlink);
  2545. EXPORT_SYMBOL(__page_symlink);
  2546. EXPORT_SYMBOL(page_symlink);
  2547. EXPORT_SYMBOL(page_symlink_inode_operations);
  2548. EXPORT_SYMBOL(path_lookup);
  2549. EXPORT_SYMBOL(kern_path);
  2550. EXPORT_SYMBOL(vfs_path_lookup);
  2551. EXPORT_SYMBOL(inode_permission);
  2552. EXPORT_SYMBOL(file_permission);
  2553. EXPORT_SYMBOL(unlock_rename);
  2554. EXPORT_SYMBOL(vfs_create);
  2555. EXPORT_SYMBOL(vfs_follow_link);
  2556. EXPORT_SYMBOL(vfs_link);
  2557. EXPORT_SYMBOL(vfs_mkdir);
  2558. EXPORT_SYMBOL(vfs_mknod);
  2559. EXPORT_SYMBOL(generic_permission);
  2560. EXPORT_SYMBOL(vfs_readlink);
  2561. EXPORT_SYMBOL(vfs_rename);
  2562. EXPORT_SYMBOL(vfs_rmdir);
  2563. EXPORT_SYMBOL(vfs_symlink);
  2564. EXPORT_SYMBOL(vfs_unlink);
  2565. EXPORT_SYMBOL(dentry_unhash);
  2566. EXPORT_SYMBOL(generic_readlink);
  2567. /* to be mentioned only in INIT_TASK */
  2568. struct fs_struct init_fs = {
  2569. .count = ATOMIC_INIT(1),
  2570. .lock = __RW_LOCK_UNLOCKED(init_fs.lock),
  2571. .umask = 0022,
  2572. };