namei.c 73 KB

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