namei.c 71 KB

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