namespace.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/percpu.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/acct.h>
  18. #include <linux/capability.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/module.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/mnt_namespace.h>
  24. #include <linux/namei.h>
  25. #include <linux/nsproxy.h>
  26. #include <linux/security.h>
  27. #include <linux/mount.h>
  28. #include <linux/ramfs.h>
  29. #include <linux/log2.h>
  30. #include <linux/idr.h>
  31. #include <linux/fs_struct.h>
  32. #include <linux/fsnotify.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/unistd.h>
  35. #include "pnode.h"
  36. #include "internal.h"
  37. #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
  38. #define HASH_SIZE (1UL << HASH_SHIFT)
  39. static int event;
  40. static DEFINE_IDA(mnt_id_ida);
  41. static DEFINE_IDA(mnt_group_ida);
  42. static DEFINE_SPINLOCK(mnt_id_lock);
  43. static int mnt_id_start = 0;
  44. static int mnt_group_start = 1;
  45. static struct list_head *mount_hashtable __read_mostly;
  46. static struct kmem_cache *mnt_cache __read_mostly;
  47. static struct rw_semaphore namespace_sem;
  48. /* /sys/fs */
  49. struct kobject *fs_kobj;
  50. EXPORT_SYMBOL_GPL(fs_kobj);
  51. /*
  52. * vfsmount lock may be taken for read to prevent changes to the
  53. * vfsmount hash, ie. during mountpoint lookups or walking back
  54. * up the tree.
  55. *
  56. * It should be taken for write in all cases where the vfsmount
  57. * tree or hash is modified or when a vfsmount structure is modified.
  58. */
  59. DEFINE_BRLOCK(vfsmount_lock);
  60. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  61. {
  62. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  63. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  64. tmp = tmp + (tmp >> HASH_SHIFT);
  65. return tmp & (HASH_SIZE - 1);
  66. }
  67. #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
  68. /*
  69. * allocation is serialized by namespace_sem, but we need the spinlock to
  70. * serialize with freeing.
  71. */
  72. static int mnt_alloc_id(struct mount *mnt)
  73. {
  74. int res;
  75. retry:
  76. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  77. spin_lock(&mnt_id_lock);
  78. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt.mnt_id);
  79. if (!res)
  80. mnt_id_start = mnt->mnt.mnt_id + 1;
  81. spin_unlock(&mnt_id_lock);
  82. if (res == -EAGAIN)
  83. goto retry;
  84. return res;
  85. }
  86. static void mnt_free_id(struct mount *mnt)
  87. {
  88. int id = mnt->mnt.mnt_id;
  89. spin_lock(&mnt_id_lock);
  90. ida_remove(&mnt_id_ida, id);
  91. if (mnt_id_start > id)
  92. mnt_id_start = id;
  93. spin_unlock(&mnt_id_lock);
  94. }
  95. /*
  96. * Allocate a new peer group ID
  97. *
  98. * mnt_group_ida is protected by namespace_sem
  99. */
  100. static int mnt_alloc_group_id(struct mount *mnt)
  101. {
  102. int res;
  103. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  104. return -ENOMEM;
  105. res = ida_get_new_above(&mnt_group_ida,
  106. mnt_group_start,
  107. &mnt->mnt.mnt_group_id);
  108. if (!res)
  109. mnt_group_start = mnt->mnt.mnt_group_id + 1;
  110. return res;
  111. }
  112. /*
  113. * Release a peer group ID
  114. */
  115. void mnt_release_group_id(struct mount *mnt)
  116. {
  117. int id = mnt->mnt.mnt_group_id;
  118. ida_remove(&mnt_group_ida, id);
  119. if (mnt_group_start > id)
  120. mnt_group_start = id;
  121. mnt->mnt.mnt_group_id = 0;
  122. }
  123. /*
  124. * vfsmount lock must be held for read
  125. */
  126. static inline void mnt_add_count(struct vfsmount *mnt, int n)
  127. {
  128. #ifdef CONFIG_SMP
  129. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  130. #else
  131. preempt_disable();
  132. mnt->mnt_count += n;
  133. preempt_enable();
  134. #endif
  135. }
  136. /*
  137. * vfsmount lock must be held for write
  138. */
  139. unsigned int mnt_get_count(struct vfsmount *mnt)
  140. {
  141. #ifdef CONFIG_SMP
  142. unsigned int count = 0;
  143. int cpu;
  144. for_each_possible_cpu(cpu) {
  145. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  146. }
  147. return count;
  148. #else
  149. return mnt->mnt_count;
  150. #endif
  151. }
  152. static struct mount *alloc_vfsmnt(const char *name)
  153. {
  154. struct mount *p = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  155. if (p) {
  156. struct vfsmount *mnt = &p->mnt;
  157. int err;
  158. err = mnt_alloc_id(p);
  159. if (err)
  160. goto out_free_cache;
  161. if (name) {
  162. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  163. if (!mnt->mnt_devname)
  164. goto out_free_id;
  165. }
  166. #ifdef CONFIG_SMP
  167. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  168. if (!mnt->mnt_pcp)
  169. goto out_free_devname;
  170. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  171. #else
  172. mnt->mnt_count = 1;
  173. mnt->mnt_writers = 0;
  174. #endif
  175. INIT_LIST_HEAD(&p->mnt.mnt_hash);
  176. INIT_LIST_HEAD(&mnt->mnt_child);
  177. INIT_LIST_HEAD(&mnt->mnt_mounts);
  178. INIT_LIST_HEAD(&mnt->mnt_list);
  179. INIT_LIST_HEAD(&mnt->mnt_expire);
  180. INIT_LIST_HEAD(&mnt->mnt_share);
  181. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  182. INIT_LIST_HEAD(&mnt->mnt_slave);
  183. #ifdef CONFIG_FSNOTIFY
  184. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  185. #endif
  186. }
  187. return p;
  188. #ifdef CONFIG_SMP
  189. out_free_devname:
  190. kfree(p->mnt.mnt_devname);
  191. #endif
  192. out_free_id:
  193. mnt_free_id(p);
  194. out_free_cache:
  195. kmem_cache_free(mnt_cache, p);
  196. return NULL;
  197. }
  198. /*
  199. * Most r/o checks on a fs are for operations that take
  200. * discrete amounts of time, like a write() or unlink().
  201. * We must keep track of when those operations start
  202. * (for permission checks) and when they end, so that
  203. * we can determine when writes are able to occur to
  204. * a filesystem.
  205. */
  206. /*
  207. * __mnt_is_readonly: check whether a mount is read-only
  208. * @mnt: the mount to check for its write status
  209. *
  210. * This shouldn't be used directly ouside of the VFS.
  211. * It does not guarantee that the filesystem will stay
  212. * r/w, just that it is right *now*. This can not and
  213. * should not be used in place of IS_RDONLY(inode).
  214. * mnt_want/drop_write() will _keep_ the filesystem
  215. * r/w.
  216. */
  217. int __mnt_is_readonly(struct vfsmount *mnt)
  218. {
  219. if (mnt->mnt_flags & MNT_READONLY)
  220. return 1;
  221. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  222. return 1;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  226. static inline void mnt_inc_writers(struct vfsmount *mnt)
  227. {
  228. #ifdef CONFIG_SMP
  229. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  230. #else
  231. mnt->mnt_writers++;
  232. #endif
  233. }
  234. static inline void mnt_dec_writers(struct vfsmount *mnt)
  235. {
  236. #ifdef CONFIG_SMP
  237. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  238. #else
  239. mnt->mnt_writers--;
  240. #endif
  241. }
  242. static unsigned int mnt_get_writers(struct vfsmount *mnt)
  243. {
  244. #ifdef CONFIG_SMP
  245. unsigned int count = 0;
  246. int cpu;
  247. for_each_possible_cpu(cpu) {
  248. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  249. }
  250. return count;
  251. #else
  252. return mnt->mnt_writers;
  253. #endif
  254. }
  255. /*
  256. * Most r/o checks on a fs are for operations that take
  257. * discrete amounts of time, like a write() or unlink().
  258. * We must keep track of when those operations start
  259. * (for permission checks) and when they end, so that
  260. * we can determine when writes are able to occur to
  261. * a filesystem.
  262. */
  263. /**
  264. * mnt_want_write - get write access to a mount
  265. * @mnt: the mount on which to take a write
  266. *
  267. * This tells the low-level filesystem that a write is
  268. * about to be performed to it, and makes sure that
  269. * writes are allowed before returning success. When
  270. * the write operation is finished, mnt_drop_write()
  271. * must be called. This is effectively a refcount.
  272. */
  273. int mnt_want_write(struct vfsmount *mnt)
  274. {
  275. int ret = 0;
  276. preempt_disable();
  277. mnt_inc_writers(mnt);
  278. /*
  279. * The store to mnt_inc_writers must be visible before we pass
  280. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  281. * incremented count after it has set MNT_WRITE_HOLD.
  282. */
  283. smp_mb();
  284. while (mnt->mnt_flags & MNT_WRITE_HOLD)
  285. cpu_relax();
  286. /*
  287. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  288. * be set to match its requirements. So we must not load that until
  289. * MNT_WRITE_HOLD is cleared.
  290. */
  291. smp_rmb();
  292. if (__mnt_is_readonly(mnt)) {
  293. mnt_dec_writers(mnt);
  294. ret = -EROFS;
  295. goto out;
  296. }
  297. out:
  298. preempt_enable();
  299. return ret;
  300. }
  301. EXPORT_SYMBOL_GPL(mnt_want_write);
  302. /**
  303. * mnt_clone_write - get write access to a mount
  304. * @mnt: the mount on which to take a write
  305. *
  306. * This is effectively like mnt_want_write, except
  307. * it must only be used to take an extra write reference
  308. * on a mountpoint that we already know has a write reference
  309. * on it. This allows some optimisation.
  310. *
  311. * After finished, mnt_drop_write must be called as usual to
  312. * drop the reference.
  313. */
  314. int mnt_clone_write(struct vfsmount *mnt)
  315. {
  316. /* superblock may be r/o */
  317. if (__mnt_is_readonly(mnt))
  318. return -EROFS;
  319. preempt_disable();
  320. mnt_inc_writers(mnt);
  321. preempt_enable();
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(mnt_clone_write);
  325. /**
  326. * mnt_want_write_file - get write access to a file's mount
  327. * @file: the file who's mount on which to take a write
  328. *
  329. * This is like mnt_want_write, but it takes a file and can
  330. * do some optimisations if the file is open for write already
  331. */
  332. int mnt_want_write_file(struct file *file)
  333. {
  334. struct inode *inode = file->f_dentry->d_inode;
  335. if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
  336. return mnt_want_write(file->f_path.mnt);
  337. else
  338. return mnt_clone_write(file->f_path.mnt);
  339. }
  340. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  341. /**
  342. * mnt_drop_write - give up write access to a mount
  343. * @mnt: the mount on which to give up write access
  344. *
  345. * Tells the low-level filesystem that we are done
  346. * performing writes to it. Must be matched with
  347. * mnt_want_write() call above.
  348. */
  349. void mnt_drop_write(struct vfsmount *mnt)
  350. {
  351. preempt_disable();
  352. mnt_dec_writers(mnt);
  353. preempt_enable();
  354. }
  355. EXPORT_SYMBOL_GPL(mnt_drop_write);
  356. void mnt_drop_write_file(struct file *file)
  357. {
  358. mnt_drop_write(file->f_path.mnt);
  359. }
  360. EXPORT_SYMBOL(mnt_drop_write_file);
  361. static int mnt_make_readonly(struct vfsmount *mnt)
  362. {
  363. int ret = 0;
  364. br_write_lock(vfsmount_lock);
  365. mnt->mnt_flags |= MNT_WRITE_HOLD;
  366. /*
  367. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  368. * should be visible before we do.
  369. */
  370. smp_mb();
  371. /*
  372. * With writers on hold, if this value is zero, then there are
  373. * definitely no active writers (although held writers may subsequently
  374. * increment the count, they'll have to wait, and decrement it after
  375. * seeing MNT_READONLY).
  376. *
  377. * It is OK to have counter incremented on one CPU and decremented on
  378. * another: the sum will add up correctly. The danger would be when we
  379. * sum up each counter, if we read a counter before it is incremented,
  380. * but then read another CPU's count which it has been subsequently
  381. * decremented from -- we would see more decrements than we should.
  382. * MNT_WRITE_HOLD protects against this scenario, because
  383. * mnt_want_write first increments count, then smp_mb, then spins on
  384. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  385. * we're counting up here.
  386. */
  387. if (mnt_get_writers(mnt) > 0)
  388. ret = -EBUSY;
  389. else
  390. mnt->mnt_flags |= MNT_READONLY;
  391. /*
  392. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  393. * that become unheld will see MNT_READONLY.
  394. */
  395. smp_wmb();
  396. mnt->mnt_flags &= ~MNT_WRITE_HOLD;
  397. br_write_unlock(vfsmount_lock);
  398. return ret;
  399. }
  400. static void __mnt_unmake_readonly(struct vfsmount *mnt)
  401. {
  402. br_write_lock(vfsmount_lock);
  403. mnt->mnt_flags &= ~MNT_READONLY;
  404. br_write_unlock(vfsmount_lock);
  405. }
  406. static void free_vfsmnt(struct mount *mnt)
  407. {
  408. kfree(mnt->mnt.mnt_devname);
  409. mnt_free_id(mnt);
  410. #ifdef CONFIG_SMP
  411. free_percpu(mnt->mnt.mnt_pcp);
  412. #endif
  413. kmem_cache_free(mnt_cache, mnt);
  414. }
  415. /*
  416. * find the first or last mount at @dentry on vfsmount @mnt depending on
  417. * @dir. If @dir is set return the first mount else return the last mount.
  418. * vfsmount_lock must be held for read or write.
  419. */
  420. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  421. int dir)
  422. {
  423. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  424. struct list_head *tmp = head;
  425. struct mount *p, *found = NULL;
  426. for (;;) {
  427. tmp = dir ? tmp->next : tmp->prev;
  428. p = NULL;
  429. if (tmp == head)
  430. break;
  431. p = list_entry(tmp, struct mount, mnt.mnt_hash);
  432. if (p->mnt.mnt_parent == mnt && p->mnt.mnt_mountpoint == dentry) {
  433. found = p;
  434. break;
  435. }
  436. }
  437. return found;
  438. }
  439. /*
  440. * lookup_mnt increments the ref count before returning
  441. * the vfsmount struct.
  442. */
  443. struct vfsmount *lookup_mnt(struct path *path)
  444. {
  445. struct mount *child_mnt;
  446. br_read_lock(vfsmount_lock);
  447. child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
  448. if (child_mnt) {
  449. mnt_add_count(child_mnt, 1);
  450. br_read_unlock(vfsmount_lock);
  451. return &child_mnt->mnt;
  452. } else {
  453. br_read_unlock(vfsmount_lock);
  454. return NULL;
  455. }
  456. }
  457. static inline int check_mnt(struct vfsmount *mnt)
  458. {
  459. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  460. }
  461. /*
  462. * vfsmount lock must be held for write
  463. */
  464. static void touch_mnt_namespace(struct mnt_namespace *ns)
  465. {
  466. if (ns) {
  467. ns->event = ++event;
  468. wake_up_interruptible(&ns->poll);
  469. }
  470. }
  471. /*
  472. * vfsmount lock must be held for write
  473. */
  474. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  475. {
  476. if (ns && ns->event != event) {
  477. ns->event = event;
  478. wake_up_interruptible(&ns->poll);
  479. }
  480. }
  481. /*
  482. * Clear dentry's mounted state if it has no remaining mounts.
  483. * vfsmount_lock must be held for write.
  484. */
  485. static void dentry_reset_mounted(struct dentry *dentry)
  486. {
  487. unsigned u;
  488. for (u = 0; u < HASH_SIZE; u++) {
  489. struct mount *p;
  490. list_for_each_entry(p, &mount_hashtable[u], mnt.mnt_hash) {
  491. if (p->mnt.mnt_mountpoint == dentry)
  492. return;
  493. }
  494. }
  495. spin_lock(&dentry->d_lock);
  496. dentry->d_flags &= ~DCACHE_MOUNTED;
  497. spin_unlock(&dentry->d_lock);
  498. }
  499. /*
  500. * vfsmount lock must be held for write
  501. */
  502. static void detach_mnt(struct mount *mnt, struct path *old_path)
  503. {
  504. old_path->dentry = mnt->mnt.mnt_mountpoint;
  505. old_path->mnt = mnt->mnt.mnt_parent;
  506. mnt->mnt.mnt_parent = &mnt->mnt;
  507. mnt->mnt.mnt_mountpoint = mnt->mnt.mnt_root;
  508. list_del_init(&mnt->mnt.mnt_child);
  509. list_del_init(&mnt->mnt.mnt_hash);
  510. dentry_reset_mounted(old_path->dentry);
  511. }
  512. /*
  513. * vfsmount lock must be held for write
  514. */
  515. void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
  516. struct vfsmount *child_mnt)
  517. {
  518. child_mnt->mnt_parent = mntget(mnt);
  519. child_mnt->mnt_mountpoint = dget(dentry);
  520. spin_lock(&dentry->d_lock);
  521. dentry->d_flags |= DCACHE_MOUNTED;
  522. spin_unlock(&dentry->d_lock);
  523. }
  524. /*
  525. * vfsmount lock must be held for write
  526. */
  527. static void attach_mnt(struct mount *mnt, struct path *path)
  528. {
  529. mnt_set_mountpoint(path->mnt, path->dentry, &mnt->mnt);
  530. list_add_tail(&mnt->mnt.mnt_hash, mount_hashtable +
  531. hash(path->mnt, path->dentry));
  532. list_add_tail(&mnt->mnt.mnt_child, &path->mnt->mnt_mounts);
  533. }
  534. static inline void __mnt_make_longterm(struct vfsmount *mnt)
  535. {
  536. #ifdef CONFIG_SMP
  537. atomic_inc(&mnt->mnt_longterm);
  538. #endif
  539. }
  540. /* needs vfsmount lock for write */
  541. static inline void __mnt_make_shortterm(struct vfsmount *mnt)
  542. {
  543. #ifdef CONFIG_SMP
  544. atomic_dec(&mnt->mnt_longterm);
  545. #endif
  546. }
  547. /*
  548. * vfsmount lock must be held for write
  549. */
  550. static void commit_tree(struct mount *mnt)
  551. {
  552. struct vfsmount *parent = mnt->mnt.mnt_parent;
  553. struct vfsmount *m;
  554. LIST_HEAD(head);
  555. struct mnt_namespace *n = parent->mnt_ns;
  556. BUG_ON(parent == &mnt->mnt);
  557. list_add_tail(&head, &mnt->mnt.mnt_list);
  558. list_for_each_entry(m, &head, mnt_list) {
  559. m->mnt_ns = n;
  560. __mnt_make_longterm(m);
  561. }
  562. list_splice(&head, n->list.prev);
  563. list_add_tail(&mnt->mnt.mnt_hash, mount_hashtable +
  564. hash(parent, mnt->mnt.mnt_mountpoint));
  565. list_add_tail(&mnt->mnt.mnt_child, &parent->mnt_mounts);
  566. touch_mnt_namespace(n);
  567. }
  568. static struct mount *next_mnt(struct mount *p, struct vfsmount *root)
  569. {
  570. struct list_head *next = p->mnt.mnt_mounts.next;
  571. if (next == &p->mnt.mnt_mounts) {
  572. while (1) {
  573. if (&p->mnt == root)
  574. return NULL;
  575. next = p->mnt.mnt_child.next;
  576. if (next != &p->mnt.mnt_parent->mnt_mounts)
  577. break;
  578. p = real_mount(p->mnt.mnt_parent);
  579. }
  580. }
  581. return list_entry(next, struct mount, mnt.mnt_child);
  582. }
  583. static struct mount *skip_mnt_tree(struct mount *p)
  584. {
  585. struct list_head *prev = p->mnt.mnt_mounts.prev;
  586. while (prev != &p->mnt.mnt_mounts) {
  587. p = list_entry(prev, struct mount, mnt.mnt_child);
  588. prev = p->mnt.mnt_mounts.prev;
  589. }
  590. return p;
  591. }
  592. struct vfsmount *
  593. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  594. {
  595. struct mount *mnt;
  596. struct dentry *root;
  597. if (!type)
  598. return ERR_PTR(-ENODEV);
  599. mnt = alloc_vfsmnt(name);
  600. if (!mnt)
  601. return ERR_PTR(-ENOMEM);
  602. if (flags & MS_KERNMOUNT)
  603. mnt->mnt.mnt_flags = MNT_INTERNAL;
  604. root = mount_fs(type, flags, name, data);
  605. if (IS_ERR(root)) {
  606. free_vfsmnt(mnt);
  607. return ERR_CAST(root);
  608. }
  609. mnt->mnt.mnt_root = root;
  610. mnt->mnt.mnt_sb = root->d_sb;
  611. mnt->mnt.mnt_mountpoint = mnt->mnt.mnt_root;
  612. mnt->mnt.mnt_parent = &mnt->mnt;
  613. return &mnt->mnt;
  614. }
  615. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  616. static struct mount *clone_mnt(struct vfsmount *old, struct dentry *root,
  617. int flag)
  618. {
  619. struct super_block *sb = old->mnt_sb;
  620. struct mount *mnt = alloc_vfsmnt(old->mnt_devname);
  621. if (mnt) {
  622. if (flag & (CL_SLAVE | CL_PRIVATE))
  623. mnt->mnt.mnt_group_id = 0; /* not a peer of original */
  624. else
  625. mnt->mnt.mnt_group_id = old->mnt_group_id;
  626. if ((flag & CL_MAKE_SHARED) && !mnt->mnt.mnt_group_id) {
  627. int err = mnt_alloc_group_id(mnt);
  628. if (err)
  629. goto out_free;
  630. }
  631. mnt->mnt.mnt_flags = old->mnt_flags & ~MNT_WRITE_HOLD;
  632. atomic_inc(&sb->s_active);
  633. mnt->mnt.mnt_sb = sb;
  634. mnt->mnt.mnt_root = dget(root);
  635. mnt->mnt.mnt_mountpoint = mnt->mnt.mnt_root;
  636. mnt->mnt.mnt_parent = &mnt->mnt;
  637. if (flag & CL_SLAVE) {
  638. list_add(&mnt->mnt.mnt_slave, &old->mnt_slave_list);
  639. mnt->mnt.mnt_master = old;
  640. CLEAR_MNT_SHARED(&mnt->mnt);
  641. } else if (!(flag & CL_PRIVATE)) {
  642. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  643. list_add(&mnt->mnt.mnt_share, &old->mnt_share);
  644. if (IS_MNT_SLAVE(old))
  645. list_add(&mnt->mnt.mnt_slave, &old->mnt_slave);
  646. mnt->mnt.mnt_master = old->mnt_master;
  647. }
  648. if (flag & CL_MAKE_SHARED)
  649. set_mnt_shared(mnt);
  650. /* stick the duplicate mount on the same expiry list
  651. * as the original if that was on one */
  652. if (flag & CL_EXPIRE) {
  653. if (!list_empty(&old->mnt_expire))
  654. list_add(&mnt->mnt.mnt_expire, &old->mnt_expire);
  655. }
  656. }
  657. return mnt;
  658. out_free:
  659. free_vfsmnt(mnt);
  660. return NULL;
  661. }
  662. static inline void mntfree(struct vfsmount *mnt)
  663. {
  664. struct super_block *sb = mnt->mnt_sb;
  665. /*
  666. * This probably indicates that somebody messed
  667. * up a mnt_want/drop_write() pair. If this
  668. * happens, the filesystem was probably unable
  669. * to make r/w->r/o transitions.
  670. */
  671. /*
  672. * The locking used to deal with mnt_count decrement provides barriers,
  673. * so mnt_get_writers() below is safe.
  674. */
  675. WARN_ON(mnt_get_writers(mnt));
  676. fsnotify_vfsmount_delete(mnt);
  677. dput(mnt->mnt_root);
  678. free_vfsmnt(real_mount(mnt));
  679. deactivate_super(sb);
  680. }
  681. static void mntput_no_expire(struct vfsmount *mnt)
  682. {
  683. put_again:
  684. #ifdef CONFIG_SMP
  685. br_read_lock(vfsmount_lock);
  686. if (likely(atomic_read(&mnt->mnt_longterm))) {
  687. mnt_add_count(mnt, -1);
  688. br_read_unlock(vfsmount_lock);
  689. return;
  690. }
  691. br_read_unlock(vfsmount_lock);
  692. br_write_lock(vfsmount_lock);
  693. mnt_add_count(mnt, -1);
  694. if (mnt_get_count(mnt)) {
  695. br_write_unlock(vfsmount_lock);
  696. return;
  697. }
  698. #else
  699. mnt_add_count(mnt, -1);
  700. if (likely(mnt_get_count(mnt)))
  701. return;
  702. br_write_lock(vfsmount_lock);
  703. #endif
  704. if (unlikely(mnt->mnt_pinned)) {
  705. mnt_add_count(mnt, mnt->mnt_pinned + 1);
  706. mnt->mnt_pinned = 0;
  707. br_write_unlock(vfsmount_lock);
  708. acct_auto_close_mnt(mnt);
  709. goto put_again;
  710. }
  711. br_write_unlock(vfsmount_lock);
  712. mntfree(mnt);
  713. }
  714. void mntput(struct vfsmount *mnt)
  715. {
  716. if (mnt) {
  717. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  718. if (unlikely(mnt->mnt_expiry_mark))
  719. mnt->mnt_expiry_mark = 0;
  720. mntput_no_expire(mnt);
  721. }
  722. }
  723. EXPORT_SYMBOL(mntput);
  724. struct vfsmount *mntget(struct vfsmount *mnt)
  725. {
  726. if (mnt)
  727. mnt_add_count(mnt, 1);
  728. return mnt;
  729. }
  730. EXPORT_SYMBOL(mntget);
  731. void mnt_pin(struct vfsmount *mnt)
  732. {
  733. br_write_lock(vfsmount_lock);
  734. mnt->mnt_pinned++;
  735. br_write_unlock(vfsmount_lock);
  736. }
  737. EXPORT_SYMBOL(mnt_pin);
  738. void mnt_unpin(struct vfsmount *mnt)
  739. {
  740. br_write_lock(vfsmount_lock);
  741. if (mnt->mnt_pinned) {
  742. mnt_add_count(mnt, 1);
  743. mnt->mnt_pinned--;
  744. }
  745. br_write_unlock(vfsmount_lock);
  746. }
  747. EXPORT_SYMBOL(mnt_unpin);
  748. static inline void mangle(struct seq_file *m, const char *s)
  749. {
  750. seq_escape(m, s, " \t\n\\");
  751. }
  752. /*
  753. * Simple .show_options callback for filesystems which don't want to
  754. * implement more complex mount option showing.
  755. *
  756. * See also save_mount_options().
  757. */
  758. int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
  759. {
  760. const char *options;
  761. rcu_read_lock();
  762. options = rcu_dereference(mnt->mnt_sb->s_options);
  763. if (options != NULL && options[0]) {
  764. seq_putc(m, ',');
  765. mangle(m, options);
  766. }
  767. rcu_read_unlock();
  768. return 0;
  769. }
  770. EXPORT_SYMBOL(generic_show_options);
  771. /*
  772. * If filesystem uses generic_show_options(), this function should be
  773. * called from the fill_super() callback.
  774. *
  775. * The .remount_fs callback usually needs to be handled in a special
  776. * way, to make sure, that previous options are not overwritten if the
  777. * remount fails.
  778. *
  779. * Also note, that if the filesystem's .remount_fs function doesn't
  780. * reset all options to their default value, but changes only newly
  781. * given options, then the displayed options will not reflect reality
  782. * any more.
  783. */
  784. void save_mount_options(struct super_block *sb, char *options)
  785. {
  786. BUG_ON(sb->s_options);
  787. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  788. }
  789. EXPORT_SYMBOL(save_mount_options);
  790. void replace_mount_options(struct super_block *sb, char *options)
  791. {
  792. char *old = sb->s_options;
  793. rcu_assign_pointer(sb->s_options, options);
  794. if (old) {
  795. synchronize_rcu();
  796. kfree(old);
  797. }
  798. }
  799. EXPORT_SYMBOL(replace_mount_options);
  800. #ifdef CONFIG_PROC_FS
  801. /* iterator */
  802. static void *m_start(struct seq_file *m, loff_t *pos)
  803. {
  804. struct proc_mounts *p = m->private;
  805. down_read(&namespace_sem);
  806. return seq_list_start(&p->ns->list, *pos);
  807. }
  808. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  809. {
  810. struct proc_mounts *p = m->private;
  811. return seq_list_next(v, &p->ns->list, pos);
  812. }
  813. static void m_stop(struct seq_file *m, void *v)
  814. {
  815. up_read(&namespace_sem);
  816. }
  817. int mnt_had_events(struct proc_mounts *p)
  818. {
  819. struct mnt_namespace *ns = p->ns;
  820. int res = 0;
  821. br_read_lock(vfsmount_lock);
  822. if (p->m.poll_event != ns->event) {
  823. p->m.poll_event = ns->event;
  824. res = 1;
  825. }
  826. br_read_unlock(vfsmount_lock);
  827. return res;
  828. }
  829. struct proc_fs_info {
  830. int flag;
  831. const char *str;
  832. };
  833. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  834. {
  835. static const struct proc_fs_info fs_info[] = {
  836. { MS_SYNCHRONOUS, ",sync" },
  837. { MS_DIRSYNC, ",dirsync" },
  838. { MS_MANDLOCK, ",mand" },
  839. { 0, NULL }
  840. };
  841. const struct proc_fs_info *fs_infop;
  842. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  843. if (sb->s_flags & fs_infop->flag)
  844. seq_puts(m, fs_infop->str);
  845. }
  846. return security_sb_show_options(m, sb);
  847. }
  848. static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
  849. {
  850. static const struct proc_fs_info mnt_info[] = {
  851. { MNT_NOSUID, ",nosuid" },
  852. { MNT_NODEV, ",nodev" },
  853. { MNT_NOEXEC, ",noexec" },
  854. { MNT_NOATIME, ",noatime" },
  855. { MNT_NODIRATIME, ",nodiratime" },
  856. { MNT_RELATIME, ",relatime" },
  857. { 0, NULL }
  858. };
  859. const struct proc_fs_info *fs_infop;
  860. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  861. if (mnt->mnt_flags & fs_infop->flag)
  862. seq_puts(m, fs_infop->str);
  863. }
  864. }
  865. static void show_type(struct seq_file *m, struct super_block *sb)
  866. {
  867. mangle(m, sb->s_type->name);
  868. if (sb->s_subtype && sb->s_subtype[0]) {
  869. seq_putc(m, '.');
  870. mangle(m, sb->s_subtype);
  871. }
  872. }
  873. static int show_vfsmnt(struct seq_file *m, void *v)
  874. {
  875. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  876. int err = 0;
  877. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  878. if (mnt->mnt_sb->s_op->show_devname) {
  879. err = mnt->mnt_sb->s_op->show_devname(m, mnt);
  880. if (err)
  881. goto out;
  882. } else {
  883. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  884. }
  885. seq_putc(m, ' ');
  886. seq_path(m, &mnt_path, " \t\n\\");
  887. seq_putc(m, ' ');
  888. show_type(m, mnt->mnt_sb);
  889. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  890. err = show_sb_opts(m, mnt->mnt_sb);
  891. if (err)
  892. goto out;
  893. show_mnt_opts(m, mnt);
  894. if (mnt->mnt_sb->s_op->show_options)
  895. err = mnt->mnt_sb->s_op->show_options(m, mnt);
  896. seq_puts(m, " 0 0\n");
  897. out:
  898. return err;
  899. }
  900. const struct seq_operations mounts_op = {
  901. .start = m_start,
  902. .next = m_next,
  903. .stop = m_stop,
  904. .show = show_vfsmnt
  905. };
  906. static int show_mountinfo(struct seq_file *m, void *v)
  907. {
  908. struct proc_mounts *p = m->private;
  909. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  910. struct super_block *sb = mnt->mnt_sb;
  911. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  912. struct path root = p->root;
  913. int err = 0;
  914. seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
  915. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  916. if (sb->s_op->show_path)
  917. err = sb->s_op->show_path(m, mnt);
  918. else
  919. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  920. if (err)
  921. goto out;
  922. seq_putc(m, ' ');
  923. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  924. err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
  925. if (err)
  926. goto out;
  927. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  928. show_mnt_opts(m, mnt);
  929. /* Tagged fields ("foo:X" or "bar") */
  930. if (IS_MNT_SHARED(mnt))
  931. seq_printf(m, " shared:%i", mnt->mnt_group_id);
  932. if (IS_MNT_SLAVE(mnt)) {
  933. int master = mnt->mnt_master->mnt_group_id;
  934. int dom = get_dominating_id(mnt, &p->root);
  935. seq_printf(m, " master:%i", master);
  936. if (dom && dom != master)
  937. seq_printf(m, " propagate_from:%i", dom);
  938. }
  939. if (IS_MNT_UNBINDABLE(mnt))
  940. seq_puts(m, " unbindable");
  941. /* Filesystem specific data */
  942. seq_puts(m, " - ");
  943. show_type(m, sb);
  944. seq_putc(m, ' ');
  945. if (sb->s_op->show_devname)
  946. err = sb->s_op->show_devname(m, mnt);
  947. else
  948. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  949. if (err)
  950. goto out;
  951. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  952. err = show_sb_opts(m, sb);
  953. if (err)
  954. goto out;
  955. if (sb->s_op->show_options)
  956. err = sb->s_op->show_options(m, mnt);
  957. seq_putc(m, '\n');
  958. out:
  959. return err;
  960. }
  961. const struct seq_operations mountinfo_op = {
  962. .start = m_start,
  963. .next = m_next,
  964. .stop = m_stop,
  965. .show = show_mountinfo,
  966. };
  967. static int show_vfsstat(struct seq_file *m, void *v)
  968. {
  969. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  970. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  971. int err = 0;
  972. /* device */
  973. if (mnt->mnt_sb->s_op->show_devname) {
  974. seq_puts(m, "device ");
  975. err = mnt->mnt_sb->s_op->show_devname(m, mnt);
  976. } else {
  977. if (mnt->mnt_devname) {
  978. seq_puts(m, "device ");
  979. mangle(m, mnt->mnt_devname);
  980. } else
  981. seq_puts(m, "no device");
  982. }
  983. /* mount point */
  984. seq_puts(m, " mounted on ");
  985. seq_path(m, &mnt_path, " \t\n\\");
  986. seq_putc(m, ' ');
  987. /* file system type */
  988. seq_puts(m, "with fstype ");
  989. show_type(m, mnt->mnt_sb);
  990. /* optional statistics */
  991. if (mnt->mnt_sb->s_op->show_stats) {
  992. seq_putc(m, ' ');
  993. if (!err)
  994. err = mnt->mnt_sb->s_op->show_stats(m, mnt);
  995. }
  996. seq_putc(m, '\n');
  997. return err;
  998. }
  999. const struct seq_operations mountstats_op = {
  1000. .start = m_start,
  1001. .next = m_next,
  1002. .stop = m_stop,
  1003. .show = show_vfsstat,
  1004. };
  1005. #endif /* CONFIG_PROC_FS */
  1006. /**
  1007. * may_umount_tree - check if a mount tree is busy
  1008. * @mnt: root of mount tree
  1009. *
  1010. * This is called to check if a tree of mounts has any
  1011. * open files, pwds, chroots or sub mounts that are
  1012. * busy.
  1013. */
  1014. int may_umount_tree(struct vfsmount *mnt)
  1015. {
  1016. int actual_refs = 0;
  1017. int minimum_refs = 0;
  1018. struct mount *p;
  1019. BUG_ON(!mnt);
  1020. /* write lock needed for mnt_get_count */
  1021. br_write_lock(vfsmount_lock);
  1022. for (p = real_mount(mnt); p; p = next_mnt(p, mnt)) {
  1023. actual_refs += mnt_get_count(&p->mnt);
  1024. minimum_refs += 2;
  1025. }
  1026. br_write_unlock(vfsmount_lock);
  1027. if (actual_refs > minimum_refs)
  1028. return 0;
  1029. return 1;
  1030. }
  1031. EXPORT_SYMBOL(may_umount_tree);
  1032. /**
  1033. * may_umount - check if a mount point is busy
  1034. * @mnt: root of mount
  1035. *
  1036. * This is called to check if a mount point has any
  1037. * open files, pwds, chroots or sub mounts. If the
  1038. * mount has sub mounts this will return busy
  1039. * regardless of whether the sub mounts are busy.
  1040. *
  1041. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1042. * give false negatives. The main reason why it's here is that we need
  1043. * a non-destructive way to look for easily umountable filesystems.
  1044. */
  1045. int may_umount(struct vfsmount *mnt)
  1046. {
  1047. int ret = 1;
  1048. down_read(&namespace_sem);
  1049. br_write_lock(vfsmount_lock);
  1050. if (propagate_mount_busy(mnt, 2))
  1051. ret = 0;
  1052. br_write_unlock(vfsmount_lock);
  1053. up_read(&namespace_sem);
  1054. return ret;
  1055. }
  1056. EXPORT_SYMBOL(may_umount);
  1057. void release_mounts(struct list_head *head)
  1058. {
  1059. struct mount *mnt;
  1060. while (!list_empty(head)) {
  1061. mnt = list_first_entry(head, struct mount, mnt.mnt_hash);
  1062. list_del_init(&mnt->mnt.mnt_hash);
  1063. if (mnt_has_parent(&mnt->mnt)) {
  1064. struct dentry *dentry;
  1065. struct vfsmount *m;
  1066. br_write_lock(vfsmount_lock);
  1067. dentry = mnt->mnt.mnt_mountpoint;
  1068. m = mnt->mnt.mnt_parent;
  1069. mnt->mnt.mnt_mountpoint = mnt->mnt.mnt_root;
  1070. mnt->mnt.mnt_parent = &mnt->mnt;
  1071. m->mnt_ghosts--;
  1072. br_write_unlock(vfsmount_lock);
  1073. dput(dentry);
  1074. mntput(m);
  1075. }
  1076. mntput(&mnt->mnt);
  1077. }
  1078. }
  1079. /*
  1080. * vfsmount lock must be held for write
  1081. * namespace_sem must be held for write
  1082. */
  1083. void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
  1084. {
  1085. LIST_HEAD(tmp_list);
  1086. struct mount *p;
  1087. for (p = real_mount(mnt); p; p = next_mnt(p, mnt))
  1088. list_move(&p->mnt.mnt_hash, &tmp_list);
  1089. if (propagate)
  1090. propagate_umount(&tmp_list);
  1091. list_for_each_entry(p, &tmp_list, mnt.mnt_hash) {
  1092. list_del_init(&p->mnt.mnt_expire);
  1093. list_del_init(&p->mnt.mnt_list);
  1094. __touch_mnt_namespace(p->mnt.mnt_ns);
  1095. p->mnt.mnt_ns = NULL;
  1096. __mnt_make_shortterm(&p->mnt);
  1097. list_del_init(&p->mnt.mnt_child);
  1098. if (mnt_has_parent(&p->mnt)) {
  1099. p->mnt.mnt_parent->mnt_ghosts++;
  1100. dentry_reset_mounted(p->mnt.mnt_mountpoint);
  1101. }
  1102. change_mnt_propagation(p, MS_PRIVATE);
  1103. }
  1104. list_splice(&tmp_list, kill);
  1105. }
  1106. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
  1107. static int do_umount(struct vfsmount *mnt, int flags)
  1108. {
  1109. struct super_block *sb = mnt->mnt_sb;
  1110. int retval;
  1111. LIST_HEAD(umount_list);
  1112. retval = security_sb_umount(mnt, flags);
  1113. if (retval)
  1114. return retval;
  1115. /*
  1116. * Allow userspace to request a mountpoint be expired rather than
  1117. * unmounting unconditionally. Unmount only happens if:
  1118. * (1) the mark is already set (the mark is cleared by mntput())
  1119. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1120. */
  1121. if (flags & MNT_EXPIRE) {
  1122. if (mnt == current->fs->root.mnt ||
  1123. flags & (MNT_FORCE | MNT_DETACH))
  1124. return -EINVAL;
  1125. /*
  1126. * probably don't strictly need the lock here if we examined
  1127. * all race cases, but it's a slowpath.
  1128. */
  1129. br_write_lock(vfsmount_lock);
  1130. if (mnt_get_count(mnt) != 2) {
  1131. br_write_unlock(vfsmount_lock);
  1132. return -EBUSY;
  1133. }
  1134. br_write_unlock(vfsmount_lock);
  1135. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1136. return -EAGAIN;
  1137. }
  1138. /*
  1139. * If we may have to abort operations to get out of this
  1140. * mount, and they will themselves hold resources we must
  1141. * allow the fs to do things. In the Unix tradition of
  1142. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1143. * might fail to complete on the first run through as other tasks
  1144. * must return, and the like. Thats for the mount program to worry
  1145. * about for the moment.
  1146. */
  1147. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1148. sb->s_op->umount_begin(sb);
  1149. }
  1150. /*
  1151. * No sense to grab the lock for this test, but test itself looks
  1152. * somewhat bogus. Suggestions for better replacement?
  1153. * Ho-hum... In principle, we might treat that as umount + switch
  1154. * to rootfs. GC would eventually take care of the old vfsmount.
  1155. * Actually it makes sense, especially if rootfs would contain a
  1156. * /reboot - static binary that would close all descriptors and
  1157. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1158. */
  1159. if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1160. /*
  1161. * Special case for "unmounting" root ...
  1162. * we just try to remount it readonly.
  1163. */
  1164. down_write(&sb->s_umount);
  1165. if (!(sb->s_flags & MS_RDONLY))
  1166. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1167. up_write(&sb->s_umount);
  1168. return retval;
  1169. }
  1170. down_write(&namespace_sem);
  1171. br_write_lock(vfsmount_lock);
  1172. event++;
  1173. if (!(flags & MNT_DETACH))
  1174. shrink_submounts(mnt, &umount_list);
  1175. retval = -EBUSY;
  1176. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  1177. if (!list_empty(&mnt->mnt_list))
  1178. umount_tree(mnt, 1, &umount_list);
  1179. retval = 0;
  1180. }
  1181. br_write_unlock(vfsmount_lock);
  1182. up_write(&namespace_sem);
  1183. release_mounts(&umount_list);
  1184. return retval;
  1185. }
  1186. /*
  1187. * Now umount can handle mount points as well as block devices.
  1188. * This is important for filesystems which use unnamed block devices.
  1189. *
  1190. * We now support a flag for forced unmount like the other 'big iron'
  1191. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1192. */
  1193. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1194. {
  1195. struct path path;
  1196. int retval;
  1197. int lookup_flags = 0;
  1198. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1199. return -EINVAL;
  1200. if (!(flags & UMOUNT_NOFOLLOW))
  1201. lookup_flags |= LOOKUP_FOLLOW;
  1202. retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
  1203. if (retval)
  1204. goto out;
  1205. retval = -EINVAL;
  1206. if (path.dentry != path.mnt->mnt_root)
  1207. goto dput_and_out;
  1208. if (!check_mnt(path.mnt))
  1209. goto dput_and_out;
  1210. retval = -EPERM;
  1211. if (!capable(CAP_SYS_ADMIN))
  1212. goto dput_and_out;
  1213. retval = do_umount(path.mnt, flags);
  1214. dput_and_out:
  1215. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1216. dput(path.dentry);
  1217. mntput_no_expire(path.mnt);
  1218. out:
  1219. return retval;
  1220. }
  1221. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1222. /*
  1223. * The 2.0 compatible umount. No flags.
  1224. */
  1225. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1226. {
  1227. return sys_umount(name, 0);
  1228. }
  1229. #endif
  1230. static int mount_is_safe(struct path *path)
  1231. {
  1232. if (capable(CAP_SYS_ADMIN))
  1233. return 0;
  1234. return -EPERM;
  1235. #ifdef notyet
  1236. if (S_ISLNK(path->dentry->d_inode->i_mode))
  1237. return -EPERM;
  1238. if (path->dentry->d_inode->i_mode & S_ISVTX) {
  1239. if (current_uid() != path->dentry->d_inode->i_uid)
  1240. return -EPERM;
  1241. }
  1242. if (inode_permission(path->dentry->d_inode, MAY_WRITE))
  1243. return -EPERM;
  1244. return 0;
  1245. #endif
  1246. }
  1247. struct mount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
  1248. int flag)
  1249. {
  1250. struct mount *res, *q;
  1251. struct vfsmount *p, *r;
  1252. struct path path;
  1253. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  1254. return NULL;
  1255. res = q = clone_mnt(mnt, dentry, flag);
  1256. if (!q)
  1257. goto Enomem;
  1258. q->mnt.mnt_mountpoint = mnt->mnt_mountpoint;
  1259. p = mnt;
  1260. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1261. struct mount *s;
  1262. if (!is_subdir(r->mnt_mountpoint, dentry))
  1263. continue;
  1264. for (s = real_mount(r); s; s = next_mnt(s, r)) {
  1265. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(&s->mnt)) {
  1266. s = skip_mnt_tree(s);
  1267. continue;
  1268. }
  1269. while (p != s->mnt.mnt_parent) {
  1270. p = p->mnt_parent;
  1271. q = real_mount(q->mnt.mnt_parent);
  1272. }
  1273. p = &s->mnt;
  1274. path.mnt = &q->mnt;
  1275. path.dentry = p->mnt_mountpoint;
  1276. q = clone_mnt(p, p->mnt_root, flag);
  1277. if (!q)
  1278. goto Enomem;
  1279. br_write_lock(vfsmount_lock);
  1280. list_add_tail(&q->mnt.mnt_list, &res->mnt.mnt_list);
  1281. attach_mnt(q, &path);
  1282. br_write_unlock(vfsmount_lock);
  1283. }
  1284. }
  1285. return res;
  1286. Enomem:
  1287. if (res) {
  1288. LIST_HEAD(umount_list);
  1289. br_write_lock(vfsmount_lock);
  1290. umount_tree(&res->mnt, 0, &umount_list);
  1291. br_write_unlock(vfsmount_lock);
  1292. release_mounts(&umount_list);
  1293. }
  1294. return NULL;
  1295. }
  1296. struct vfsmount *collect_mounts(struct path *path)
  1297. {
  1298. struct mount *tree;
  1299. down_write(&namespace_sem);
  1300. tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
  1301. up_write(&namespace_sem);
  1302. return tree ? &tree->mnt : NULL;
  1303. }
  1304. void drop_collected_mounts(struct vfsmount *mnt)
  1305. {
  1306. LIST_HEAD(umount_list);
  1307. down_write(&namespace_sem);
  1308. br_write_lock(vfsmount_lock);
  1309. umount_tree(mnt, 0, &umount_list);
  1310. br_write_unlock(vfsmount_lock);
  1311. up_write(&namespace_sem);
  1312. release_mounts(&umount_list);
  1313. }
  1314. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1315. struct vfsmount *root)
  1316. {
  1317. struct vfsmount *mnt;
  1318. int res = f(root, arg);
  1319. if (res)
  1320. return res;
  1321. list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
  1322. res = f(mnt, arg);
  1323. if (res)
  1324. return res;
  1325. }
  1326. return 0;
  1327. }
  1328. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1329. {
  1330. struct mount *p;
  1331. for (p = mnt; p != end; p = next_mnt(p, &mnt->mnt)) {
  1332. if (p->mnt.mnt_group_id && !IS_MNT_SHARED(&p->mnt))
  1333. mnt_release_group_id(p);
  1334. }
  1335. }
  1336. static int invent_group_ids(struct mount *mnt, bool recurse)
  1337. {
  1338. struct mount *p;
  1339. for (p = mnt; p; p = recurse ? next_mnt(p, &mnt->mnt) : NULL) {
  1340. if (!p->mnt.mnt_group_id && !IS_MNT_SHARED(&p->mnt)) {
  1341. int err = mnt_alloc_group_id(p);
  1342. if (err) {
  1343. cleanup_group_ids(mnt, p);
  1344. return err;
  1345. }
  1346. }
  1347. }
  1348. return 0;
  1349. }
  1350. /*
  1351. * @source_mnt : mount tree to be attached
  1352. * @nd : place the mount tree @source_mnt is attached
  1353. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1354. * store the parent mount and mountpoint dentry.
  1355. * (done when source_mnt is moved)
  1356. *
  1357. * NOTE: in the table below explains the semantics when a source mount
  1358. * of a given type is attached to a destination mount of a given type.
  1359. * ---------------------------------------------------------------------------
  1360. * | BIND MOUNT OPERATION |
  1361. * |**************************************************************************
  1362. * | source-->| shared | private | slave | unbindable |
  1363. * | dest | | | | |
  1364. * | | | | | | |
  1365. * | v | | | | |
  1366. * |**************************************************************************
  1367. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1368. * | | | | | |
  1369. * |non-shared| shared (+) | private | slave (*) | invalid |
  1370. * ***************************************************************************
  1371. * A bind operation clones the source mount and mounts the clone on the
  1372. * destination mount.
  1373. *
  1374. * (++) the cloned mount is propagated to all the mounts in the propagation
  1375. * tree of the destination mount and the cloned mount is added to
  1376. * the peer group of the source mount.
  1377. * (+) the cloned mount is created under the destination mount and is marked
  1378. * as shared. The cloned mount is added to the peer group of the source
  1379. * mount.
  1380. * (+++) the mount is propagated to all the mounts in the propagation tree
  1381. * of the destination mount and the cloned mount is made slave
  1382. * of the same master as that of the source mount. The cloned mount
  1383. * is marked as 'shared and slave'.
  1384. * (*) the cloned mount is made a slave of the same master as that of the
  1385. * source mount.
  1386. *
  1387. * ---------------------------------------------------------------------------
  1388. * | MOVE MOUNT OPERATION |
  1389. * |**************************************************************************
  1390. * | source-->| shared | private | slave | unbindable |
  1391. * | dest | | | | |
  1392. * | | | | | | |
  1393. * | v | | | | |
  1394. * |**************************************************************************
  1395. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1396. * | | | | | |
  1397. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1398. * ***************************************************************************
  1399. *
  1400. * (+) the mount is moved to the destination. And is then propagated to
  1401. * all the mounts in the propagation tree of the destination mount.
  1402. * (+*) the mount is moved to the destination.
  1403. * (+++) the mount is moved to the destination and is then propagated to
  1404. * all the mounts belonging to the destination mount's propagation tree.
  1405. * the mount is marked as 'shared and slave'.
  1406. * (*) the mount continues to be a slave at the new location.
  1407. *
  1408. * if the source mount is a tree, the operations explained above is
  1409. * applied to each mount in the tree.
  1410. * Must be called without spinlocks held, since this function can sleep
  1411. * in allocations.
  1412. */
  1413. static int attach_recursive_mnt(struct mount *source_mnt,
  1414. struct path *path, struct path *parent_path)
  1415. {
  1416. LIST_HEAD(tree_list);
  1417. struct vfsmount *dest_mnt = path->mnt;
  1418. struct dentry *dest_dentry = path->dentry;
  1419. struct mount *child, *p;
  1420. int err;
  1421. if (IS_MNT_SHARED(dest_mnt)) {
  1422. err = invent_group_ids(source_mnt, true);
  1423. if (err)
  1424. goto out;
  1425. }
  1426. err = propagate_mnt(dest_mnt, dest_dentry, &source_mnt->mnt, &tree_list);
  1427. if (err)
  1428. goto out_cleanup_ids;
  1429. br_write_lock(vfsmount_lock);
  1430. if (IS_MNT_SHARED(dest_mnt)) {
  1431. for (p = source_mnt; p; p = next_mnt(p, &source_mnt->mnt))
  1432. set_mnt_shared(p);
  1433. }
  1434. if (parent_path) {
  1435. detach_mnt(source_mnt, parent_path);
  1436. attach_mnt(source_mnt, path);
  1437. touch_mnt_namespace(parent_path->mnt->mnt_ns);
  1438. } else {
  1439. mnt_set_mountpoint(dest_mnt, dest_dentry, &source_mnt->mnt);
  1440. commit_tree(source_mnt);
  1441. }
  1442. list_for_each_entry_safe(child, p, &tree_list, mnt.mnt_hash) {
  1443. list_del_init(&child->mnt.mnt_hash);
  1444. commit_tree(child);
  1445. }
  1446. br_write_unlock(vfsmount_lock);
  1447. return 0;
  1448. out_cleanup_ids:
  1449. if (IS_MNT_SHARED(dest_mnt))
  1450. cleanup_group_ids(source_mnt, NULL);
  1451. out:
  1452. return err;
  1453. }
  1454. static int lock_mount(struct path *path)
  1455. {
  1456. struct vfsmount *mnt;
  1457. retry:
  1458. mutex_lock(&path->dentry->d_inode->i_mutex);
  1459. if (unlikely(cant_mount(path->dentry))) {
  1460. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1461. return -ENOENT;
  1462. }
  1463. down_write(&namespace_sem);
  1464. mnt = lookup_mnt(path);
  1465. if (likely(!mnt))
  1466. return 0;
  1467. up_write(&namespace_sem);
  1468. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1469. path_put(path);
  1470. path->mnt = mnt;
  1471. path->dentry = dget(mnt->mnt_root);
  1472. goto retry;
  1473. }
  1474. static void unlock_mount(struct path *path)
  1475. {
  1476. up_write(&namespace_sem);
  1477. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1478. }
  1479. static int graft_tree(struct vfsmount *mnt, struct path *path)
  1480. {
  1481. if (mnt->mnt_sb->s_flags & MS_NOUSER)
  1482. return -EINVAL;
  1483. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1484. S_ISDIR(mnt->mnt_root->d_inode->i_mode))
  1485. return -ENOTDIR;
  1486. if (d_unlinked(path->dentry))
  1487. return -ENOENT;
  1488. return attach_recursive_mnt(real_mount(mnt), path, NULL);
  1489. }
  1490. /*
  1491. * Sanity check the flags to change_mnt_propagation.
  1492. */
  1493. static int flags_to_propagation_type(int flags)
  1494. {
  1495. int type = flags & ~(MS_REC | MS_SILENT);
  1496. /* Fail if any non-propagation flags are set */
  1497. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1498. return 0;
  1499. /* Only one propagation flag should be set */
  1500. if (!is_power_of_2(type))
  1501. return 0;
  1502. return type;
  1503. }
  1504. /*
  1505. * recursively change the type of the mountpoint.
  1506. */
  1507. static int do_change_type(struct path *path, int flag)
  1508. {
  1509. struct mount *m;
  1510. struct mount *mnt = real_mount(path->mnt);
  1511. int recurse = flag & MS_REC;
  1512. int type;
  1513. int err = 0;
  1514. if (!capable(CAP_SYS_ADMIN))
  1515. return -EPERM;
  1516. if (path->dentry != path->mnt->mnt_root)
  1517. return -EINVAL;
  1518. type = flags_to_propagation_type(flag);
  1519. if (!type)
  1520. return -EINVAL;
  1521. down_write(&namespace_sem);
  1522. if (type == MS_SHARED) {
  1523. err = invent_group_ids(mnt, recurse);
  1524. if (err)
  1525. goto out_unlock;
  1526. }
  1527. br_write_lock(vfsmount_lock);
  1528. for (m = mnt; m; m = (recurse ? next_mnt(m, &mnt->mnt) : NULL))
  1529. change_mnt_propagation(m, type);
  1530. br_write_unlock(vfsmount_lock);
  1531. out_unlock:
  1532. up_write(&namespace_sem);
  1533. return err;
  1534. }
  1535. /*
  1536. * do loopback mount.
  1537. */
  1538. static int do_loopback(struct path *path, char *old_name,
  1539. int recurse)
  1540. {
  1541. LIST_HEAD(umount_list);
  1542. struct path old_path;
  1543. struct mount *mnt = NULL;
  1544. int err = mount_is_safe(path);
  1545. if (err)
  1546. return err;
  1547. if (!old_name || !*old_name)
  1548. return -EINVAL;
  1549. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1550. if (err)
  1551. return err;
  1552. err = lock_mount(path);
  1553. if (err)
  1554. goto out;
  1555. err = -EINVAL;
  1556. if (IS_MNT_UNBINDABLE(old_path.mnt))
  1557. goto out2;
  1558. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1559. goto out2;
  1560. err = -ENOMEM;
  1561. if (recurse)
  1562. mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
  1563. else
  1564. mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
  1565. if (!mnt)
  1566. goto out2;
  1567. err = graft_tree(&mnt->mnt, path);
  1568. if (err) {
  1569. br_write_lock(vfsmount_lock);
  1570. umount_tree(&mnt->mnt, 0, &umount_list);
  1571. br_write_unlock(vfsmount_lock);
  1572. }
  1573. out2:
  1574. unlock_mount(path);
  1575. release_mounts(&umount_list);
  1576. out:
  1577. path_put(&old_path);
  1578. return err;
  1579. }
  1580. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1581. {
  1582. int error = 0;
  1583. int readonly_request = 0;
  1584. if (ms_flags & MS_RDONLY)
  1585. readonly_request = 1;
  1586. if (readonly_request == __mnt_is_readonly(mnt))
  1587. return 0;
  1588. if (readonly_request)
  1589. error = mnt_make_readonly(mnt);
  1590. else
  1591. __mnt_unmake_readonly(mnt);
  1592. return error;
  1593. }
  1594. /*
  1595. * change filesystem flags. dir should be a physical root of filesystem.
  1596. * If you've mounted a non-root directory somewhere and want to do remount
  1597. * on it - tough luck.
  1598. */
  1599. static int do_remount(struct path *path, int flags, int mnt_flags,
  1600. void *data)
  1601. {
  1602. int err;
  1603. struct super_block *sb = path->mnt->mnt_sb;
  1604. if (!capable(CAP_SYS_ADMIN))
  1605. return -EPERM;
  1606. if (!check_mnt(path->mnt))
  1607. return -EINVAL;
  1608. if (path->dentry != path->mnt->mnt_root)
  1609. return -EINVAL;
  1610. err = security_sb_remount(sb, data);
  1611. if (err)
  1612. return err;
  1613. down_write(&sb->s_umount);
  1614. if (flags & MS_BIND)
  1615. err = change_mount_flags(path->mnt, flags);
  1616. else
  1617. err = do_remount_sb(sb, flags, data, 0);
  1618. if (!err) {
  1619. br_write_lock(vfsmount_lock);
  1620. mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
  1621. path->mnt->mnt_flags = mnt_flags;
  1622. br_write_unlock(vfsmount_lock);
  1623. }
  1624. up_write(&sb->s_umount);
  1625. if (!err) {
  1626. br_write_lock(vfsmount_lock);
  1627. touch_mnt_namespace(path->mnt->mnt_ns);
  1628. br_write_unlock(vfsmount_lock);
  1629. }
  1630. return err;
  1631. }
  1632. static inline int tree_contains_unbindable(struct mount *mnt)
  1633. {
  1634. struct mount *p;
  1635. for (p = mnt; p; p = next_mnt(p, &mnt->mnt)) {
  1636. if (IS_MNT_UNBINDABLE(&p->mnt))
  1637. return 1;
  1638. }
  1639. return 0;
  1640. }
  1641. static int do_move_mount(struct path *path, char *old_name)
  1642. {
  1643. struct path old_path, parent_path;
  1644. struct vfsmount *p;
  1645. struct mount *old;
  1646. int err = 0;
  1647. if (!capable(CAP_SYS_ADMIN))
  1648. return -EPERM;
  1649. if (!old_name || !*old_name)
  1650. return -EINVAL;
  1651. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1652. if (err)
  1653. return err;
  1654. err = lock_mount(path);
  1655. if (err < 0)
  1656. goto out;
  1657. err = -EINVAL;
  1658. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1659. goto out1;
  1660. if (d_unlinked(path->dentry))
  1661. goto out1;
  1662. err = -EINVAL;
  1663. if (old_path.dentry != old_path.mnt->mnt_root)
  1664. goto out1;
  1665. old = real_mount(old_path.mnt);
  1666. if (!mnt_has_parent(old_path.mnt))
  1667. goto out1;
  1668. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1669. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1670. goto out1;
  1671. /*
  1672. * Don't move a mount residing in a shared parent.
  1673. */
  1674. if (IS_MNT_SHARED(old_path.mnt->mnt_parent))
  1675. goto out1;
  1676. /*
  1677. * Don't move a mount tree containing unbindable mounts to a destination
  1678. * mount which is shared.
  1679. */
  1680. if (IS_MNT_SHARED(path->mnt) &&
  1681. tree_contains_unbindable(old))
  1682. goto out1;
  1683. err = -ELOOP;
  1684. for (p = path->mnt; mnt_has_parent(p); p = p->mnt_parent)
  1685. if (p == old_path.mnt)
  1686. goto out1;
  1687. err = attach_recursive_mnt(old, path, &parent_path);
  1688. if (err)
  1689. goto out1;
  1690. /* if the mount is moved, it should no longer be expire
  1691. * automatically */
  1692. list_del_init(&old_path.mnt->mnt_expire);
  1693. out1:
  1694. unlock_mount(path);
  1695. out:
  1696. if (!err)
  1697. path_put(&parent_path);
  1698. path_put(&old_path);
  1699. return err;
  1700. }
  1701. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  1702. {
  1703. int err;
  1704. const char *subtype = strchr(fstype, '.');
  1705. if (subtype) {
  1706. subtype++;
  1707. err = -EINVAL;
  1708. if (!subtype[0])
  1709. goto err;
  1710. } else
  1711. subtype = "";
  1712. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  1713. err = -ENOMEM;
  1714. if (!mnt->mnt_sb->s_subtype)
  1715. goto err;
  1716. return mnt;
  1717. err:
  1718. mntput(mnt);
  1719. return ERR_PTR(err);
  1720. }
  1721. static struct vfsmount *
  1722. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1723. {
  1724. struct file_system_type *type = get_fs_type(fstype);
  1725. struct vfsmount *mnt;
  1726. if (!type)
  1727. return ERR_PTR(-ENODEV);
  1728. mnt = vfs_kern_mount(type, flags, name, data);
  1729. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1730. !mnt->mnt_sb->s_subtype)
  1731. mnt = fs_set_subtype(mnt, fstype);
  1732. put_filesystem(type);
  1733. return mnt;
  1734. }
  1735. /*
  1736. * add a mount into a namespace's mount tree
  1737. */
  1738. static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
  1739. {
  1740. int err;
  1741. mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
  1742. err = lock_mount(path);
  1743. if (err)
  1744. return err;
  1745. err = -EINVAL;
  1746. if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
  1747. goto unlock;
  1748. /* Refuse the same filesystem on the same mount point */
  1749. err = -EBUSY;
  1750. if (path->mnt->mnt_sb == newmnt->mnt_sb &&
  1751. path->mnt->mnt_root == path->dentry)
  1752. goto unlock;
  1753. err = -EINVAL;
  1754. if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
  1755. goto unlock;
  1756. newmnt->mnt_flags = mnt_flags;
  1757. err = graft_tree(newmnt, path);
  1758. unlock:
  1759. unlock_mount(path);
  1760. return err;
  1761. }
  1762. /*
  1763. * create a new mount for userspace and request it to be added into the
  1764. * namespace's tree
  1765. */
  1766. static int do_new_mount(struct path *path, char *type, int flags,
  1767. int mnt_flags, char *name, void *data)
  1768. {
  1769. struct vfsmount *mnt;
  1770. int err;
  1771. if (!type)
  1772. return -EINVAL;
  1773. /* we need capabilities... */
  1774. if (!capable(CAP_SYS_ADMIN))
  1775. return -EPERM;
  1776. mnt = do_kern_mount(type, flags, name, data);
  1777. if (IS_ERR(mnt))
  1778. return PTR_ERR(mnt);
  1779. err = do_add_mount(mnt, path, mnt_flags);
  1780. if (err)
  1781. mntput(mnt);
  1782. return err;
  1783. }
  1784. int finish_automount(struct vfsmount *m, struct path *path)
  1785. {
  1786. int err;
  1787. /* The new mount record should have at least 2 refs to prevent it being
  1788. * expired before we get a chance to add it
  1789. */
  1790. BUG_ON(mnt_get_count(m) < 2);
  1791. if (m->mnt_sb == path->mnt->mnt_sb &&
  1792. m->mnt_root == path->dentry) {
  1793. err = -ELOOP;
  1794. goto fail;
  1795. }
  1796. err = do_add_mount(m, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  1797. if (!err)
  1798. return 0;
  1799. fail:
  1800. /* remove m from any expiration list it may be on */
  1801. if (!list_empty(&m->mnt_expire)) {
  1802. down_write(&namespace_sem);
  1803. br_write_lock(vfsmount_lock);
  1804. list_del_init(&m->mnt_expire);
  1805. br_write_unlock(vfsmount_lock);
  1806. up_write(&namespace_sem);
  1807. }
  1808. mntput(m);
  1809. mntput(m);
  1810. return err;
  1811. }
  1812. /**
  1813. * mnt_set_expiry - Put a mount on an expiration list
  1814. * @mnt: The mount to list.
  1815. * @expiry_list: The list to add the mount to.
  1816. */
  1817. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  1818. {
  1819. down_write(&namespace_sem);
  1820. br_write_lock(vfsmount_lock);
  1821. list_add_tail(&mnt->mnt_expire, expiry_list);
  1822. br_write_unlock(vfsmount_lock);
  1823. up_write(&namespace_sem);
  1824. }
  1825. EXPORT_SYMBOL(mnt_set_expiry);
  1826. /*
  1827. * process a list of expirable mountpoints with the intent of discarding any
  1828. * mountpoints that aren't in use and haven't been touched since last we came
  1829. * here
  1830. */
  1831. void mark_mounts_for_expiry(struct list_head *mounts)
  1832. {
  1833. struct vfsmount *mnt, *next;
  1834. LIST_HEAD(graveyard);
  1835. LIST_HEAD(umounts);
  1836. if (list_empty(mounts))
  1837. return;
  1838. down_write(&namespace_sem);
  1839. br_write_lock(vfsmount_lock);
  1840. /* extract from the expiration list every vfsmount that matches the
  1841. * following criteria:
  1842. * - only referenced by its parent vfsmount
  1843. * - still marked for expiry (marked on the last call here; marks are
  1844. * cleared by mntput())
  1845. */
  1846. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1847. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1848. propagate_mount_busy(mnt, 1))
  1849. continue;
  1850. list_move(&mnt->mnt_expire, &graveyard);
  1851. }
  1852. while (!list_empty(&graveyard)) {
  1853. mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
  1854. touch_mnt_namespace(mnt->mnt_ns);
  1855. umount_tree(mnt, 1, &umounts);
  1856. }
  1857. br_write_unlock(vfsmount_lock);
  1858. up_write(&namespace_sem);
  1859. release_mounts(&umounts);
  1860. }
  1861. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1862. /*
  1863. * Ripoff of 'select_parent()'
  1864. *
  1865. * search the list of submounts for a given mountpoint, and move any
  1866. * shrinkable submounts to the 'graveyard' list.
  1867. */
  1868. static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
  1869. {
  1870. struct vfsmount *this_parent = parent;
  1871. struct list_head *next;
  1872. int found = 0;
  1873. repeat:
  1874. next = this_parent->mnt_mounts.next;
  1875. resume:
  1876. while (next != &this_parent->mnt_mounts) {
  1877. struct list_head *tmp = next;
  1878. struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
  1879. next = tmp->next;
  1880. if (!(mnt->mnt_flags & MNT_SHRINKABLE))
  1881. continue;
  1882. /*
  1883. * Descend a level if the d_mounts list is non-empty.
  1884. */
  1885. if (!list_empty(&mnt->mnt_mounts)) {
  1886. this_parent = mnt;
  1887. goto repeat;
  1888. }
  1889. if (!propagate_mount_busy(mnt, 1)) {
  1890. list_move_tail(&mnt->mnt_expire, graveyard);
  1891. found++;
  1892. }
  1893. }
  1894. /*
  1895. * All done at this level ... ascend and resume the search
  1896. */
  1897. if (this_parent != parent) {
  1898. next = this_parent->mnt_child.next;
  1899. this_parent = this_parent->mnt_parent;
  1900. goto resume;
  1901. }
  1902. return found;
  1903. }
  1904. /*
  1905. * process a list of expirable mountpoints with the intent of discarding any
  1906. * submounts of a specific parent mountpoint
  1907. *
  1908. * vfsmount_lock must be held for write
  1909. */
  1910. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
  1911. {
  1912. LIST_HEAD(graveyard);
  1913. struct vfsmount *m;
  1914. /* extract submounts of 'mountpoint' from the expiration list */
  1915. while (select_submounts(mnt, &graveyard)) {
  1916. while (!list_empty(&graveyard)) {
  1917. m = list_first_entry(&graveyard, struct vfsmount,
  1918. mnt_expire);
  1919. touch_mnt_namespace(m->mnt_ns);
  1920. umount_tree(m, 1, umounts);
  1921. }
  1922. }
  1923. }
  1924. /*
  1925. * Some copy_from_user() implementations do not return the exact number of
  1926. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1927. * Note that this function differs from copy_from_user() in that it will oops
  1928. * on bad values of `to', rather than returning a short copy.
  1929. */
  1930. static long exact_copy_from_user(void *to, const void __user * from,
  1931. unsigned long n)
  1932. {
  1933. char *t = to;
  1934. const char __user *f = from;
  1935. char c;
  1936. if (!access_ok(VERIFY_READ, from, n))
  1937. return n;
  1938. while (n) {
  1939. if (__get_user(c, f)) {
  1940. memset(t, 0, n);
  1941. break;
  1942. }
  1943. *t++ = c;
  1944. f++;
  1945. n--;
  1946. }
  1947. return n;
  1948. }
  1949. int copy_mount_options(const void __user * data, unsigned long *where)
  1950. {
  1951. int i;
  1952. unsigned long page;
  1953. unsigned long size;
  1954. *where = 0;
  1955. if (!data)
  1956. return 0;
  1957. if (!(page = __get_free_page(GFP_KERNEL)))
  1958. return -ENOMEM;
  1959. /* We only care that *some* data at the address the user
  1960. * gave us is valid. Just in case, we'll zero
  1961. * the remainder of the page.
  1962. */
  1963. /* copy_from_user cannot cross TASK_SIZE ! */
  1964. size = TASK_SIZE - (unsigned long)data;
  1965. if (size > PAGE_SIZE)
  1966. size = PAGE_SIZE;
  1967. i = size - exact_copy_from_user((void *)page, data, size);
  1968. if (!i) {
  1969. free_page(page);
  1970. return -EFAULT;
  1971. }
  1972. if (i != PAGE_SIZE)
  1973. memset((char *)page + i, 0, PAGE_SIZE - i);
  1974. *where = page;
  1975. return 0;
  1976. }
  1977. int copy_mount_string(const void __user *data, char **where)
  1978. {
  1979. char *tmp;
  1980. if (!data) {
  1981. *where = NULL;
  1982. return 0;
  1983. }
  1984. tmp = strndup_user(data, PAGE_SIZE);
  1985. if (IS_ERR(tmp))
  1986. return PTR_ERR(tmp);
  1987. *where = tmp;
  1988. return 0;
  1989. }
  1990. /*
  1991. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1992. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1993. *
  1994. * data is a (void *) that can point to any structure up to
  1995. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1996. * information (or be NULL).
  1997. *
  1998. * Pre-0.97 versions of mount() didn't have a flags word.
  1999. * When the flags word was introduced its top half was required
  2000. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  2001. * Therefore, if this magic number is present, it carries no information
  2002. * and must be discarded.
  2003. */
  2004. long do_mount(char *dev_name, char *dir_name, char *type_page,
  2005. unsigned long flags, void *data_page)
  2006. {
  2007. struct path path;
  2008. int retval = 0;
  2009. int mnt_flags = 0;
  2010. /* Discard magic */
  2011. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  2012. flags &= ~MS_MGC_MSK;
  2013. /* Basic sanity checks */
  2014. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  2015. return -EINVAL;
  2016. if (data_page)
  2017. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  2018. /* ... and get the mountpoint */
  2019. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  2020. if (retval)
  2021. return retval;
  2022. retval = security_sb_mount(dev_name, &path,
  2023. type_page, flags, data_page);
  2024. if (retval)
  2025. goto dput_out;
  2026. /* Default to relatime unless overriden */
  2027. if (!(flags & MS_NOATIME))
  2028. mnt_flags |= MNT_RELATIME;
  2029. /* Separate the per-mountpoint flags */
  2030. if (flags & MS_NOSUID)
  2031. mnt_flags |= MNT_NOSUID;
  2032. if (flags & MS_NODEV)
  2033. mnt_flags |= MNT_NODEV;
  2034. if (flags & MS_NOEXEC)
  2035. mnt_flags |= MNT_NOEXEC;
  2036. if (flags & MS_NOATIME)
  2037. mnt_flags |= MNT_NOATIME;
  2038. if (flags & MS_NODIRATIME)
  2039. mnt_flags |= MNT_NODIRATIME;
  2040. if (flags & MS_STRICTATIME)
  2041. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  2042. if (flags & MS_RDONLY)
  2043. mnt_flags |= MNT_READONLY;
  2044. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  2045. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  2046. MS_STRICTATIME);
  2047. if (flags & MS_REMOUNT)
  2048. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  2049. data_page);
  2050. else if (flags & MS_BIND)
  2051. retval = do_loopback(&path, dev_name, flags & MS_REC);
  2052. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2053. retval = do_change_type(&path, flags);
  2054. else if (flags & MS_MOVE)
  2055. retval = do_move_mount(&path, dev_name);
  2056. else
  2057. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  2058. dev_name, data_page);
  2059. dput_out:
  2060. path_put(&path);
  2061. return retval;
  2062. }
  2063. static struct mnt_namespace *alloc_mnt_ns(void)
  2064. {
  2065. struct mnt_namespace *new_ns;
  2066. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  2067. if (!new_ns)
  2068. return ERR_PTR(-ENOMEM);
  2069. atomic_set(&new_ns->count, 1);
  2070. new_ns->root = NULL;
  2071. INIT_LIST_HEAD(&new_ns->list);
  2072. init_waitqueue_head(&new_ns->poll);
  2073. new_ns->event = 0;
  2074. return new_ns;
  2075. }
  2076. void mnt_make_longterm(struct vfsmount *mnt)
  2077. {
  2078. __mnt_make_longterm(mnt);
  2079. }
  2080. void mnt_make_shortterm(struct vfsmount *mnt)
  2081. {
  2082. #ifdef CONFIG_SMP
  2083. if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
  2084. return;
  2085. br_write_lock(vfsmount_lock);
  2086. atomic_dec(&mnt->mnt_longterm);
  2087. br_write_unlock(vfsmount_lock);
  2088. #endif
  2089. }
  2090. /*
  2091. * Allocate a new namespace structure and populate it with contents
  2092. * copied from the namespace of the passed in task structure.
  2093. */
  2094. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  2095. struct fs_struct *fs)
  2096. {
  2097. struct mnt_namespace *new_ns;
  2098. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  2099. struct mount *p, *q;
  2100. struct mount *new;
  2101. new_ns = alloc_mnt_ns();
  2102. if (IS_ERR(new_ns))
  2103. return new_ns;
  2104. down_write(&namespace_sem);
  2105. /* First pass: copy the tree topology */
  2106. new = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
  2107. CL_COPY_ALL | CL_EXPIRE);
  2108. if (!new) {
  2109. up_write(&namespace_sem);
  2110. kfree(new_ns);
  2111. return ERR_PTR(-ENOMEM);
  2112. }
  2113. new_ns->root = &new->mnt;
  2114. br_write_lock(vfsmount_lock);
  2115. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  2116. br_write_unlock(vfsmount_lock);
  2117. /*
  2118. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  2119. * as belonging to new namespace. We have already acquired a private
  2120. * fs_struct, so tsk->fs->lock is not needed.
  2121. */
  2122. p = real_mount(mnt_ns->root);
  2123. q = new;
  2124. while (p) {
  2125. q->mnt.mnt_ns = new_ns;
  2126. __mnt_make_longterm(&q->mnt);
  2127. if (fs) {
  2128. if (&p->mnt == fs->root.mnt) {
  2129. fs->root.mnt = mntget(&q->mnt);
  2130. __mnt_make_longterm(&q->mnt);
  2131. mnt_make_shortterm(&p->mnt);
  2132. rootmnt = &p->mnt;
  2133. }
  2134. if (&p->mnt == fs->pwd.mnt) {
  2135. fs->pwd.mnt = mntget(&q->mnt);
  2136. __mnt_make_longterm(&q->mnt);
  2137. mnt_make_shortterm(&p->mnt);
  2138. pwdmnt = &p->mnt;
  2139. }
  2140. }
  2141. p = next_mnt(p, mnt_ns->root);
  2142. q = next_mnt(q, new_ns->root);
  2143. }
  2144. up_write(&namespace_sem);
  2145. if (rootmnt)
  2146. mntput(rootmnt);
  2147. if (pwdmnt)
  2148. mntput(pwdmnt);
  2149. return new_ns;
  2150. }
  2151. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2152. struct fs_struct *new_fs)
  2153. {
  2154. struct mnt_namespace *new_ns;
  2155. BUG_ON(!ns);
  2156. get_mnt_ns(ns);
  2157. if (!(flags & CLONE_NEWNS))
  2158. return ns;
  2159. new_ns = dup_mnt_ns(ns, new_fs);
  2160. put_mnt_ns(ns);
  2161. return new_ns;
  2162. }
  2163. /**
  2164. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2165. * @mnt: pointer to the new root filesystem mountpoint
  2166. */
  2167. static struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
  2168. {
  2169. struct mnt_namespace *new_ns;
  2170. new_ns = alloc_mnt_ns();
  2171. if (!IS_ERR(new_ns)) {
  2172. mnt->mnt_ns = new_ns;
  2173. __mnt_make_longterm(mnt);
  2174. new_ns->root = mnt;
  2175. list_add(&new_ns->list, &new_ns->root->mnt_list);
  2176. } else {
  2177. mntput(mnt);
  2178. }
  2179. return new_ns;
  2180. }
  2181. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2182. {
  2183. struct mnt_namespace *ns;
  2184. struct super_block *s;
  2185. struct path path;
  2186. int err;
  2187. ns = create_mnt_ns(mnt);
  2188. if (IS_ERR(ns))
  2189. return ERR_CAST(ns);
  2190. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2191. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2192. put_mnt_ns(ns);
  2193. if (err)
  2194. return ERR_PTR(err);
  2195. /* trade a vfsmount reference for active sb one */
  2196. s = path.mnt->mnt_sb;
  2197. atomic_inc(&s->s_active);
  2198. mntput(path.mnt);
  2199. /* lock the sucker */
  2200. down_write(&s->s_umount);
  2201. /* ... and return the root of (sub)tree on it */
  2202. return path.dentry;
  2203. }
  2204. EXPORT_SYMBOL(mount_subtree);
  2205. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2206. char __user *, type, unsigned long, flags, void __user *, data)
  2207. {
  2208. int ret;
  2209. char *kernel_type;
  2210. char *kernel_dir;
  2211. char *kernel_dev;
  2212. unsigned long data_page;
  2213. ret = copy_mount_string(type, &kernel_type);
  2214. if (ret < 0)
  2215. goto out_type;
  2216. kernel_dir = getname(dir_name);
  2217. if (IS_ERR(kernel_dir)) {
  2218. ret = PTR_ERR(kernel_dir);
  2219. goto out_dir;
  2220. }
  2221. ret = copy_mount_string(dev_name, &kernel_dev);
  2222. if (ret < 0)
  2223. goto out_dev;
  2224. ret = copy_mount_options(data, &data_page);
  2225. if (ret < 0)
  2226. goto out_data;
  2227. ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
  2228. (void *) data_page);
  2229. free_page(data_page);
  2230. out_data:
  2231. kfree(kernel_dev);
  2232. out_dev:
  2233. putname(kernel_dir);
  2234. out_dir:
  2235. kfree(kernel_type);
  2236. out_type:
  2237. return ret;
  2238. }
  2239. /*
  2240. * Return true if path is reachable from root
  2241. *
  2242. * namespace_sem or vfsmount_lock is held
  2243. */
  2244. bool is_path_reachable(struct vfsmount *mnt, struct dentry *dentry,
  2245. const struct path *root)
  2246. {
  2247. while (mnt != root->mnt && mnt_has_parent(mnt)) {
  2248. dentry = mnt->mnt_mountpoint;
  2249. mnt = mnt->mnt_parent;
  2250. }
  2251. return mnt == root->mnt && is_subdir(dentry, root->dentry);
  2252. }
  2253. int path_is_under(struct path *path1, struct path *path2)
  2254. {
  2255. int res;
  2256. br_read_lock(vfsmount_lock);
  2257. res = is_path_reachable(path1->mnt, path1->dentry, path2);
  2258. br_read_unlock(vfsmount_lock);
  2259. return res;
  2260. }
  2261. EXPORT_SYMBOL(path_is_under);
  2262. /*
  2263. * pivot_root Semantics:
  2264. * Moves the root file system of the current process to the directory put_old,
  2265. * makes new_root as the new root file system of the current process, and sets
  2266. * root/cwd of all processes which had them on the current root to new_root.
  2267. *
  2268. * Restrictions:
  2269. * The new_root and put_old must be directories, and must not be on the
  2270. * same file system as the current process root. The put_old must be
  2271. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2272. * pointed to by put_old must yield the same directory as new_root. No other
  2273. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2274. *
  2275. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2276. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2277. * in this situation.
  2278. *
  2279. * Notes:
  2280. * - we don't move root/cwd if they are not at the root (reason: if something
  2281. * cared enough to change them, it's probably wrong to force them elsewhere)
  2282. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2283. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2284. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2285. * first.
  2286. */
  2287. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2288. const char __user *, put_old)
  2289. {
  2290. struct path new, old, parent_path, root_parent, root;
  2291. struct mount *new_mnt, *root_mnt;
  2292. int error;
  2293. if (!capable(CAP_SYS_ADMIN))
  2294. return -EPERM;
  2295. error = user_path_dir(new_root, &new);
  2296. if (error)
  2297. goto out0;
  2298. error = user_path_dir(put_old, &old);
  2299. if (error)
  2300. goto out1;
  2301. error = security_sb_pivotroot(&old, &new);
  2302. if (error)
  2303. goto out2;
  2304. get_fs_root(current->fs, &root);
  2305. error = lock_mount(&old);
  2306. if (error)
  2307. goto out3;
  2308. error = -EINVAL;
  2309. new_mnt = real_mount(new.mnt);
  2310. root_mnt = real_mount(root.mnt);
  2311. if (IS_MNT_SHARED(old.mnt) ||
  2312. IS_MNT_SHARED(new.mnt->mnt_parent) ||
  2313. IS_MNT_SHARED(root.mnt->mnt_parent))
  2314. goto out4;
  2315. if (!check_mnt(root.mnt) || !check_mnt(new.mnt))
  2316. goto out4;
  2317. error = -ENOENT;
  2318. if (d_unlinked(new.dentry))
  2319. goto out4;
  2320. if (d_unlinked(old.dentry))
  2321. goto out4;
  2322. error = -EBUSY;
  2323. if (new.mnt == root.mnt ||
  2324. old.mnt == root.mnt)
  2325. goto out4; /* loop, on the same file system */
  2326. error = -EINVAL;
  2327. if (root.mnt->mnt_root != root.dentry)
  2328. goto out4; /* not a mountpoint */
  2329. if (!mnt_has_parent(root.mnt))
  2330. goto out4; /* not attached */
  2331. if (new.mnt->mnt_root != new.dentry)
  2332. goto out4; /* not a mountpoint */
  2333. if (!mnt_has_parent(new.mnt))
  2334. goto out4; /* not attached */
  2335. /* make sure we can reach put_old from new_root */
  2336. if (!is_path_reachable(old.mnt, old.dentry, &new))
  2337. goto out4;
  2338. br_write_lock(vfsmount_lock);
  2339. detach_mnt(new_mnt, &parent_path);
  2340. detach_mnt(root_mnt, &root_parent);
  2341. /* mount old root on put_old */
  2342. attach_mnt(root_mnt, &old);
  2343. /* mount new_root on / */
  2344. attach_mnt(new_mnt, &root_parent);
  2345. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2346. br_write_unlock(vfsmount_lock);
  2347. chroot_fs_refs(&root, &new);
  2348. error = 0;
  2349. out4:
  2350. unlock_mount(&old);
  2351. if (!error) {
  2352. path_put(&root_parent);
  2353. path_put(&parent_path);
  2354. }
  2355. out3:
  2356. path_put(&root);
  2357. out2:
  2358. path_put(&old);
  2359. out1:
  2360. path_put(&new);
  2361. out0:
  2362. return error;
  2363. }
  2364. static void __init init_mount_tree(void)
  2365. {
  2366. struct vfsmount *mnt;
  2367. struct mnt_namespace *ns;
  2368. struct path root;
  2369. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  2370. if (IS_ERR(mnt))
  2371. panic("Can't create rootfs");
  2372. ns = create_mnt_ns(mnt);
  2373. if (IS_ERR(ns))
  2374. panic("Can't allocate initial namespace");
  2375. init_task.nsproxy->mnt_ns = ns;
  2376. get_mnt_ns(ns);
  2377. root.mnt = ns->root;
  2378. root.dentry = ns->root->mnt_root;
  2379. set_fs_pwd(current->fs, &root);
  2380. set_fs_root(current->fs, &root);
  2381. }
  2382. void __init mnt_init(void)
  2383. {
  2384. unsigned u;
  2385. int err;
  2386. init_rwsem(&namespace_sem);
  2387. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2388. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2389. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  2390. if (!mount_hashtable)
  2391. panic("Failed to allocate mount hash table\n");
  2392. printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
  2393. for (u = 0; u < HASH_SIZE; u++)
  2394. INIT_LIST_HEAD(&mount_hashtable[u]);
  2395. br_lock_init(vfsmount_lock);
  2396. err = sysfs_init();
  2397. if (err)
  2398. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2399. __func__, err);
  2400. fs_kobj = kobject_create_and_add("fs", NULL);
  2401. if (!fs_kobj)
  2402. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2403. init_rootfs();
  2404. init_mount_tree();
  2405. }
  2406. void put_mnt_ns(struct mnt_namespace *ns)
  2407. {
  2408. LIST_HEAD(umount_list);
  2409. if (!atomic_dec_and_test(&ns->count))
  2410. return;
  2411. down_write(&namespace_sem);
  2412. br_write_lock(vfsmount_lock);
  2413. umount_tree(ns->root, 0, &umount_list);
  2414. br_write_unlock(vfsmount_lock);
  2415. up_write(&namespace_sem);
  2416. release_mounts(&umount_list);
  2417. kfree(ns);
  2418. }
  2419. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2420. {
  2421. struct vfsmount *mnt;
  2422. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2423. if (!IS_ERR(mnt)) {
  2424. /*
  2425. * it is a longterm mount, don't release mnt until
  2426. * we unmount before file sys is unregistered
  2427. */
  2428. mnt_make_longterm(mnt);
  2429. }
  2430. return mnt;
  2431. }
  2432. EXPORT_SYMBOL_GPL(kern_mount_data);
  2433. void kern_unmount(struct vfsmount *mnt)
  2434. {
  2435. /* release long term mount so mount point can be released */
  2436. if (!IS_ERR_OR_NULL(mnt)) {
  2437. mnt_make_shortterm(mnt);
  2438. mntput(mnt);
  2439. }
  2440. }
  2441. EXPORT_SYMBOL(kern_unmount);
  2442. bool our_mnt(struct vfsmount *mnt)
  2443. {
  2444. return check_mnt(mnt);
  2445. }