namei.c 70 KB

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