namespace.c 57 KB

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