shmem.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/file.h>
  29. #include <linux/mm.h>
  30. #include <linux/export.h>
  31. #include <linux/swap.h>
  32. static struct vfsmount *shm_mnt;
  33. #ifdef CONFIG_SHMEM
  34. /*
  35. * This virtual memory filesystem is heavily based on the ramfs. It
  36. * extends ramfs by the ability to use swap and honor resource limits
  37. * which makes it a completely usable filesystem.
  38. */
  39. #include <linux/xattr.h>
  40. #include <linux/exportfs.h>
  41. #include <linux/posix_acl.h>
  42. #include <linux/generic_acl.h>
  43. #include <linux/mman.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/shmem_fs.h>
  48. #include <linux/writeback.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/pagevec.h>
  51. #include <linux/percpu_counter.h>
  52. #include <linux/splice.h>
  53. #include <linux/security.h>
  54. #include <linux/swapops.h>
  55. #include <linux/mempolicy.h>
  56. #include <linux/namei.h>
  57. #include <linux/ctype.h>
  58. #include <linux/migrate.h>
  59. #include <linux/highmem.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/magic.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/pgtable.h>
  64. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  65. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  66. /* Pretend that each entry is of this size in directory's i_size */
  67. #define BOGO_DIRENT_SIZE 20
  68. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  69. #define SHORT_SYMLINK_LEN 128
  70. struct shmem_xattr {
  71. struct list_head list; /* anchored by shmem_inode_info->xattr_list */
  72. char *name; /* xattr name */
  73. size_t size;
  74. char value[0];
  75. };
  76. /* Flag allocation requirements to shmem_getpage */
  77. enum sgp_type {
  78. SGP_READ, /* don't exceed i_size, don't allocate page */
  79. SGP_CACHE, /* don't exceed i_size, may allocate page */
  80. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  81. SGP_WRITE, /* may exceed i_size, may allocate page */
  82. };
  83. #ifdef CONFIG_TMPFS
  84. static unsigned long shmem_default_max_blocks(void)
  85. {
  86. return totalram_pages / 2;
  87. }
  88. static unsigned long shmem_default_max_inodes(void)
  89. {
  90. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  91. }
  92. #endif
  93. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  94. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  95. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  96. struct page **pagep, enum sgp_type sgp, int *fault_type)
  97. {
  98. return shmem_getpage_gfp(inode, index, pagep, sgp,
  99. mapping_gfp_mask(inode->i_mapping), fault_type);
  100. }
  101. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  102. {
  103. return sb->s_fs_info;
  104. }
  105. /*
  106. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  107. * for shared memory and for shared anonymous (/dev/zero) mappings
  108. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  109. * consistent with the pre-accounting of private mappings ...
  110. */
  111. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  112. {
  113. return (flags & VM_NORESERVE) ?
  114. 0 : security_vm_enough_memory_kern(VM_ACCT(size));
  115. }
  116. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  117. {
  118. if (!(flags & VM_NORESERVE))
  119. vm_unacct_memory(VM_ACCT(size));
  120. }
  121. /*
  122. * ... whereas tmpfs objects are accounted incrementally as
  123. * pages are allocated, in order to allow huge sparse files.
  124. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  125. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  126. */
  127. static inline int shmem_acct_block(unsigned long flags)
  128. {
  129. return (flags & VM_NORESERVE) ?
  130. security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  131. }
  132. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  133. {
  134. if (flags & VM_NORESERVE)
  135. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  136. }
  137. static const struct super_operations shmem_ops;
  138. static const struct address_space_operations shmem_aops;
  139. static const struct file_operations shmem_file_operations;
  140. static const struct inode_operations shmem_inode_operations;
  141. static const struct inode_operations shmem_dir_inode_operations;
  142. static const struct inode_operations shmem_special_inode_operations;
  143. static const struct vm_operations_struct shmem_vm_ops;
  144. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  145. .ra_pages = 0, /* No readahead */
  146. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  147. };
  148. static LIST_HEAD(shmem_swaplist);
  149. static DEFINE_MUTEX(shmem_swaplist_mutex);
  150. static int shmem_reserve_inode(struct super_block *sb)
  151. {
  152. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  153. if (sbinfo->max_inodes) {
  154. spin_lock(&sbinfo->stat_lock);
  155. if (!sbinfo->free_inodes) {
  156. spin_unlock(&sbinfo->stat_lock);
  157. return -ENOSPC;
  158. }
  159. sbinfo->free_inodes--;
  160. spin_unlock(&sbinfo->stat_lock);
  161. }
  162. return 0;
  163. }
  164. static void shmem_free_inode(struct super_block *sb)
  165. {
  166. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  167. if (sbinfo->max_inodes) {
  168. spin_lock(&sbinfo->stat_lock);
  169. sbinfo->free_inodes++;
  170. spin_unlock(&sbinfo->stat_lock);
  171. }
  172. }
  173. /**
  174. * shmem_recalc_inode - recalculate the block usage of an inode
  175. * @inode: inode to recalc
  176. *
  177. * We have to calculate the free blocks since the mm can drop
  178. * undirtied hole pages behind our back.
  179. *
  180. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  181. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  182. *
  183. * It has to be called with the spinlock held.
  184. */
  185. static void shmem_recalc_inode(struct inode *inode)
  186. {
  187. struct shmem_inode_info *info = SHMEM_I(inode);
  188. long freed;
  189. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  190. if (freed > 0) {
  191. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  192. if (sbinfo->max_blocks)
  193. percpu_counter_add(&sbinfo->used_blocks, -freed);
  194. info->alloced -= freed;
  195. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  196. shmem_unacct_blocks(info->flags, freed);
  197. }
  198. }
  199. /*
  200. * Replace item expected in radix tree by a new item, while holding tree lock.
  201. */
  202. static int shmem_radix_tree_replace(struct address_space *mapping,
  203. pgoff_t index, void *expected, void *replacement)
  204. {
  205. void **pslot;
  206. void *item = NULL;
  207. VM_BUG_ON(!expected);
  208. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  209. if (pslot)
  210. item = radix_tree_deref_slot_protected(pslot,
  211. &mapping->tree_lock);
  212. if (item != expected)
  213. return -ENOENT;
  214. if (replacement)
  215. radix_tree_replace_slot(pslot, replacement);
  216. else
  217. radix_tree_delete(&mapping->page_tree, index);
  218. return 0;
  219. }
  220. /*
  221. * Like add_to_page_cache_locked, but error if expected item has gone.
  222. */
  223. static int shmem_add_to_page_cache(struct page *page,
  224. struct address_space *mapping,
  225. pgoff_t index, gfp_t gfp, void *expected)
  226. {
  227. int error = 0;
  228. VM_BUG_ON(!PageLocked(page));
  229. VM_BUG_ON(!PageSwapBacked(page));
  230. if (!expected)
  231. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  232. if (!error) {
  233. page_cache_get(page);
  234. page->mapping = mapping;
  235. page->index = index;
  236. spin_lock_irq(&mapping->tree_lock);
  237. if (!expected)
  238. error = radix_tree_insert(&mapping->page_tree,
  239. index, page);
  240. else
  241. error = shmem_radix_tree_replace(mapping, index,
  242. expected, page);
  243. if (!error) {
  244. mapping->nrpages++;
  245. __inc_zone_page_state(page, NR_FILE_PAGES);
  246. __inc_zone_page_state(page, NR_SHMEM);
  247. spin_unlock_irq(&mapping->tree_lock);
  248. } else {
  249. page->mapping = NULL;
  250. spin_unlock_irq(&mapping->tree_lock);
  251. page_cache_release(page);
  252. }
  253. if (!expected)
  254. radix_tree_preload_end();
  255. }
  256. if (error)
  257. mem_cgroup_uncharge_cache_page(page);
  258. return error;
  259. }
  260. /*
  261. * Like delete_from_page_cache, but substitutes swap for page.
  262. */
  263. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  264. {
  265. struct address_space *mapping = page->mapping;
  266. int error;
  267. spin_lock_irq(&mapping->tree_lock);
  268. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  269. page->mapping = NULL;
  270. mapping->nrpages--;
  271. __dec_zone_page_state(page, NR_FILE_PAGES);
  272. __dec_zone_page_state(page, NR_SHMEM);
  273. spin_unlock_irq(&mapping->tree_lock);
  274. page_cache_release(page);
  275. BUG_ON(error);
  276. }
  277. /*
  278. * Like find_get_pages, but collecting swap entries as well as pages.
  279. */
  280. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  281. pgoff_t start, unsigned int nr_pages,
  282. struct page **pages, pgoff_t *indices)
  283. {
  284. unsigned int i;
  285. unsigned int ret;
  286. unsigned int nr_found;
  287. rcu_read_lock();
  288. restart:
  289. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  290. (void ***)pages, indices, start, nr_pages);
  291. ret = 0;
  292. for (i = 0; i < nr_found; i++) {
  293. struct page *page;
  294. repeat:
  295. page = radix_tree_deref_slot((void **)pages[i]);
  296. if (unlikely(!page))
  297. continue;
  298. if (radix_tree_exception(page)) {
  299. if (radix_tree_deref_retry(page))
  300. goto restart;
  301. /*
  302. * Otherwise, we must be storing a swap entry
  303. * here as an exceptional entry: so return it
  304. * without attempting to raise page count.
  305. */
  306. goto export;
  307. }
  308. if (!page_cache_get_speculative(page))
  309. goto repeat;
  310. /* Has the page moved? */
  311. if (unlikely(page != *((void **)pages[i]))) {
  312. page_cache_release(page);
  313. goto repeat;
  314. }
  315. export:
  316. indices[ret] = indices[i];
  317. pages[ret] = page;
  318. ret++;
  319. }
  320. if (unlikely(!ret && nr_found))
  321. goto restart;
  322. rcu_read_unlock();
  323. return ret;
  324. }
  325. /*
  326. * Remove swap entry from radix tree, free the swap and its page cache.
  327. */
  328. static int shmem_free_swap(struct address_space *mapping,
  329. pgoff_t index, void *radswap)
  330. {
  331. int error;
  332. spin_lock_irq(&mapping->tree_lock);
  333. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  334. spin_unlock_irq(&mapping->tree_lock);
  335. if (!error)
  336. free_swap_and_cache(radix_to_swp_entry(radswap));
  337. return error;
  338. }
  339. /*
  340. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  341. */
  342. static void shmem_pagevec_release(struct pagevec *pvec)
  343. {
  344. int i, j;
  345. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  346. struct page *page = pvec->pages[i];
  347. if (!radix_tree_exceptional_entry(page))
  348. pvec->pages[j++] = page;
  349. }
  350. pvec->nr = j;
  351. pagevec_release(pvec);
  352. }
  353. /*
  354. * Remove range of pages and swap entries from radix tree, and free them.
  355. */
  356. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  357. {
  358. struct address_space *mapping = inode->i_mapping;
  359. struct shmem_inode_info *info = SHMEM_I(inode);
  360. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  361. unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  362. pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
  363. struct pagevec pvec;
  364. pgoff_t indices[PAGEVEC_SIZE];
  365. long nr_swaps_freed = 0;
  366. pgoff_t index;
  367. int i;
  368. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  369. pagevec_init(&pvec, 0);
  370. index = start;
  371. while (index <= end) {
  372. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  373. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  374. pvec.pages, indices);
  375. if (!pvec.nr)
  376. break;
  377. mem_cgroup_uncharge_start();
  378. for (i = 0; i < pagevec_count(&pvec); i++) {
  379. struct page *page = pvec.pages[i];
  380. index = indices[i];
  381. if (index > end)
  382. break;
  383. if (radix_tree_exceptional_entry(page)) {
  384. nr_swaps_freed += !shmem_free_swap(mapping,
  385. index, page);
  386. continue;
  387. }
  388. if (!trylock_page(page))
  389. continue;
  390. if (page->mapping == mapping) {
  391. VM_BUG_ON(PageWriteback(page));
  392. truncate_inode_page(mapping, page);
  393. }
  394. unlock_page(page);
  395. }
  396. shmem_pagevec_release(&pvec);
  397. mem_cgroup_uncharge_end();
  398. cond_resched();
  399. index++;
  400. }
  401. if (partial) {
  402. struct page *page = NULL;
  403. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  404. if (page) {
  405. zero_user_segment(page, partial, PAGE_CACHE_SIZE);
  406. set_page_dirty(page);
  407. unlock_page(page);
  408. page_cache_release(page);
  409. }
  410. }
  411. index = start;
  412. for ( ; ; ) {
  413. cond_resched();
  414. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  415. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  416. pvec.pages, indices);
  417. if (!pvec.nr) {
  418. if (index == start)
  419. break;
  420. index = start;
  421. continue;
  422. }
  423. if (index == start && indices[0] > end) {
  424. shmem_pagevec_release(&pvec);
  425. break;
  426. }
  427. mem_cgroup_uncharge_start();
  428. for (i = 0; i < pagevec_count(&pvec); i++) {
  429. struct page *page = pvec.pages[i];
  430. index = indices[i];
  431. if (index > end)
  432. break;
  433. if (radix_tree_exceptional_entry(page)) {
  434. nr_swaps_freed += !shmem_free_swap(mapping,
  435. index, page);
  436. continue;
  437. }
  438. lock_page(page);
  439. if (page->mapping == mapping) {
  440. VM_BUG_ON(PageWriteback(page));
  441. truncate_inode_page(mapping, page);
  442. }
  443. unlock_page(page);
  444. }
  445. shmem_pagevec_release(&pvec);
  446. mem_cgroup_uncharge_end();
  447. index++;
  448. }
  449. spin_lock(&info->lock);
  450. info->swapped -= nr_swaps_freed;
  451. shmem_recalc_inode(inode);
  452. spin_unlock(&info->lock);
  453. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  454. }
  455. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  456. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  457. {
  458. struct inode *inode = dentry->d_inode;
  459. int error;
  460. error = inode_change_ok(inode, attr);
  461. if (error)
  462. return error;
  463. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  464. loff_t oldsize = inode->i_size;
  465. loff_t newsize = attr->ia_size;
  466. if (newsize != oldsize) {
  467. i_size_write(inode, newsize);
  468. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  469. }
  470. if (newsize < oldsize) {
  471. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  472. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  473. shmem_truncate_range(inode, newsize, (loff_t)-1);
  474. /* unmap again to remove racily COWed private pages */
  475. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  476. }
  477. }
  478. setattr_copy(inode, attr);
  479. #ifdef CONFIG_TMPFS_POSIX_ACL
  480. if (attr->ia_valid & ATTR_MODE)
  481. error = generic_acl_chmod(inode);
  482. #endif
  483. return error;
  484. }
  485. static void shmem_evict_inode(struct inode *inode)
  486. {
  487. struct shmem_inode_info *info = SHMEM_I(inode);
  488. struct shmem_xattr *xattr, *nxattr;
  489. if (inode->i_mapping->a_ops == &shmem_aops) {
  490. shmem_unacct_size(info->flags, inode->i_size);
  491. inode->i_size = 0;
  492. shmem_truncate_range(inode, 0, (loff_t)-1);
  493. if (!list_empty(&info->swaplist)) {
  494. mutex_lock(&shmem_swaplist_mutex);
  495. list_del_init(&info->swaplist);
  496. mutex_unlock(&shmem_swaplist_mutex);
  497. }
  498. } else
  499. kfree(info->symlink);
  500. list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
  501. kfree(xattr->name);
  502. kfree(xattr);
  503. }
  504. BUG_ON(inode->i_blocks);
  505. shmem_free_inode(inode->i_sb);
  506. end_writeback(inode);
  507. }
  508. /*
  509. * If swap found in inode, free it and move page from swapcache to filecache.
  510. */
  511. static int shmem_unuse_inode(struct shmem_inode_info *info,
  512. swp_entry_t swap, struct page *page)
  513. {
  514. struct address_space *mapping = info->vfs_inode.i_mapping;
  515. void *radswap;
  516. pgoff_t index;
  517. int error;
  518. radswap = swp_to_radix_entry(swap);
  519. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  520. if (index == -1)
  521. return 0;
  522. /*
  523. * Move _head_ to start search for next from here.
  524. * But be careful: shmem_evict_inode checks list_empty without taking
  525. * mutex, and there's an instant in list_move_tail when info->swaplist
  526. * would appear empty, if it were the only one on shmem_swaplist.
  527. */
  528. if (shmem_swaplist.next != &info->swaplist)
  529. list_move_tail(&shmem_swaplist, &info->swaplist);
  530. /*
  531. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  532. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  533. * beneath us (pagelock doesn't help until the page is in pagecache).
  534. */
  535. error = shmem_add_to_page_cache(page, mapping, index,
  536. GFP_NOWAIT, radswap);
  537. /* which does mem_cgroup_uncharge_cache_page on error */
  538. if (error != -ENOMEM) {
  539. /*
  540. * Truncation and eviction use free_swap_and_cache(), which
  541. * only does trylock page: if we raced, best clean up here.
  542. */
  543. delete_from_swap_cache(page);
  544. set_page_dirty(page);
  545. if (!error) {
  546. spin_lock(&info->lock);
  547. info->swapped--;
  548. spin_unlock(&info->lock);
  549. swap_free(swap);
  550. }
  551. error = 1; /* not an error, but entry was found */
  552. }
  553. return error;
  554. }
  555. /*
  556. * Search through swapped inodes to find and replace swap by page.
  557. */
  558. int shmem_unuse(swp_entry_t swap, struct page *page)
  559. {
  560. struct list_head *this, *next;
  561. struct shmem_inode_info *info;
  562. int found = 0;
  563. int error;
  564. /*
  565. * Charge page using GFP_KERNEL while we can wait, before taking
  566. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  567. * Charged back to the user (not to caller) when swap account is used.
  568. */
  569. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  570. if (error)
  571. goto out;
  572. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  573. mutex_lock(&shmem_swaplist_mutex);
  574. list_for_each_safe(this, next, &shmem_swaplist) {
  575. info = list_entry(this, struct shmem_inode_info, swaplist);
  576. if (info->swapped)
  577. found = shmem_unuse_inode(info, swap, page);
  578. else
  579. list_del_init(&info->swaplist);
  580. cond_resched();
  581. if (found)
  582. break;
  583. }
  584. mutex_unlock(&shmem_swaplist_mutex);
  585. if (!found)
  586. mem_cgroup_uncharge_cache_page(page);
  587. if (found < 0)
  588. error = found;
  589. out:
  590. unlock_page(page);
  591. page_cache_release(page);
  592. return error;
  593. }
  594. /*
  595. * Move the page from the page cache to the swap cache.
  596. */
  597. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  598. {
  599. struct shmem_inode_info *info;
  600. struct address_space *mapping;
  601. struct inode *inode;
  602. swp_entry_t swap;
  603. pgoff_t index;
  604. BUG_ON(!PageLocked(page));
  605. mapping = page->mapping;
  606. index = page->index;
  607. inode = mapping->host;
  608. info = SHMEM_I(inode);
  609. if (info->flags & VM_LOCKED)
  610. goto redirty;
  611. if (!total_swap_pages)
  612. goto redirty;
  613. /*
  614. * shmem_backing_dev_info's capabilities prevent regular writeback or
  615. * sync from ever calling shmem_writepage; but a stacking filesystem
  616. * might use ->writepage of its underlying filesystem, in which case
  617. * tmpfs should write out to swap only in response to memory pressure,
  618. * and not for the writeback threads or sync.
  619. */
  620. if (!wbc->for_reclaim) {
  621. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  622. goto redirty;
  623. }
  624. swap = get_swap_page();
  625. if (!swap.val)
  626. goto redirty;
  627. /*
  628. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  629. * if it's not already there. Do it now before the page is
  630. * moved to swap cache, when its pagelock no longer protects
  631. * the inode from eviction. But don't unlock the mutex until
  632. * we've incremented swapped, because shmem_unuse_inode() will
  633. * prune a !swapped inode from the swaplist under this mutex.
  634. */
  635. mutex_lock(&shmem_swaplist_mutex);
  636. if (list_empty(&info->swaplist))
  637. list_add_tail(&info->swaplist, &shmem_swaplist);
  638. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  639. swap_shmem_alloc(swap);
  640. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  641. spin_lock(&info->lock);
  642. info->swapped++;
  643. shmem_recalc_inode(inode);
  644. spin_unlock(&info->lock);
  645. mutex_unlock(&shmem_swaplist_mutex);
  646. BUG_ON(page_mapped(page));
  647. swap_writepage(page, wbc);
  648. return 0;
  649. }
  650. mutex_unlock(&shmem_swaplist_mutex);
  651. swapcache_free(swap, NULL);
  652. redirty:
  653. set_page_dirty(page);
  654. if (wbc->for_reclaim)
  655. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  656. unlock_page(page);
  657. return 0;
  658. }
  659. #ifdef CONFIG_NUMA
  660. #ifdef CONFIG_TMPFS
  661. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  662. {
  663. char buffer[64];
  664. if (!mpol || mpol->mode == MPOL_DEFAULT)
  665. return; /* show nothing */
  666. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  667. seq_printf(seq, ",mpol=%s", buffer);
  668. }
  669. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  670. {
  671. struct mempolicy *mpol = NULL;
  672. if (sbinfo->mpol) {
  673. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  674. mpol = sbinfo->mpol;
  675. mpol_get(mpol);
  676. spin_unlock(&sbinfo->stat_lock);
  677. }
  678. return mpol;
  679. }
  680. #endif /* CONFIG_TMPFS */
  681. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  682. struct shmem_inode_info *info, pgoff_t index)
  683. {
  684. struct mempolicy mpol, *spol;
  685. struct vm_area_struct pvma;
  686. spol = mpol_cond_copy(&mpol,
  687. mpol_shared_policy_lookup(&info->policy, index));
  688. /* Create a pseudo vma that just contains the policy */
  689. pvma.vm_start = 0;
  690. pvma.vm_pgoff = index;
  691. pvma.vm_ops = NULL;
  692. pvma.vm_policy = spol;
  693. return swapin_readahead(swap, gfp, &pvma, 0);
  694. }
  695. static struct page *shmem_alloc_page(gfp_t gfp,
  696. struct shmem_inode_info *info, pgoff_t index)
  697. {
  698. struct vm_area_struct pvma;
  699. /* Create a pseudo vma that just contains the policy */
  700. pvma.vm_start = 0;
  701. pvma.vm_pgoff = index;
  702. pvma.vm_ops = NULL;
  703. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  704. /*
  705. * alloc_page_vma() will drop the shared policy reference
  706. */
  707. return alloc_page_vma(gfp, &pvma, 0);
  708. }
  709. #else /* !CONFIG_NUMA */
  710. #ifdef CONFIG_TMPFS
  711. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  712. {
  713. }
  714. #endif /* CONFIG_TMPFS */
  715. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  716. struct shmem_inode_info *info, pgoff_t index)
  717. {
  718. return swapin_readahead(swap, gfp, NULL, 0);
  719. }
  720. static inline struct page *shmem_alloc_page(gfp_t gfp,
  721. struct shmem_inode_info *info, pgoff_t index)
  722. {
  723. return alloc_page(gfp);
  724. }
  725. #endif /* CONFIG_NUMA */
  726. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  727. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  728. {
  729. return NULL;
  730. }
  731. #endif
  732. /*
  733. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  734. *
  735. * If we allocate a new one we do not mark it dirty. That's up to the
  736. * vm. If we swap it in we mark it dirty since we also free the swap
  737. * entry since a page cannot live in both the swap and page cache
  738. */
  739. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  740. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  741. {
  742. struct address_space *mapping = inode->i_mapping;
  743. struct shmem_inode_info *info;
  744. struct shmem_sb_info *sbinfo;
  745. struct page *page;
  746. swp_entry_t swap;
  747. int error;
  748. int once = 0;
  749. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  750. return -EFBIG;
  751. repeat:
  752. swap.val = 0;
  753. page = find_lock_page(mapping, index);
  754. if (radix_tree_exceptional_entry(page)) {
  755. swap = radix_to_swp_entry(page);
  756. page = NULL;
  757. }
  758. if (sgp != SGP_WRITE &&
  759. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  760. error = -EINVAL;
  761. goto failed;
  762. }
  763. if (page || (sgp == SGP_READ && !swap.val)) {
  764. /*
  765. * Once we can get the page lock, it must be uptodate:
  766. * if there were an error in reading back from swap,
  767. * the page would not be inserted into the filecache.
  768. */
  769. BUG_ON(page && !PageUptodate(page));
  770. *pagep = page;
  771. return 0;
  772. }
  773. /*
  774. * Fast cache lookup did not find it:
  775. * bring it back from swap or allocate.
  776. */
  777. info = SHMEM_I(inode);
  778. sbinfo = SHMEM_SB(inode->i_sb);
  779. if (swap.val) {
  780. /* Look it up and read it in.. */
  781. page = lookup_swap_cache(swap);
  782. if (!page) {
  783. /* here we actually do the io */
  784. if (fault_type)
  785. *fault_type |= VM_FAULT_MAJOR;
  786. page = shmem_swapin(swap, gfp, info, index);
  787. if (!page) {
  788. error = -ENOMEM;
  789. goto failed;
  790. }
  791. }
  792. /* We have to do this with page locked to prevent races */
  793. lock_page(page);
  794. if (!PageUptodate(page)) {
  795. error = -EIO;
  796. goto failed;
  797. }
  798. wait_on_page_writeback(page);
  799. /* Someone may have already done it for us */
  800. if (page->mapping) {
  801. if (page->mapping == mapping &&
  802. page->index == index)
  803. goto done;
  804. error = -EEXIST;
  805. goto failed;
  806. }
  807. error = mem_cgroup_cache_charge(page, current->mm,
  808. gfp & GFP_RECLAIM_MASK);
  809. if (!error)
  810. error = shmem_add_to_page_cache(page, mapping, index,
  811. gfp, swp_to_radix_entry(swap));
  812. if (error)
  813. goto failed;
  814. spin_lock(&info->lock);
  815. info->swapped--;
  816. shmem_recalc_inode(inode);
  817. spin_unlock(&info->lock);
  818. delete_from_swap_cache(page);
  819. set_page_dirty(page);
  820. swap_free(swap);
  821. } else {
  822. if (shmem_acct_block(info->flags)) {
  823. error = -ENOSPC;
  824. goto failed;
  825. }
  826. if (sbinfo->max_blocks) {
  827. if (percpu_counter_compare(&sbinfo->used_blocks,
  828. sbinfo->max_blocks) >= 0) {
  829. error = -ENOSPC;
  830. goto unacct;
  831. }
  832. percpu_counter_inc(&sbinfo->used_blocks);
  833. }
  834. page = shmem_alloc_page(gfp, info, index);
  835. if (!page) {
  836. error = -ENOMEM;
  837. goto decused;
  838. }
  839. SetPageSwapBacked(page);
  840. __set_page_locked(page);
  841. error = mem_cgroup_cache_charge(page, current->mm,
  842. gfp & GFP_RECLAIM_MASK);
  843. if (!error)
  844. error = shmem_add_to_page_cache(page, mapping, index,
  845. gfp, NULL);
  846. if (error)
  847. goto decused;
  848. lru_cache_add_anon(page);
  849. spin_lock(&info->lock);
  850. info->alloced++;
  851. inode->i_blocks += BLOCKS_PER_PAGE;
  852. shmem_recalc_inode(inode);
  853. spin_unlock(&info->lock);
  854. clear_highpage(page);
  855. flush_dcache_page(page);
  856. SetPageUptodate(page);
  857. if (sgp == SGP_DIRTY)
  858. set_page_dirty(page);
  859. }
  860. done:
  861. /* Perhaps the file has been truncated since we checked */
  862. if (sgp != SGP_WRITE &&
  863. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  864. error = -EINVAL;
  865. goto trunc;
  866. }
  867. *pagep = page;
  868. return 0;
  869. /*
  870. * Error recovery.
  871. */
  872. trunc:
  873. ClearPageDirty(page);
  874. delete_from_page_cache(page);
  875. spin_lock(&info->lock);
  876. info->alloced--;
  877. inode->i_blocks -= BLOCKS_PER_PAGE;
  878. spin_unlock(&info->lock);
  879. decused:
  880. if (sbinfo->max_blocks)
  881. percpu_counter_add(&sbinfo->used_blocks, -1);
  882. unacct:
  883. shmem_unacct_blocks(info->flags, 1);
  884. failed:
  885. if (swap.val && error != -EINVAL) {
  886. struct page *test = find_get_page(mapping, index);
  887. if (test && !radix_tree_exceptional_entry(test))
  888. page_cache_release(test);
  889. /* Have another try if the entry has changed */
  890. if (test != swp_to_radix_entry(swap))
  891. error = -EEXIST;
  892. }
  893. if (page) {
  894. unlock_page(page);
  895. page_cache_release(page);
  896. }
  897. if (error == -ENOSPC && !once++) {
  898. info = SHMEM_I(inode);
  899. spin_lock(&info->lock);
  900. shmem_recalc_inode(inode);
  901. spin_unlock(&info->lock);
  902. goto repeat;
  903. }
  904. if (error == -EEXIST)
  905. goto repeat;
  906. return error;
  907. }
  908. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  909. {
  910. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  911. int error;
  912. int ret = VM_FAULT_LOCKED;
  913. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  914. if (error)
  915. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  916. if (ret & VM_FAULT_MAJOR) {
  917. count_vm_event(PGMAJFAULT);
  918. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  919. }
  920. return ret;
  921. }
  922. #ifdef CONFIG_NUMA
  923. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  924. {
  925. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  926. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  927. }
  928. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  929. unsigned long addr)
  930. {
  931. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  932. pgoff_t index;
  933. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  934. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  935. }
  936. #endif
  937. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  938. {
  939. struct inode *inode = file->f_path.dentry->d_inode;
  940. struct shmem_inode_info *info = SHMEM_I(inode);
  941. int retval = -ENOMEM;
  942. spin_lock(&info->lock);
  943. if (lock && !(info->flags & VM_LOCKED)) {
  944. if (!user_shm_lock(inode->i_size, user))
  945. goto out_nomem;
  946. info->flags |= VM_LOCKED;
  947. mapping_set_unevictable(file->f_mapping);
  948. }
  949. if (!lock && (info->flags & VM_LOCKED) && user) {
  950. user_shm_unlock(inode->i_size, user);
  951. info->flags &= ~VM_LOCKED;
  952. mapping_clear_unevictable(file->f_mapping);
  953. /*
  954. * Ensure that a racing putback_lru_page() can see
  955. * the pages of this mapping are evictable when we
  956. * skip them due to !PageLRU during the scan.
  957. */
  958. smp_mb__after_clear_bit();
  959. scan_mapping_unevictable_pages(file->f_mapping);
  960. }
  961. retval = 0;
  962. out_nomem:
  963. spin_unlock(&info->lock);
  964. return retval;
  965. }
  966. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  967. {
  968. file_accessed(file);
  969. vma->vm_ops = &shmem_vm_ops;
  970. vma->vm_flags |= VM_CAN_NONLINEAR;
  971. return 0;
  972. }
  973. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  974. umode_t mode, dev_t dev, unsigned long flags)
  975. {
  976. struct inode *inode;
  977. struct shmem_inode_info *info;
  978. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  979. if (shmem_reserve_inode(sb))
  980. return NULL;
  981. inode = new_inode(sb);
  982. if (inode) {
  983. inode->i_ino = get_next_ino();
  984. inode_init_owner(inode, dir, mode);
  985. inode->i_blocks = 0;
  986. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  987. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  988. inode->i_generation = get_seconds();
  989. info = SHMEM_I(inode);
  990. memset(info, 0, (char *)inode - (char *)info);
  991. spin_lock_init(&info->lock);
  992. info->flags = flags & VM_NORESERVE;
  993. INIT_LIST_HEAD(&info->swaplist);
  994. INIT_LIST_HEAD(&info->xattr_list);
  995. cache_no_acl(inode);
  996. switch (mode & S_IFMT) {
  997. default:
  998. inode->i_op = &shmem_special_inode_operations;
  999. init_special_inode(inode, mode, dev);
  1000. break;
  1001. case S_IFREG:
  1002. inode->i_mapping->a_ops = &shmem_aops;
  1003. inode->i_op = &shmem_inode_operations;
  1004. inode->i_fop = &shmem_file_operations;
  1005. mpol_shared_policy_init(&info->policy,
  1006. shmem_get_sbmpol(sbinfo));
  1007. break;
  1008. case S_IFDIR:
  1009. inc_nlink(inode);
  1010. /* Some things misbehave if size == 0 on a directory */
  1011. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1012. inode->i_op = &shmem_dir_inode_operations;
  1013. inode->i_fop = &simple_dir_operations;
  1014. break;
  1015. case S_IFLNK:
  1016. /*
  1017. * Must not load anything in the rbtree,
  1018. * mpol_free_shared_policy will not be called.
  1019. */
  1020. mpol_shared_policy_init(&info->policy, NULL);
  1021. break;
  1022. }
  1023. } else
  1024. shmem_free_inode(sb);
  1025. return inode;
  1026. }
  1027. #ifdef CONFIG_TMPFS
  1028. static const struct inode_operations shmem_symlink_inode_operations;
  1029. static const struct inode_operations shmem_short_symlink_operations;
  1030. static int
  1031. shmem_write_begin(struct file *file, struct address_space *mapping,
  1032. loff_t pos, unsigned len, unsigned flags,
  1033. struct page **pagep, void **fsdata)
  1034. {
  1035. struct inode *inode = mapping->host;
  1036. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1037. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1038. }
  1039. static int
  1040. shmem_write_end(struct file *file, struct address_space *mapping,
  1041. loff_t pos, unsigned len, unsigned copied,
  1042. struct page *page, void *fsdata)
  1043. {
  1044. struct inode *inode = mapping->host;
  1045. if (pos + copied > inode->i_size)
  1046. i_size_write(inode, pos + copied);
  1047. set_page_dirty(page);
  1048. unlock_page(page);
  1049. page_cache_release(page);
  1050. return copied;
  1051. }
  1052. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1053. {
  1054. struct inode *inode = filp->f_path.dentry->d_inode;
  1055. struct address_space *mapping = inode->i_mapping;
  1056. pgoff_t index;
  1057. unsigned long offset;
  1058. enum sgp_type sgp = SGP_READ;
  1059. /*
  1060. * Might this read be for a stacking filesystem? Then when reading
  1061. * holes of a sparse file, we actually need to allocate those pages,
  1062. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1063. */
  1064. if (segment_eq(get_fs(), KERNEL_DS))
  1065. sgp = SGP_DIRTY;
  1066. index = *ppos >> PAGE_CACHE_SHIFT;
  1067. offset = *ppos & ~PAGE_CACHE_MASK;
  1068. for (;;) {
  1069. struct page *page = NULL;
  1070. pgoff_t end_index;
  1071. unsigned long nr, ret;
  1072. loff_t i_size = i_size_read(inode);
  1073. end_index = i_size >> PAGE_CACHE_SHIFT;
  1074. if (index > end_index)
  1075. break;
  1076. if (index == end_index) {
  1077. nr = i_size & ~PAGE_CACHE_MASK;
  1078. if (nr <= offset)
  1079. break;
  1080. }
  1081. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1082. if (desc->error) {
  1083. if (desc->error == -EINVAL)
  1084. desc->error = 0;
  1085. break;
  1086. }
  1087. if (page)
  1088. unlock_page(page);
  1089. /*
  1090. * We must evaluate after, since reads (unlike writes)
  1091. * are called without i_mutex protection against truncate
  1092. */
  1093. nr = PAGE_CACHE_SIZE;
  1094. i_size = i_size_read(inode);
  1095. end_index = i_size >> PAGE_CACHE_SHIFT;
  1096. if (index == end_index) {
  1097. nr = i_size & ~PAGE_CACHE_MASK;
  1098. if (nr <= offset) {
  1099. if (page)
  1100. page_cache_release(page);
  1101. break;
  1102. }
  1103. }
  1104. nr -= offset;
  1105. if (page) {
  1106. /*
  1107. * If users can be writing to this page using arbitrary
  1108. * virtual addresses, take care about potential aliasing
  1109. * before reading the page on the kernel side.
  1110. */
  1111. if (mapping_writably_mapped(mapping))
  1112. flush_dcache_page(page);
  1113. /*
  1114. * Mark the page accessed if we read the beginning.
  1115. */
  1116. if (!offset)
  1117. mark_page_accessed(page);
  1118. } else {
  1119. page = ZERO_PAGE(0);
  1120. page_cache_get(page);
  1121. }
  1122. /*
  1123. * Ok, we have the page, and it's up-to-date, so
  1124. * now we can copy it to user space...
  1125. *
  1126. * The actor routine returns how many bytes were actually used..
  1127. * NOTE! This may not be the same as how much of a user buffer
  1128. * we filled up (we may be padding etc), so we can only update
  1129. * "pos" here (the actor routine has to update the user buffer
  1130. * pointers and the remaining count).
  1131. */
  1132. ret = actor(desc, page, offset, nr);
  1133. offset += ret;
  1134. index += offset >> PAGE_CACHE_SHIFT;
  1135. offset &= ~PAGE_CACHE_MASK;
  1136. page_cache_release(page);
  1137. if (ret != nr || !desc->count)
  1138. break;
  1139. cond_resched();
  1140. }
  1141. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1142. file_accessed(filp);
  1143. }
  1144. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1145. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1146. {
  1147. struct file *filp = iocb->ki_filp;
  1148. ssize_t retval;
  1149. unsigned long seg;
  1150. size_t count;
  1151. loff_t *ppos = &iocb->ki_pos;
  1152. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1153. if (retval)
  1154. return retval;
  1155. for (seg = 0; seg < nr_segs; seg++) {
  1156. read_descriptor_t desc;
  1157. desc.written = 0;
  1158. desc.arg.buf = iov[seg].iov_base;
  1159. desc.count = iov[seg].iov_len;
  1160. if (desc.count == 0)
  1161. continue;
  1162. desc.error = 0;
  1163. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1164. retval += desc.written;
  1165. if (desc.error) {
  1166. retval = retval ?: desc.error;
  1167. break;
  1168. }
  1169. if (desc.count > 0)
  1170. break;
  1171. }
  1172. return retval;
  1173. }
  1174. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1175. struct pipe_inode_info *pipe, size_t len,
  1176. unsigned int flags)
  1177. {
  1178. struct address_space *mapping = in->f_mapping;
  1179. struct inode *inode = mapping->host;
  1180. unsigned int loff, nr_pages, req_pages;
  1181. struct page *pages[PIPE_DEF_BUFFERS];
  1182. struct partial_page partial[PIPE_DEF_BUFFERS];
  1183. struct page *page;
  1184. pgoff_t index, end_index;
  1185. loff_t isize, left;
  1186. int error, page_nr;
  1187. struct splice_pipe_desc spd = {
  1188. .pages = pages,
  1189. .partial = partial,
  1190. .flags = flags,
  1191. .ops = &page_cache_pipe_buf_ops,
  1192. .spd_release = spd_release_page,
  1193. };
  1194. isize = i_size_read(inode);
  1195. if (unlikely(*ppos >= isize))
  1196. return 0;
  1197. left = isize - *ppos;
  1198. if (unlikely(left < len))
  1199. len = left;
  1200. if (splice_grow_spd(pipe, &spd))
  1201. return -ENOMEM;
  1202. index = *ppos >> PAGE_CACHE_SHIFT;
  1203. loff = *ppos & ~PAGE_CACHE_MASK;
  1204. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1205. nr_pages = min(req_pages, pipe->buffers);
  1206. spd.nr_pages = find_get_pages_contig(mapping, index,
  1207. nr_pages, spd.pages);
  1208. index += spd.nr_pages;
  1209. error = 0;
  1210. while (spd.nr_pages < nr_pages) {
  1211. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1212. if (error)
  1213. break;
  1214. unlock_page(page);
  1215. spd.pages[spd.nr_pages++] = page;
  1216. index++;
  1217. }
  1218. index = *ppos >> PAGE_CACHE_SHIFT;
  1219. nr_pages = spd.nr_pages;
  1220. spd.nr_pages = 0;
  1221. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1222. unsigned int this_len;
  1223. if (!len)
  1224. break;
  1225. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1226. page = spd.pages[page_nr];
  1227. if (!PageUptodate(page) || page->mapping != mapping) {
  1228. error = shmem_getpage(inode, index, &page,
  1229. SGP_CACHE, NULL);
  1230. if (error)
  1231. break;
  1232. unlock_page(page);
  1233. page_cache_release(spd.pages[page_nr]);
  1234. spd.pages[page_nr] = page;
  1235. }
  1236. isize = i_size_read(inode);
  1237. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1238. if (unlikely(!isize || index > end_index))
  1239. break;
  1240. if (end_index == index) {
  1241. unsigned int plen;
  1242. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1243. if (plen <= loff)
  1244. break;
  1245. this_len = min(this_len, plen - loff);
  1246. len = this_len;
  1247. }
  1248. spd.partial[page_nr].offset = loff;
  1249. spd.partial[page_nr].len = this_len;
  1250. len -= this_len;
  1251. loff = 0;
  1252. spd.nr_pages++;
  1253. index++;
  1254. }
  1255. while (page_nr < nr_pages)
  1256. page_cache_release(spd.pages[page_nr++]);
  1257. if (spd.nr_pages)
  1258. error = splice_to_pipe(pipe, &spd);
  1259. splice_shrink_spd(pipe, &spd);
  1260. if (error > 0) {
  1261. *ppos += error;
  1262. file_accessed(in);
  1263. }
  1264. return error;
  1265. }
  1266. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1267. {
  1268. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1269. buf->f_type = TMPFS_MAGIC;
  1270. buf->f_bsize = PAGE_CACHE_SIZE;
  1271. buf->f_namelen = NAME_MAX;
  1272. if (sbinfo->max_blocks) {
  1273. buf->f_blocks = sbinfo->max_blocks;
  1274. buf->f_bavail =
  1275. buf->f_bfree = sbinfo->max_blocks -
  1276. percpu_counter_sum(&sbinfo->used_blocks);
  1277. }
  1278. if (sbinfo->max_inodes) {
  1279. buf->f_files = sbinfo->max_inodes;
  1280. buf->f_ffree = sbinfo->free_inodes;
  1281. }
  1282. /* else leave those fields 0 like simple_statfs */
  1283. return 0;
  1284. }
  1285. /*
  1286. * File creation. Allocate an inode, and we're done..
  1287. */
  1288. static int
  1289. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  1290. {
  1291. struct inode *inode;
  1292. int error = -ENOSPC;
  1293. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1294. if (inode) {
  1295. error = security_inode_init_security(inode, dir,
  1296. &dentry->d_name,
  1297. NULL, NULL);
  1298. if (error) {
  1299. if (error != -EOPNOTSUPP) {
  1300. iput(inode);
  1301. return error;
  1302. }
  1303. }
  1304. #ifdef CONFIG_TMPFS_POSIX_ACL
  1305. error = generic_acl_init(inode, dir);
  1306. if (error) {
  1307. iput(inode);
  1308. return error;
  1309. }
  1310. #else
  1311. error = 0;
  1312. #endif
  1313. dir->i_size += BOGO_DIRENT_SIZE;
  1314. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1315. d_instantiate(dentry, inode);
  1316. dget(dentry); /* Extra count - pin the dentry in core */
  1317. }
  1318. return error;
  1319. }
  1320. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1321. {
  1322. int error;
  1323. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1324. return error;
  1325. inc_nlink(dir);
  1326. return 0;
  1327. }
  1328. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1329. struct nameidata *nd)
  1330. {
  1331. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1332. }
  1333. /*
  1334. * Link a file..
  1335. */
  1336. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1337. {
  1338. struct inode *inode = old_dentry->d_inode;
  1339. int ret;
  1340. /*
  1341. * No ordinary (disk based) filesystem counts links as inodes;
  1342. * but each new link needs a new dentry, pinning lowmem, and
  1343. * tmpfs dentries cannot be pruned until they are unlinked.
  1344. */
  1345. ret = shmem_reserve_inode(inode->i_sb);
  1346. if (ret)
  1347. goto out;
  1348. dir->i_size += BOGO_DIRENT_SIZE;
  1349. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1350. inc_nlink(inode);
  1351. ihold(inode); /* New dentry reference */
  1352. dget(dentry); /* Extra pinning count for the created dentry */
  1353. d_instantiate(dentry, inode);
  1354. out:
  1355. return ret;
  1356. }
  1357. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1358. {
  1359. struct inode *inode = dentry->d_inode;
  1360. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1361. shmem_free_inode(inode->i_sb);
  1362. dir->i_size -= BOGO_DIRENT_SIZE;
  1363. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1364. drop_nlink(inode);
  1365. dput(dentry); /* Undo the count from "create" - this does all the work */
  1366. return 0;
  1367. }
  1368. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1369. {
  1370. if (!simple_empty(dentry))
  1371. return -ENOTEMPTY;
  1372. drop_nlink(dentry->d_inode);
  1373. drop_nlink(dir);
  1374. return shmem_unlink(dir, dentry);
  1375. }
  1376. /*
  1377. * The VFS layer already does all the dentry stuff for rename,
  1378. * we just have to decrement the usage count for the target if
  1379. * it exists so that the VFS layer correctly free's it when it
  1380. * gets overwritten.
  1381. */
  1382. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1383. {
  1384. struct inode *inode = old_dentry->d_inode;
  1385. int they_are_dirs = S_ISDIR(inode->i_mode);
  1386. if (!simple_empty(new_dentry))
  1387. return -ENOTEMPTY;
  1388. if (new_dentry->d_inode) {
  1389. (void) shmem_unlink(new_dir, new_dentry);
  1390. if (they_are_dirs)
  1391. drop_nlink(old_dir);
  1392. } else if (they_are_dirs) {
  1393. drop_nlink(old_dir);
  1394. inc_nlink(new_dir);
  1395. }
  1396. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1397. new_dir->i_size += BOGO_DIRENT_SIZE;
  1398. old_dir->i_ctime = old_dir->i_mtime =
  1399. new_dir->i_ctime = new_dir->i_mtime =
  1400. inode->i_ctime = CURRENT_TIME;
  1401. return 0;
  1402. }
  1403. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1404. {
  1405. int error;
  1406. int len;
  1407. struct inode *inode;
  1408. struct page *page;
  1409. char *kaddr;
  1410. struct shmem_inode_info *info;
  1411. len = strlen(symname) + 1;
  1412. if (len > PAGE_CACHE_SIZE)
  1413. return -ENAMETOOLONG;
  1414. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1415. if (!inode)
  1416. return -ENOSPC;
  1417. error = security_inode_init_security(inode, dir, &dentry->d_name,
  1418. NULL, NULL);
  1419. if (error) {
  1420. if (error != -EOPNOTSUPP) {
  1421. iput(inode);
  1422. return error;
  1423. }
  1424. error = 0;
  1425. }
  1426. info = SHMEM_I(inode);
  1427. inode->i_size = len-1;
  1428. if (len <= SHORT_SYMLINK_LEN) {
  1429. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1430. if (!info->symlink) {
  1431. iput(inode);
  1432. return -ENOMEM;
  1433. }
  1434. inode->i_op = &shmem_short_symlink_operations;
  1435. } else {
  1436. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1437. if (error) {
  1438. iput(inode);
  1439. return error;
  1440. }
  1441. inode->i_mapping->a_ops = &shmem_aops;
  1442. inode->i_op = &shmem_symlink_inode_operations;
  1443. kaddr = kmap_atomic(page, KM_USER0);
  1444. memcpy(kaddr, symname, len);
  1445. kunmap_atomic(kaddr, KM_USER0);
  1446. set_page_dirty(page);
  1447. unlock_page(page);
  1448. page_cache_release(page);
  1449. }
  1450. dir->i_size += BOGO_DIRENT_SIZE;
  1451. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1452. d_instantiate(dentry, inode);
  1453. dget(dentry);
  1454. return 0;
  1455. }
  1456. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1457. {
  1458. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1459. return NULL;
  1460. }
  1461. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1462. {
  1463. struct page *page = NULL;
  1464. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1465. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1466. if (page)
  1467. unlock_page(page);
  1468. return page;
  1469. }
  1470. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1471. {
  1472. if (!IS_ERR(nd_get_link(nd))) {
  1473. struct page *page = cookie;
  1474. kunmap(page);
  1475. mark_page_accessed(page);
  1476. page_cache_release(page);
  1477. }
  1478. }
  1479. #ifdef CONFIG_TMPFS_XATTR
  1480. /*
  1481. * Superblocks without xattr inode operations may get some security.* xattr
  1482. * support from the LSM "for free". As soon as we have any other xattrs
  1483. * like ACLs, we also need to implement the security.* handlers at
  1484. * filesystem level, though.
  1485. */
  1486. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1487. void *buffer, size_t size)
  1488. {
  1489. struct shmem_inode_info *info;
  1490. struct shmem_xattr *xattr;
  1491. int ret = -ENODATA;
  1492. info = SHMEM_I(dentry->d_inode);
  1493. spin_lock(&info->lock);
  1494. list_for_each_entry(xattr, &info->xattr_list, list) {
  1495. if (strcmp(name, xattr->name))
  1496. continue;
  1497. ret = xattr->size;
  1498. if (buffer) {
  1499. if (size < xattr->size)
  1500. ret = -ERANGE;
  1501. else
  1502. memcpy(buffer, xattr->value, xattr->size);
  1503. }
  1504. break;
  1505. }
  1506. spin_unlock(&info->lock);
  1507. return ret;
  1508. }
  1509. static int shmem_xattr_set(struct dentry *dentry, const char *name,
  1510. const void *value, size_t size, int flags)
  1511. {
  1512. struct inode *inode = dentry->d_inode;
  1513. struct shmem_inode_info *info = SHMEM_I(inode);
  1514. struct shmem_xattr *xattr;
  1515. struct shmem_xattr *new_xattr = NULL;
  1516. size_t len;
  1517. int err = 0;
  1518. /* value == NULL means remove */
  1519. if (value) {
  1520. /* wrap around? */
  1521. len = sizeof(*new_xattr) + size;
  1522. if (len <= sizeof(*new_xattr))
  1523. return -ENOMEM;
  1524. new_xattr = kmalloc(len, GFP_KERNEL);
  1525. if (!new_xattr)
  1526. return -ENOMEM;
  1527. new_xattr->name = kstrdup(name, GFP_KERNEL);
  1528. if (!new_xattr->name) {
  1529. kfree(new_xattr);
  1530. return -ENOMEM;
  1531. }
  1532. new_xattr->size = size;
  1533. memcpy(new_xattr->value, value, size);
  1534. }
  1535. spin_lock(&info->lock);
  1536. list_for_each_entry(xattr, &info->xattr_list, list) {
  1537. if (!strcmp(name, xattr->name)) {
  1538. if (flags & XATTR_CREATE) {
  1539. xattr = new_xattr;
  1540. err = -EEXIST;
  1541. } else if (new_xattr) {
  1542. list_replace(&xattr->list, &new_xattr->list);
  1543. } else {
  1544. list_del(&xattr->list);
  1545. }
  1546. goto out;
  1547. }
  1548. }
  1549. if (flags & XATTR_REPLACE) {
  1550. xattr = new_xattr;
  1551. err = -ENODATA;
  1552. } else {
  1553. list_add(&new_xattr->list, &info->xattr_list);
  1554. xattr = NULL;
  1555. }
  1556. out:
  1557. spin_unlock(&info->lock);
  1558. if (xattr)
  1559. kfree(xattr->name);
  1560. kfree(xattr);
  1561. return err;
  1562. }
  1563. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1564. #ifdef CONFIG_TMPFS_POSIX_ACL
  1565. &generic_acl_access_handler,
  1566. &generic_acl_default_handler,
  1567. #endif
  1568. NULL
  1569. };
  1570. static int shmem_xattr_validate(const char *name)
  1571. {
  1572. struct { const char *prefix; size_t len; } arr[] = {
  1573. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1574. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1575. };
  1576. int i;
  1577. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1578. size_t preflen = arr[i].len;
  1579. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1580. if (!name[preflen])
  1581. return -EINVAL;
  1582. return 0;
  1583. }
  1584. }
  1585. return -EOPNOTSUPP;
  1586. }
  1587. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1588. void *buffer, size_t size)
  1589. {
  1590. int err;
  1591. /*
  1592. * If this is a request for a synthetic attribute in the system.*
  1593. * namespace use the generic infrastructure to resolve a handler
  1594. * for it via sb->s_xattr.
  1595. */
  1596. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1597. return generic_getxattr(dentry, name, buffer, size);
  1598. err = shmem_xattr_validate(name);
  1599. if (err)
  1600. return err;
  1601. return shmem_xattr_get(dentry, name, buffer, size);
  1602. }
  1603. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1604. const void *value, size_t size, int flags)
  1605. {
  1606. int err;
  1607. /*
  1608. * If this is a request for a synthetic attribute in the system.*
  1609. * namespace use the generic infrastructure to resolve a handler
  1610. * for it via sb->s_xattr.
  1611. */
  1612. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1613. return generic_setxattr(dentry, name, value, size, flags);
  1614. err = shmem_xattr_validate(name);
  1615. if (err)
  1616. return err;
  1617. if (size == 0)
  1618. value = ""; /* empty EA, do not remove */
  1619. return shmem_xattr_set(dentry, name, value, size, flags);
  1620. }
  1621. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1622. {
  1623. int err;
  1624. /*
  1625. * If this is a request for a synthetic attribute in the system.*
  1626. * namespace use the generic infrastructure to resolve a handler
  1627. * for it via sb->s_xattr.
  1628. */
  1629. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1630. return generic_removexattr(dentry, name);
  1631. err = shmem_xattr_validate(name);
  1632. if (err)
  1633. return err;
  1634. return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
  1635. }
  1636. static bool xattr_is_trusted(const char *name)
  1637. {
  1638. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1639. }
  1640. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1641. {
  1642. bool trusted = capable(CAP_SYS_ADMIN);
  1643. struct shmem_xattr *xattr;
  1644. struct shmem_inode_info *info;
  1645. size_t used = 0;
  1646. info = SHMEM_I(dentry->d_inode);
  1647. spin_lock(&info->lock);
  1648. list_for_each_entry(xattr, &info->xattr_list, list) {
  1649. size_t len;
  1650. /* skip "trusted." attributes for unprivileged callers */
  1651. if (!trusted && xattr_is_trusted(xattr->name))
  1652. continue;
  1653. len = strlen(xattr->name) + 1;
  1654. used += len;
  1655. if (buffer) {
  1656. if (size < used) {
  1657. used = -ERANGE;
  1658. break;
  1659. }
  1660. memcpy(buffer, xattr->name, len);
  1661. buffer += len;
  1662. }
  1663. }
  1664. spin_unlock(&info->lock);
  1665. return used;
  1666. }
  1667. #endif /* CONFIG_TMPFS_XATTR */
  1668. static const struct inode_operations shmem_short_symlink_operations = {
  1669. .readlink = generic_readlink,
  1670. .follow_link = shmem_follow_short_symlink,
  1671. #ifdef CONFIG_TMPFS_XATTR
  1672. .setxattr = shmem_setxattr,
  1673. .getxattr = shmem_getxattr,
  1674. .listxattr = shmem_listxattr,
  1675. .removexattr = shmem_removexattr,
  1676. #endif
  1677. };
  1678. static const struct inode_operations shmem_symlink_inode_operations = {
  1679. .readlink = generic_readlink,
  1680. .follow_link = shmem_follow_link,
  1681. .put_link = shmem_put_link,
  1682. #ifdef CONFIG_TMPFS_XATTR
  1683. .setxattr = shmem_setxattr,
  1684. .getxattr = shmem_getxattr,
  1685. .listxattr = shmem_listxattr,
  1686. .removexattr = shmem_removexattr,
  1687. #endif
  1688. };
  1689. static struct dentry *shmem_get_parent(struct dentry *child)
  1690. {
  1691. return ERR_PTR(-ESTALE);
  1692. }
  1693. static int shmem_match(struct inode *ino, void *vfh)
  1694. {
  1695. __u32 *fh = vfh;
  1696. __u64 inum = fh[2];
  1697. inum = (inum << 32) | fh[1];
  1698. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1699. }
  1700. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1701. struct fid *fid, int fh_len, int fh_type)
  1702. {
  1703. struct inode *inode;
  1704. struct dentry *dentry = NULL;
  1705. u64 inum = fid->raw[2];
  1706. inum = (inum << 32) | fid->raw[1];
  1707. if (fh_len < 3)
  1708. return NULL;
  1709. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1710. shmem_match, fid->raw);
  1711. if (inode) {
  1712. dentry = d_find_alias(inode);
  1713. iput(inode);
  1714. }
  1715. return dentry;
  1716. }
  1717. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1718. int connectable)
  1719. {
  1720. struct inode *inode = dentry->d_inode;
  1721. if (*len < 3) {
  1722. *len = 3;
  1723. return 255;
  1724. }
  1725. if (inode_unhashed(inode)) {
  1726. /* Unfortunately insert_inode_hash is not idempotent,
  1727. * so as we hash inodes here rather than at creation
  1728. * time, we need a lock to ensure we only try
  1729. * to do it once
  1730. */
  1731. static DEFINE_SPINLOCK(lock);
  1732. spin_lock(&lock);
  1733. if (inode_unhashed(inode))
  1734. __insert_inode_hash(inode,
  1735. inode->i_ino + inode->i_generation);
  1736. spin_unlock(&lock);
  1737. }
  1738. fh[0] = inode->i_generation;
  1739. fh[1] = inode->i_ino;
  1740. fh[2] = ((__u64)inode->i_ino) >> 32;
  1741. *len = 3;
  1742. return 1;
  1743. }
  1744. static const struct export_operations shmem_export_ops = {
  1745. .get_parent = shmem_get_parent,
  1746. .encode_fh = shmem_encode_fh,
  1747. .fh_to_dentry = shmem_fh_to_dentry,
  1748. };
  1749. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1750. bool remount)
  1751. {
  1752. char *this_char, *value, *rest;
  1753. while (options != NULL) {
  1754. this_char = options;
  1755. for (;;) {
  1756. /*
  1757. * NUL-terminate this option: unfortunately,
  1758. * mount options form a comma-separated list,
  1759. * but mpol's nodelist may also contain commas.
  1760. */
  1761. options = strchr(options, ',');
  1762. if (options == NULL)
  1763. break;
  1764. options++;
  1765. if (!isdigit(*options)) {
  1766. options[-1] = '\0';
  1767. break;
  1768. }
  1769. }
  1770. if (!*this_char)
  1771. continue;
  1772. if ((value = strchr(this_char,'=')) != NULL) {
  1773. *value++ = 0;
  1774. } else {
  1775. printk(KERN_ERR
  1776. "tmpfs: No value for mount option '%s'\n",
  1777. this_char);
  1778. return 1;
  1779. }
  1780. if (!strcmp(this_char,"size")) {
  1781. unsigned long long size;
  1782. size = memparse(value,&rest);
  1783. if (*rest == '%') {
  1784. size <<= PAGE_SHIFT;
  1785. size *= totalram_pages;
  1786. do_div(size, 100);
  1787. rest++;
  1788. }
  1789. if (*rest)
  1790. goto bad_val;
  1791. sbinfo->max_blocks =
  1792. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1793. } else if (!strcmp(this_char,"nr_blocks")) {
  1794. sbinfo->max_blocks = memparse(value, &rest);
  1795. if (*rest)
  1796. goto bad_val;
  1797. } else if (!strcmp(this_char,"nr_inodes")) {
  1798. sbinfo->max_inodes = memparse(value, &rest);
  1799. if (*rest)
  1800. goto bad_val;
  1801. } else if (!strcmp(this_char,"mode")) {
  1802. if (remount)
  1803. continue;
  1804. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1805. if (*rest)
  1806. goto bad_val;
  1807. } else if (!strcmp(this_char,"uid")) {
  1808. if (remount)
  1809. continue;
  1810. sbinfo->uid = simple_strtoul(value, &rest, 0);
  1811. if (*rest)
  1812. goto bad_val;
  1813. } else if (!strcmp(this_char,"gid")) {
  1814. if (remount)
  1815. continue;
  1816. sbinfo->gid = simple_strtoul(value, &rest, 0);
  1817. if (*rest)
  1818. goto bad_val;
  1819. } else if (!strcmp(this_char,"mpol")) {
  1820. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  1821. goto bad_val;
  1822. } else {
  1823. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1824. this_char);
  1825. return 1;
  1826. }
  1827. }
  1828. return 0;
  1829. bad_val:
  1830. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1831. value, this_char);
  1832. return 1;
  1833. }
  1834. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1835. {
  1836. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1837. struct shmem_sb_info config = *sbinfo;
  1838. unsigned long inodes;
  1839. int error = -EINVAL;
  1840. if (shmem_parse_options(data, &config, true))
  1841. return error;
  1842. spin_lock(&sbinfo->stat_lock);
  1843. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1844. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  1845. goto out;
  1846. if (config.max_inodes < inodes)
  1847. goto out;
  1848. /*
  1849. * Those tests disallow limited->unlimited while any are in use;
  1850. * but we must separately disallow unlimited->limited, because
  1851. * in that case we have no record of how much is already in use.
  1852. */
  1853. if (config.max_blocks && !sbinfo->max_blocks)
  1854. goto out;
  1855. if (config.max_inodes && !sbinfo->max_inodes)
  1856. goto out;
  1857. error = 0;
  1858. sbinfo->max_blocks = config.max_blocks;
  1859. sbinfo->max_inodes = config.max_inodes;
  1860. sbinfo->free_inodes = config.max_inodes - inodes;
  1861. mpol_put(sbinfo->mpol);
  1862. sbinfo->mpol = config.mpol; /* transfers initial ref */
  1863. out:
  1864. spin_unlock(&sbinfo->stat_lock);
  1865. return error;
  1866. }
  1867. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  1868. {
  1869. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  1870. if (sbinfo->max_blocks != shmem_default_max_blocks())
  1871. seq_printf(seq, ",size=%luk",
  1872. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  1873. if (sbinfo->max_inodes != shmem_default_max_inodes())
  1874. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  1875. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  1876. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  1877. if (sbinfo->uid != 0)
  1878. seq_printf(seq, ",uid=%u", sbinfo->uid);
  1879. if (sbinfo->gid != 0)
  1880. seq_printf(seq, ",gid=%u", sbinfo->gid);
  1881. shmem_show_mpol(seq, sbinfo->mpol);
  1882. return 0;
  1883. }
  1884. #endif /* CONFIG_TMPFS */
  1885. static void shmem_put_super(struct super_block *sb)
  1886. {
  1887. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1888. percpu_counter_destroy(&sbinfo->used_blocks);
  1889. kfree(sbinfo);
  1890. sb->s_fs_info = NULL;
  1891. }
  1892. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  1893. {
  1894. struct inode *inode;
  1895. struct dentry *root;
  1896. struct shmem_sb_info *sbinfo;
  1897. int err = -ENOMEM;
  1898. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1899. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  1900. L1_CACHE_BYTES), GFP_KERNEL);
  1901. if (!sbinfo)
  1902. return -ENOMEM;
  1903. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  1904. sbinfo->uid = current_fsuid();
  1905. sbinfo->gid = current_fsgid();
  1906. sb->s_fs_info = sbinfo;
  1907. #ifdef CONFIG_TMPFS
  1908. /*
  1909. * Per default we only allow half of the physical ram per
  1910. * tmpfs instance, limiting inodes to one per page of lowmem;
  1911. * but the internal instance is left unlimited.
  1912. */
  1913. if (!(sb->s_flags & MS_NOUSER)) {
  1914. sbinfo->max_blocks = shmem_default_max_blocks();
  1915. sbinfo->max_inodes = shmem_default_max_inodes();
  1916. if (shmem_parse_options(data, sbinfo, false)) {
  1917. err = -EINVAL;
  1918. goto failed;
  1919. }
  1920. }
  1921. sb->s_export_op = &shmem_export_ops;
  1922. #else
  1923. sb->s_flags |= MS_NOUSER;
  1924. #endif
  1925. spin_lock_init(&sbinfo->stat_lock);
  1926. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  1927. goto failed;
  1928. sbinfo->free_inodes = sbinfo->max_inodes;
  1929. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1930. sb->s_blocksize = PAGE_CACHE_SIZE;
  1931. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1932. sb->s_magic = TMPFS_MAGIC;
  1933. sb->s_op = &shmem_ops;
  1934. sb->s_time_gran = 1;
  1935. #ifdef CONFIG_TMPFS_XATTR
  1936. sb->s_xattr = shmem_xattr_handlers;
  1937. #endif
  1938. #ifdef CONFIG_TMPFS_POSIX_ACL
  1939. sb->s_flags |= MS_POSIXACL;
  1940. #endif
  1941. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  1942. if (!inode)
  1943. goto failed;
  1944. inode->i_uid = sbinfo->uid;
  1945. inode->i_gid = sbinfo->gid;
  1946. root = d_alloc_root(inode);
  1947. if (!root)
  1948. goto failed_iput;
  1949. sb->s_root = root;
  1950. return 0;
  1951. failed_iput:
  1952. iput(inode);
  1953. failed:
  1954. shmem_put_super(sb);
  1955. return err;
  1956. }
  1957. static struct kmem_cache *shmem_inode_cachep;
  1958. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1959. {
  1960. struct shmem_inode_info *info;
  1961. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  1962. if (!info)
  1963. return NULL;
  1964. return &info->vfs_inode;
  1965. }
  1966. static void shmem_destroy_callback(struct rcu_head *head)
  1967. {
  1968. struct inode *inode = container_of(head, struct inode, i_rcu);
  1969. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1970. }
  1971. static void shmem_destroy_inode(struct inode *inode)
  1972. {
  1973. if (S_ISREG(inode->i_mode))
  1974. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1975. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  1976. }
  1977. static void shmem_init_inode(void *foo)
  1978. {
  1979. struct shmem_inode_info *info = foo;
  1980. inode_init_once(&info->vfs_inode);
  1981. }
  1982. static int shmem_init_inodecache(void)
  1983. {
  1984. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1985. sizeof(struct shmem_inode_info),
  1986. 0, SLAB_PANIC, shmem_init_inode);
  1987. return 0;
  1988. }
  1989. static void shmem_destroy_inodecache(void)
  1990. {
  1991. kmem_cache_destroy(shmem_inode_cachep);
  1992. }
  1993. static const struct address_space_operations shmem_aops = {
  1994. .writepage = shmem_writepage,
  1995. .set_page_dirty = __set_page_dirty_no_writeback,
  1996. #ifdef CONFIG_TMPFS
  1997. .write_begin = shmem_write_begin,
  1998. .write_end = shmem_write_end,
  1999. #endif
  2000. .migratepage = migrate_page,
  2001. .error_remove_page = generic_error_remove_page,
  2002. };
  2003. static const struct file_operations shmem_file_operations = {
  2004. .mmap = shmem_mmap,
  2005. #ifdef CONFIG_TMPFS
  2006. .llseek = generic_file_llseek,
  2007. .read = do_sync_read,
  2008. .write = do_sync_write,
  2009. .aio_read = shmem_file_aio_read,
  2010. .aio_write = generic_file_aio_write,
  2011. .fsync = noop_fsync,
  2012. .splice_read = shmem_file_splice_read,
  2013. .splice_write = generic_file_splice_write,
  2014. #endif
  2015. };
  2016. static const struct inode_operations shmem_inode_operations = {
  2017. .setattr = shmem_setattr,
  2018. .truncate_range = shmem_truncate_range,
  2019. #ifdef CONFIG_TMPFS_XATTR
  2020. .setxattr = shmem_setxattr,
  2021. .getxattr = shmem_getxattr,
  2022. .listxattr = shmem_listxattr,
  2023. .removexattr = shmem_removexattr,
  2024. #endif
  2025. };
  2026. static const struct inode_operations shmem_dir_inode_operations = {
  2027. #ifdef CONFIG_TMPFS
  2028. .create = shmem_create,
  2029. .lookup = simple_lookup,
  2030. .link = shmem_link,
  2031. .unlink = shmem_unlink,
  2032. .symlink = shmem_symlink,
  2033. .mkdir = shmem_mkdir,
  2034. .rmdir = shmem_rmdir,
  2035. .mknod = shmem_mknod,
  2036. .rename = shmem_rename,
  2037. #endif
  2038. #ifdef CONFIG_TMPFS_XATTR
  2039. .setxattr = shmem_setxattr,
  2040. .getxattr = shmem_getxattr,
  2041. .listxattr = shmem_listxattr,
  2042. .removexattr = shmem_removexattr,
  2043. #endif
  2044. #ifdef CONFIG_TMPFS_POSIX_ACL
  2045. .setattr = shmem_setattr,
  2046. #endif
  2047. };
  2048. static const struct inode_operations shmem_special_inode_operations = {
  2049. #ifdef CONFIG_TMPFS_XATTR
  2050. .setxattr = shmem_setxattr,
  2051. .getxattr = shmem_getxattr,
  2052. .listxattr = shmem_listxattr,
  2053. .removexattr = shmem_removexattr,
  2054. #endif
  2055. #ifdef CONFIG_TMPFS_POSIX_ACL
  2056. .setattr = shmem_setattr,
  2057. #endif
  2058. };
  2059. static const struct super_operations shmem_ops = {
  2060. .alloc_inode = shmem_alloc_inode,
  2061. .destroy_inode = shmem_destroy_inode,
  2062. #ifdef CONFIG_TMPFS
  2063. .statfs = shmem_statfs,
  2064. .remount_fs = shmem_remount_fs,
  2065. .show_options = shmem_show_options,
  2066. #endif
  2067. .evict_inode = shmem_evict_inode,
  2068. .drop_inode = generic_delete_inode,
  2069. .put_super = shmem_put_super,
  2070. };
  2071. static const struct vm_operations_struct shmem_vm_ops = {
  2072. .fault = shmem_fault,
  2073. #ifdef CONFIG_NUMA
  2074. .set_policy = shmem_set_policy,
  2075. .get_policy = shmem_get_policy,
  2076. #endif
  2077. };
  2078. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2079. int flags, const char *dev_name, void *data)
  2080. {
  2081. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2082. }
  2083. static struct file_system_type shmem_fs_type = {
  2084. .owner = THIS_MODULE,
  2085. .name = "tmpfs",
  2086. .mount = shmem_mount,
  2087. .kill_sb = kill_litter_super,
  2088. };
  2089. int __init shmem_init(void)
  2090. {
  2091. int error;
  2092. error = bdi_init(&shmem_backing_dev_info);
  2093. if (error)
  2094. goto out4;
  2095. error = shmem_init_inodecache();
  2096. if (error)
  2097. goto out3;
  2098. error = register_filesystem(&shmem_fs_type);
  2099. if (error) {
  2100. printk(KERN_ERR "Could not register tmpfs\n");
  2101. goto out2;
  2102. }
  2103. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2104. shmem_fs_type.name, NULL);
  2105. if (IS_ERR(shm_mnt)) {
  2106. error = PTR_ERR(shm_mnt);
  2107. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2108. goto out1;
  2109. }
  2110. return 0;
  2111. out1:
  2112. unregister_filesystem(&shmem_fs_type);
  2113. out2:
  2114. shmem_destroy_inodecache();
  2115. out3:
  2116. bdi_destroy(&shmem_backing_dev_info);
  2117. out4:
  2118. shm_mnt = ERR_PTR(error);
  2119. return error;
  2120. }
  2121. #else /* !CONFIG_SHMEM */
  2122. /*
  2123. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2124. *
  2125. * This is intended for small system where the benefits of the full
  2126. * shmem code (swap-backed and resource-limited) are outweighed by
  2127. * their complexity. On systems without swap this code should be
  2128. * effectively equivalent, but much lighter weight.
  2129. */
  2130. #include <linux/ramfs.h>
  2131. static struct file_system_type shmem_fs_type = {
  2132. .name = "tmpfs",
  2133. .mount = ramfs_mount,
  2134. .kill_sb = kill_litter_super,
  2135. };
  2136. int __init shmem_init(void)
  2137. {
  2138. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2139. shm_mnt = kern_mount(&shmem_fs_type);
  2140. BUG_ON(IS_ERR(shm_mnt));
  2141. return 0;
  2142. }
  2143. int shmem_unuse(swp_entry_t swap, struct page *page)
  2144. {
  2145. return 0;
  2146. }
  2147. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2148. {
  2149. return 0;
  2150. }
  2151. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2152. {
  2153. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2154. }
  2155. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2156. #define shmem_vm_ops generic_file_vm_ops
  2157. #define shmem_file_operations ramfs_file_operations
  2158. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2159. #define shmem_acct_size(flags, size) 0
  2160. #define shmem_unacct_size(flags, size) do {} while (0)
  2161. #endif /* CONFIG_SHMEM */
  2162. /* common code */
  2163. /**
  2164. * shmem_file_setup - get an unlinked file living in tmpfs
  2165. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2166. * @size: size to be set for the file
  2167. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2168. */
  2169. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2170. {
  2171. int error;
  2172. struct file *file;
  2173. struct inode *inode;
  2174. struct path path;
  2175. struct dentry *root;
  2176. struct qstr this;
  2177. if (IS_ERR(shm_mnt))
  2178. return (void *)shm_mnt;
  2179. if (size < 0 || size > MAX_LFS_FILESIZE)
  2180. return ERR_PTR(-EINVAL);
  2181. if (shmem_acct_size(flags, size))
  2182. return ERR_PTR(-ENOMEM);
  2183. error = -ENOMEM;
  2184. this.name = name;
  2185. this.len = strlen(name);
  2186. this.hash = 0; /* will go */
  2187. root = shm_mnt->mnt_root;
  2188. path.dentry = d_alloc(root, &this);
  2189. if (!path.dentry)
  2190. goto put_memory;
  2191. path.mnt = mntget(shm_mnt);
  2192. error = -ENOSPC;
  2193. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2194. if (!inode)
  2195. goto put_dentry;
  2196. d_instantiate(path.dentry, inode);
  2197. inode->i_size = size;
  2198. clear_nlink(inode); /* It is unlinked */
  2199. #ifndef CONFIG_MMU
  2200. error = ramfs_nommu_expand_for_mapping(inode, size);
  2201. if (error)
  2202. goto put_dentry;
  2203. #endif
  2204. error = -ENFILE;
  2205. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2206. &shmem_file_operations);
  2207. if (!file)
  2208. goto put_dentry;
  2209. return file;
  2210. put_dentry:
  2211. path_put(&path);
  2212. put_memory:
  2213. shmem_unacct_size(flags, size);
  2214. return ERR_PTR(error);
  2215. }
  2216. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2217. /**
  2218. * shmem_zero_setup - setup a shared anonymous mapping
  2219. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2220. */
  2221. int shmem_zero_setup(struct vm_area_struct *vma)
  2222. {
  2223. struct file *file;
  2224. loff_t size = vma->vm_end - vma->vm_start;
  2225. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2226. if (IS_ERR(file))
  2227. return PTR_ERR(file);
  2228. if (vma->vm_file)
  2229. fput(vma->vm_file);
  2230. vma->vm_file = file;
  2231. vma->vm_ops = &shmem_vm_ops;
  2232. vma->vm_flags |= VM_CAN_NONLINEAR;
  2233. return 0;
  2234. }
  2235. /**
  2236. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2237. * @mapping: the page's address_space
  2238. * @index: the page index
  2239. * @gfp: the page allocator flags to use if allocating
  2240. *
  2241. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2242. * with any new page allocations done using the specified allocation flags.
  2243. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2244. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2245. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2246. *
  2247. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2248. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2249. */
  2250. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2251. pgoff_t index, gfp_t gfp)
  2252. {
  2253. #ifdef CONFIG_SHMEM
  2254. struct inode *inode = mapping->host;
  2255. struct page *page;
  2256. int error;
  2257. BUG_ON(mapping->a_ops != &shmem_aops);
  2258. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2259. if (error)
  2260. page = ERR_PTR(error);
  2261. else
  2262. unlock_page(page);
  2263. return page;
  2264. #else
  2265. /*
  2266. * The tiny !SHMEM case uses ramfs without swap
  2267. */
  2268. return read_cache_page_gfp(mapping, index, gfp);
  2269. #endif
  2270. }
  2271. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);