namei.c 71 KB

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