shmem.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  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/module.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. scan_mapping_unevictable_pages(file->f_mapping);
  954. }
  955. retval = 0;
  956. out_nomem:
  957. spin_unlock(&info->lock);
  958. return retval;
  959. }
  960. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  961. {
  962. file_accessed(file);
  963. vma->vm_ops = &shmem_vm_ops;
  964. vma->vm_flags |= VM_CAN_NONLINEAR;
  965. return 0;
  966. }
  967. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  968. int mode, dev_t dev, unsigned long flags)
  969. {
  970. struct inode *inode;
  971. struct shmem_inode_info *info;
  972. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  973. if (shmem_reserve_inode(sb))
  974. return NULL;
  975. inode = new_inode(sb);
  976. if (inode) {
  977. inode->i_ino = get_next_ino();
  978. inode_init_owner(inode, dir, mode);
  979. inode->i_blocks = 0;
  980. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  981. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  982. inode->i_generation = get_seconds();
  983. info = SHMEM_I(inode);
  984. memset(info, 0, (char *)inode - (char *)info);
  985. spin_lock_init(&info->lock);
  986. info->flags = flags & VM_NORESERVE;
  987. INIT_LIST_HEAD(&info->swaplist);
  988. INIT_LIST_HEAD(&info->xattr_list);
  989. cache_no_acl(inode);
  990. switch (mode & S_IFMT) {
  991. default:
  992. inode->i_op = &shmem_special_inode_operations;
  993. init_special_inode(inode, mode, dev);
  994. break;
  995. case S_IFREG:
  996. inode->i_mapping->a_ops = &shmem_aops;
  997. inode->i_op = &shmem_inode_operations;
  998. inode->i_fop = &shmem_file_operations;
  999. mpol_shared_policy_init(&info->policy,
  1000. shmem_get_sbmpol(sbinfo));
  1001. break;
  1002. case S_IFDIR:
  1003. inc_nlink(inode);
  1004. /* Some things misbehave if size == 0 on a directory */
  1005. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1006. inode->i_op = &shmem_dir_inode_operations;
  1007. inode->i_fop = &simple_dir_operations;
  1008. break;
  1009. case S_IFLNK:
  1010. /*
  1011. * Must not load anything in the rbtree,
  1012. * mpol_free_shared_policy will not be called.
  1013. */
  1014. mpol_shared_policy_init(&info->policy, NULL);
  1015. break;
  1016. }
  1017. } else
  1018. shmem_free_inode(sb);
  1019. return inode;
  1020. }
  1021. #ifdef CONFIG_TMPFS
  1022. static const struct inode_operations shmem_symlink_inode_operations;
  1023. static const struct inode_operations shmem_short_symlink_operations;
  1024. static int
  1025. shmem_write_begin(struct file *file, struct address_space *mapping,
  1026. loff_t pos, unsigned len, unsigned flags,
  1027. struct page **pagep, void **fsdata)
  1028. {
  1029. struct inode *inode = mapping->host;
  1030. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1031. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1032. }
  1033. static int
  1034. shmem_write_end(struct file *file, struct address_space *mapping,
  1035. loff_t pos, unsigned len, unsigned copied,
  1036. struct page *page, void *fsdata)
  1037. {
  1038. struct inode *inode = mapping->host;
  1039. if (pos + copied > inode->i_size)
  1040. i_size_write(inode, pos + copied);
  1041. set_page_dirty(page);
  1042. unlock_page(page);
  1043. page_cache_release(page);
  1044. return copied;
  1045. }
  1046. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1047. {
  1048. struct inode *inode = filp->f_path.dentry->d_inode;
  1049. struct address_space *mapping = inode->i_mapping;
  1050. pgoff_t index;
  1051. unsigned long offset;
  1052. enum sgp_type sgp = SGP_READ;
  1053. /*
  1054. * Might this read be for a stacking filesystem? Then when reading
  1055. * holes of a sparse file, we actually need to allocate those pages,
  1056. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1057. */
  1058. if (segment_eq(get_fs(), KERNEL_DS))
  1059. sgp = SGP_DIRTY;
  1060. index = *ppos >> PAGE_CACHE_SHIFT;
  1061. offset = *ppos & ~PAGE_CACHE_MASK;
  1062. for (;;) {
  1063. struct page *page = NULL;
  1064. pgoff_t end_index;
  1065. unsigned long nr, ret;
  1066. loff_t i_size = i_size_read(inode);
  1067. end_index = i_size >> PAGE_CACHE_SHIFT;
  1068. if (index > end_index)
  1069. break;
  1070. if (index == end_index) {
  1071. nr = i_size & ~PAGE_CACHE_MASK;
  1072. if (nr <= offset)
  1073. break;
  1074. }
  1075. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1076. if (desc->error) {
  1077. if (desc->error == -EINVAL)
  1078. desc->error = 0;
  1079. break;
  1080. }
  1081. if (page)
  1082. unlock_page(page);
  1083. /*
  1084. * We must evaluate after, since reads (unlike writes)
  1085. * are called without i_mutex protection against truncate
  1086. */
  1087. nr = PAGE_CACHE_SIZE;
  1088. i_size = i_size_read(inode);
  1089. end_index = i_size >> PAGE_CACHE_SHIFT;
  1090. if (index == end_index) {
  1091. nr = i_size & ~PAGE_CACHE_MASK;
  1092. if (nr <= offset) {
  1093. if (page)
  1094. page_cache_release(page);
  1095. break;
  1096. }
  1097. }
  1098. nr -= offset;
  1099. if (page) {
  1100. /*
  1101. * If users can be writing to this page using arbitrary
  1102. * virtual addresses, take care about potential aliasing
  1103. * before reading the page on the kernel side.
  1104. */
  1105. if (mapping_writably_mapped(mapping))
  1106. flush_dcache_page(page);
  1107. /*
  1108. * Mark the page accessed if we read the beginning.
  1109. */
  1110. if (!offset)
  1111. mark_page_accessed(page);
  1112. } else {
  1113. page = ZERO_PAGE(0);
  1114. page_cache_get(page);
  1115. }
  1116. /*
  1117. * Ok, we have the page, and it's up-to-date, so
  1118. * now we can copy it to user space...
  1119. *
  1120. * The actor routine returns how many bytes were actually used..
  1121. * NOTE! This may not be the same as how much of a user buffer
  1122. * we filled up (we may be padding etc), so we can only update
  1123. * "pos" here (the actor routine has to update the user buffer
  1124. * pointers and the remaining count).
  1125. */
  1126. ret = actor(desc, page, offset, nr);
  1127. offset += ret;
  1128. index += offset >> PAGE_CACHE_SHIFT;
  1129. offset &= ~PAGE_CACHE_MASK;
  1130. page_cache_release(page);
  1131. if (ret != nr || !desc->count)
  1132. break;
  1133. cond_resched();
  1134. }
  1135. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1136. file_accessed(filp);
  1137. }
  1138. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1139. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1140. {
  1141. struct file *filp = iocb->ki_filp;
  1142. ssize_t retval;
  1143. unsigned long seg;
  1144. size_t count;
  1145. loff_t *ppos = &iocb->ki_pos;
  1146. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1147. if (retval)
  1148. return retval;
  1149. for (seg = 0; seg < nr_segs; seg++) {
  1150. read_descriptor_t desc;
  1151. desc.written = 0;
  1152. desc.arg.buf = iov[seg].iov_base;
  1153. desc.count = iov[seg].iov_len;
  1154. if (desc.count == 0)
  1155. continue;
  1156. desc.error = 0;
  1157. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1158. retval += desc.written;
  1159. if (desc.error) {
  1160. retval = retval ?: desc.error;
  1161. break;
  1162. }
  1163. if (desc.count > 0)
  1164. break;
  1165. }
  1166. return retval;
  1167. }
  1168. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1169. struct pipe_inode_info *pipe, size_t len,
  1170. unsigned int flags)
  1171. {
  1172. struct address_space *mapping = in->f_mapping;
  1173. struct inode *inode = mapping->host;
  1174. unsigned int loff, nr_pages, req_pages;
  1175. struct page *pages[PIPE_DEF_BUFFERS];
  1176. struct partial_page partial[PIPE_DEF_BUFFERS];
  1177. struct page *page;
  1178. pgoff_t index, end_index;
  1179. loff_t isize, left;
  1180. int error, page_nr;
  1181. struct splice_pipe_desc spd = {
  1182. .pages = pages,
  1183. .partial = partial,
  1184. .flags = flags,
  1185. .ops = &page_cache_pipe_buf_ops,
  1186. .spd_release = spd_release_page,
  1187. };
  1188. isize = i_size_read(inode);
  1189. if (unlikely(*ppos >= isize))
  1190. return 0;
  1191. left = isize - *ppos;
  1192. if (unlikely(left < len))
  1193. len = left;
  1194. if (splice_grow_spd(pipe, &spd))
  1195. return -ENOMEM;
  1196. index = *ppos >> PAGE_CACHE_SHIFT;
  1197. loff = *ppos & ~PAGE_CACHE_MASK;
  1198. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1199. nr_pages = min(req_pages, pipe->buffers);
  1200. spd.nr_pages = find_get_pages_contig(mapping, index,
  1201. nr_pages, spd.pages);
  1202. index += spd.nr_pages;
  1203. error = 0;
  1204. while (spd.nr_pages < nr_pages) {
  1205. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1206. if (error)
  1207. break;
  1208. unlock_page(page);
  1209. spd.pages[spd.nr_pages++] = page;
  1210. index++;
  1211. }
  1212. index = *ppos >> PAGE_CACHE_SHIFT;
  1213. nr_pages = spd.nr_pages;
  1214. spd.nr_pages = 0;
  1215. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1216. unsigned int this_len;
  1217. if (!len)
  1218. break;
  1219. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1220. page = spd.pages[page_nr];
  1221. if (!PageUptodate(page) || page->mapping != mapping) {
  1222. error = shmem_getpage(inode, index, &page,
  1223. SGP_CACHE, NULL);
  1224. if (error)
  1225. break;
  1226. unlock_page(page);
  1227. page_cache_release(spd.pages[page_nr]);
  1228. spd.pages[page_nr] = page;
  1229. }
  1230. isize = i_size_read(inode);
  1231. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1232. if (unlikely(!isize || index > end_index))
  1233. break;
  1234. if (end_index == index) {
  1235. unsigned int plen;
  1236. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1237. if (plen <= loff)
  1238. break;
  1239. this_len = min(this_len, plen - loff);
  1240. len = this_len;
  1241. }
  1242. spd.partial[page_nr].offset = loff;
  1243. spd.partial[page_nr].len = this_len;
  1244. len -= this_len;
  1245. loff = 0;
  1246. spd.nr_pages++;
  1247. index++;
  1248. }
  1249. while (page_nr < nr_pages)
  1250. page_cache_release(spd.pages[page_nr++]);
  1251. if (spd.nr_pages)
  1252. error = splice_to_pipe(pipe, &spd);
  1253. splice_shrink_spd(pipe, &spd);
  1254. if (error > 0) {
  1255. *ppos += error;
  1256. file_accessed(in);
  1257. }
  1258. return error;
  1259. }
  1260. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1261. {
  1262. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1263. buf->f_type = TMPFS_MAGIC;
  1264. buf->f_bsize = PAGE_CACHE_SIZE;
  1265. buf->f_namelen = NAME_MAX;
  1266. if (sbinfo->max_blocks) {
  1267. buf->f_blocks = sbinfo->max_blocks;
  1268. buf->f_bavail =
  1269. buf->f_bfree = sbinfo->max_blocks -
  1270. percpu_counter_sum(&sbinfo->used_blocks);
  1271. }
  1272. if (sbinfo->max_inodes) {
  1273. buf->f_files = sbinfo->max_inodes;
  1274. buf->f_ffree = sbinfo->free_inodes;
  1275. }
  1276. /* else leave those fields 0 like simple_statfs */
  1277. return 0;
  1278. }
  1279. /*
  1280. * File creation. Allocate an inode, and we're done..
  1281. */
  1282. static int
  1283. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1284. {
  1285. struct inode *inode;
  1286. int error = -ENOSPC;
  1287. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1288. if (inode) {
  1289. error = security_inode_init_security(inode, dir,
  1290. &dentry->d_name, NULL,
  1291. NULL, NULL);
  1292. if (error) {
  1293. if (error != -EOPNOTSUPP) {
  1294. iput(inode);
  1295. return error;
  1296. }
  1297. }
  1298. #ifdef CONFIG_TMPFS_POSIX_ACL
  1299. error = generic_acl_init(inode, dir);
  1300. if (error) {
  1301. iput(inode);
  1302. return error;
  1303. }
  1304. #else
  1305. error = 0;
  1306. #endif
  1307. dir->i_size += BOGO_DIRENT_SIZE;
  1308. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1309. d_instantiate(dentry, inode);
  1310. dget(dentry); /* Extra count - pin the dentry in core */
  1311. }
  1312. return error;
  1313. }
  1314. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1315. {
  1316. int error;
  1317. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1318. return error;
  1319. inc_nlink(dir);
  1320. return 0;
  1321. }
  1322. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1323. struct nameidata *nd)
  1324. {
  1325. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1326. }
  1327. /*
  1328. * Link a file..
  1329. */
  1330. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1331. {
  1332. struct inode *inode = old_dentry->d_inode;
  1333. int ret;
  1334. /*
  1335. * No ordinary (disk based) filesystem counts links as inodes;
  1336. * but each new link needs a new dentry, pinning lowmem, and
  1337. * tmpfs dentries cannot be pruned until they are unlinked.
  1338. */
  1339. ret = shmem_reserve_inode(inode->i_sb);
  1340. if (ret)
  1341. goto out;
  1342. dir->i_size += BOGO_DIRENT_SIZE;
  1343. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1344. inc_nlink(inode);
  1345. ihold(inode); /* New dentry reference */
  1346. dget(dentry); /* Extra pinning count for the created dentry */
  1347. d_instantiate(dentry, inode);
  1348. out:
  1349. return ret;
  1350. }
  1351. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1352. {
  1353. struct inode *inode = dentry->d_inode;
  1354. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1355. shmem_free_inode(inode->i_sb);
  1356. dir->i_size -= BOGO_DIRENT_SIZE;
  1357. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1358. drop_nlink(inode);
  1359. dput(dentry); /* Undo the count from "create" - this does all the work */
  1360. return 0;
  1361. }
  1362. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1363. {
  1364. if (!simple_empty(dentry))
  1365. return -ENOTEMPTY;
  1366. drop_nlink(dentry->d_inode);
  1367. drop_nlink(dir);
  1368. return shmem_unlink(dir, dentry);
  1369. }
  1370. /*
  1371. * The VFS layer already does all the dentry stuff for rename,
  1372. * we just have to decrement the usage count for the target if
  1373. * it exists so that the VFS layer correctly free's it when it
  1374. * gets overwritten.
  1375. */
  1376. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1377. {
  1378. struct inode *inode = old_dentry->d_inode;
  1379. int they_are_dirs = S_ISDIR(inode->i_mode);
  1380. if (!simple_empty(new_dentry))
  1381. return -ENOTEMPTY;
  1382. if (new_dentry->d_inode) {
  1383. (void) shmem_unlink(new_dir, new_dentry);
  1384. if (they_are_dirs)
  1385. drop_nlink(old_dir);
  1386. } else if (they_are_dirs) {
  1387. drop_nlink(old_dir);
  1388. inc_nlink(new_dir);
  1389. }
  1390. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1391. new_dir->i_size += BOGO_DIRENT_SIZE;
  1392. old_dir->i_ctime = old_dir->i_mtime =
  1393. new_dir->i_ctime = new_dir->i_mtime =
  1394. inode->i_ctime = CURRENT_TIME;
  1395. return 0;
  1396. }
  1397. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1398. {
  1399. int error;
  1400. int len;
  1401. struct inode *inode;
  1402. struct page *page;
  1403. char *kaddr;
  1404. struct shmem_inode_info *info;
  1405. len = strlen(symname) + 1;
  1406. if (len > PAGE_CACHE_SIZE)
  1407. return -ENAMETOOLONG;
  1408. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1409. if (!inode)
  1410. return -ENOSPC;
  1411. error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
  1412. NULL, NULL);
  1413. if (error) {
  1414. if (error != -EOPNOTSUPP) {
  1415. iput(inode);
  1416. return error;
  1417. }
  1418. error = 0;
  1419. }
  1420. info = SHMEM_I(inode);
  1421. inode->i_size = len-1;
  1422. if (len <= SHORT_SYMLINK_LEN) {
  1423. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1424. if (!info->symlink) {
  1425. iput(inode);
  1426. return -ENOMEM;
  1427. }
  1428. inode->i_op = &shmem_short_symlink_operations;
  1429. } else {
  1430. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1431. if (error) {
  1432. iput(inode);
  1433. return error;
  1434. }
  1435. inode->i_mapping->a_ops = &shmem_aops;
  1436. inode->i_op = &shmem_symlink_inode_operations;
  1437. kaddr = kmap_atomic(page, KM_USER0);
  1438. memcpy(kaddr, symname, len);
  1439. kunmap_atomic(kaddr, KM_USER0);
  1440. set_page_dirty(page);
  1441. unlock_page(page);
  1442. page_cache_release(page);
  1443. }
  1444. dir->i_size += BOGO_DIRENT_SIZE;
  1445. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1446. d_instantiate(dentry, inode);
  1447. dget(dentry);
  1448. return 0;
  1449. }
  1450. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1451. {
  1452. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1453. return NULL;
  1454. }
  1455. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1456. {
  1457. struct page *page = NULL;
  1458. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1459. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1460. if (page)
  1461. unlock_page(page);
  1462. return page;
  1463. }
  1464. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1465. {
  1466. if (!IS_ERR(nd_get_link(nd))) {
  1467. struct page *page = cookie;
  1468. kunmap(page);
  1469. mark_page_accessed(page);
  1470. page_cache_release(page);
  1471. }
  1472. }
  1473. #ifdef CONFIG_TMPFS_XATTR
  1474. /*
  1475. * Superblocks without xattr inode operations may get some security.* xattr
  1476. * support from the LSM "for free". As soon as we have any other xattrs
  1477. * like ACLs, we also need to implement the security.* handlers at
  1478. * filesystem level, though.
  1479. */
  1480. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1481. void *buffer, size_t size)
  1482. {
  1483. struct shmem_inode_info *info;
  1484. struct shmem_xattr *xattr;
  1485. int ret = -ENODATA;
  1486. info = SHMEM_I(dentry->d_inode);
  1487. spin_lock(&info->lock);
  1488. list_for_each_entry(xattr, &info->xattr_list, list) {
  1489. if (strcmp(name, xattr->name))
  1490. continue;
  1491. ret = xattr->size;
  1492. if (buffer) {
  1493. if (size < xattr->size)
  1494. ret = -ERANGE;
  1495. else
  1496. memcpy(buffer, xattr->value, xattr->size);
  1497. }
  1498. break;
  1499. }
  1500. spin_unlock(&info->lock);
  1501. return ret;
  1502. }
  1503. static int shmem_xattr_set(struct dentry *dentry, const char *name,
  1504. const void *value, size_t size, int flags)
  1505. {
  1506. struct inode *inode = dentry->d_inode;
  1507. struct shmem_inode_info *info = SHMEM_I(inode);
  1508. struct shmem_xattr *xattr;
  1509. struct shmem_xattr *new_xattr = NULL;
  1510. size_t len;
  1511. int err = 0;
  1512. /* value == NULL means remove */
  1513. if (value) {
  1514. /* wrap around? */
  1515. len = sizeof(*new_xattr) + size;
  1516. if (len <= sizeof(*new_xattr))
  1517. return -ENOMEM;
  1518. new_xattr = kmalloc(len, GFP_KERNEL);
  1519. if (!new_xattr)
  1520. return -ENOMEM;
  1521. new_xattr->name = kstrdup(name, GFP_KERNEL);
  1522. if (!new_xattr->name) {
  1523. kfree(new_xattr);
  1524. return -ENOMEM;
  1525. }
  1526. new_xattr->size = size;
  1527. memcpy(new_xattr->value, value, size);
  1528. }
  1529. spin_lock(&info->lock);
  1530. list_for_each_entry(xattr, &info->xattr_list, list) {
  1531. if (!strcmp(name, xattr->name)) {
  1532. if (flags & XATTR_CREATE) {
  1533. xattr = new_xattr;
  1534. err = -EEXIST;
  1535. } else if (new_xattr) {
  1536. list_replace(&xattr->list, &new_xattr->list);
  1537. } else {
  1538. list_del(&xattr->list);
  1539. }
  1540. goto out;
  1541. }
  1542. }
  1543. if (flags & XATTR_REPLACE) {
  1544. xattr = new_xattr;
  1545. err = -ENODATA;
  1546. } else {
  1547. list_add(&new_xattr->list, &info->xattr_list);
  1548. xattr = NULL;
  1549. }
  1550. out:
  1551. spin_unlock(&info->lock);
  1552. if (xattr)
  1553. kfree(xattr->name);
  1554. kfree(xattr);
  1555. return err;
  1556. }
  1557. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1558. #ifdef CONFIG_TMPFS_POSIX_ACL
  1559. &generic_acl_access_handler,
  1560. &generic_acl_default_handler,
  1561. #endif
  1562. NULL
  1563. };
  1564. static int shmem_xattr_validate(const char *name)
  1565. {
  1566. struct { const char *prefix; size_t len; } arr[] = {
  1567. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1568. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1569. };
  1570. int i;
  1571. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1572. size_t preflen = arr[i].len;
  1573. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1574. if (!name[preflen])
  1575. return -EINVAL;
  1576. return 0;
  1577. }
  1578. }
  1579. return -EOPNOTSUPP;
  1580. }
  1581. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1582. void *buffer, size_t size)
  1583. {
  1584. int err;
  1585. /*
  1586. * If this is a request for a synthetic attribute in the system.*
  1587. * namespace use the generic infrastructure to resolve a handler
  1588. * for it via sb->s_xattr.
  1589. */
  1590. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1591. return generic_getxattr(dentry, name, buffer, size);
  1592. err = shmem_xattr_validate(name);
  1593. if (err)
  1594. return err;
  1595. return shmem_xattr_get(dentry, name, buffer, size);
  1596. }
  1597. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1598. const void *value, size_t size, int flags)
  1599. {
  1600. int err;
  1601. /*
  1602. * If this is a request for a synthetic attribute in the system.*
  1603. * namespace use the generic infrastructure to resolve a handler
  1604. * for it via sb->s_xattr.
  1605. */
  1606. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1607. return generic_setxattr(dentry, name, value, size, flags);
  1608. err = shmem_xattr_validate(name);
  1609. if (err)
  1610. return err;
  1611. if (size == 0)
  1612. value = ""; /* empty EA, do not remove */
  1613. return shmem_xattr_set(dentry, name, value, size, flags);
  1614. }
  1615. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1616. {
  1617. int err;
  1618. /*
  1619. * If this is a request for a synthetic attribute in the system.*
  1620. * namespace use the generic infrastructure to resolve a handler
  1621. * for it via sb->s_xattr.
  1622. */
  1623. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1624. return generic_removexattr(dentry, name);
  1625. err = shmem_xattr_validate(name);
  1626. if (err)
  1627. return err;
  1628. return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
  1629. }
  1630. static bool xattr_is_trusted(const char *name)
  1631. {
  1632. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1633. }
  1634. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1635. {
  1636. bool trusted = capable(CAP_SYS_ADMIN);
  1637. struct shmem_xattr *xattr;
  1638. struct shmem_inode_info *info;
  1639. size_t used = 0;
  1640. info = SHMEM_I(dentry->d_inode);
  1641. spin_lock(&info->lock);
  1642. list_for_each_entry(xattr, &info->xattr_list, list) {
  1643. size_t len;
  1644. /* skip "trusted." attributes for unprivileged callers */
  1645. if (!trusted && xattr_is_trusted(xattr->name))
  1646. continue;
  1647. len = strlen(xattr->name) + 1;
  1648. used += len;
  1649. if (buffer) {
  1650. if (size < used) {
  1651. used = -ERANGE;
  1652. break;
  1653. }
  1654. memcpy(buffer, xattr->name, len);
  1655. buffer += len;
  1656. }
  1657. }
  1658. spin_unlock(&info->lock);
  1659. return used;
  1660. }
  1661. #endif /* CONFIG_TMPFS_XATTR */
  1662. static const struct inode_operations shmem_short_symlink_operations = {
  1663. .readlink = generic_readlink,
  1664. .follow_link = shmem_follow_short_symlink,
  1665. #ifdef CONFIG_TMPFS_XATTR
  1666. .setxattr = shmem_setxattr,
  1667. .getxattr = shmem_getxattr,
  1668. .listxattr = shmem_listxattr,
  1669. .removexattr = shmem_removexattr,
  1670. #endif
  1671. };
  1672. static const struct inode_operations shmem_symlink_inode_operations = {
  1673. .readlink = generic_readlink,
  1674. .follow_link = shmem_follow_link,
  1675. .put_link = shmem_put_link,
  1676. #ifdef CONFIG_TMPFS_XATTR
  1677. .setxattr = shmem_setxattr,
  1678. .getxattr = shmem_getxattr,
  1679. .listxattr = shmem_listxattr,
  1680. .removexattr = shmem_removexattr,
  1681. #endif
  1682. };
  1683. static struct dentry *shmem_get_parent(struct dentry *child)
  1684. {
  1685. return ERR_PTR(-ESTALE);
  1686. }
  1687. static int shmem_match(struct inode *ino, void *vfh)
  1688. {
  1689. __u32 *fh = vfh;
  1690. __u64 inum = fh[2];
  1691. inum = (inum << 32) | fh[1];
  1692. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1693. }
  1694. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1695. struct fid *fid, int fh_len, int fh_type)
  1696. {
  1697. struct inode *inode;
  1698. struct dentry *dentry = NULL;
  1699. u64 inum = fid->raw[2];
  1700. inum = (inum << 32) | fid->raw[1];
  1701. if (fh_len < 3)
  1702. return NULL;
  1703. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1704. shmem_match, fid->raw);
  1705. if (inode) {
  1706. dentry = d_find_alias(inode);
  1707. iput(inode);
  1708. }
  1709. return dentry;
  1710. }
  1711. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1712. int connectable)
  1713. {
  1714. struct inode *inode = dentry->d_inode;
  1715. if (*len < 3) {
  1716. *len = 3;
  1717. return 255;
  1718. }
  1719. if (inode_unhashed(inode)) {
  1720. /* Unfortunately insert_inode_hash is not idempotent,
  1721. * so as we hash inodes here rather than at creation
  1722. * time, we need a lock to ensure we only try
  1723. * to do it once
  1724. */
  1725. static DEFINE_SPINLOCK(lock);
  1726. spin_lock(&lock);
  1727. if (inode_unhashed(inode))
  1728. __insert_inode_hash(inode,
  1729. inode->i_ino + inode->i_generation);
  1730. spin_unlock(&lock);
  1731. }
  1732. fh[0] = inode->i_generation;
  1733. fh[1] = inode->i_ino;
  1734. fh[2] = ((__u64)inode->i_ino) >> 32;
  1735. *len = 3;
  1736. return 1;
  1737. }
  1738. static const struct export_operations shmem_export_ops = {
  1739. .get_parent = shmem_get_parent,
  1740. .encode_fh = shmem_encode_fh,
  1741. .fh_to_dentry = shmem_fh_to_dentry,
  1742. };
  1743. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1744. bool remount)
  1745. {
  1746. char *this_char, *value, *rest;
  1747. while (options != NULL) {
  1748. this_char = options;
  1749. for (;;) {
  1750. /*
  1751. * NUL-terminate this option: unfortunately,
  1752. * mount options form a comma-separated list,
  1753. * but mpol's nodelist may also contain commas.
  1754. */
  1755. options = strchr(options, ',');
  1756. if (options == NULL)
  1757. break;
  1758. options++;
  1759. if (!isdigit(*options)) {
  1760. options[-1] = '\0';
  1761. break;
  1762. }
  1763. }
  1764. if (!*this_char)
  1765. continue;
  1766. if ((value = strchr(this_char,'=')) != NULL) {
  1767. *value++ = 0;
  1768. } else {
  1769. printk(KERN_ERR
  1770. "tmpfs: No value for mount option '%s'\n",
  1771. this_char);
  1772. return 1;
  1773. }
  1774. if (!strcmp(this_char,"size")) {
  1775. unsigned long long size;
  1776. size = memparse(value,&rest);
  1777. if (*rest == '%') {
  1778. size <<= PAGE_SHIFT;
  1779. size *= totalram_pages;
  1780. do_div(size, 100);
  1781. rest++;
  1782. }
  1783. if (*rest)
  1784. goto bad_val;
  1785. sbinfo->max_blocks =
  1786. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1787. } else if (!strcmp(this_char,"nr_blocks")) {
  1788. sbinfo->max_blocks = memparse(value, &rest);
  1789. if (*rest)
  1790. goto bad_val;
  1791. } else if (!strcmp(this_char,"nr_inodes")) {
  1792. sbinfo->max_inodes = memparse(value, &rest);
  1793. if (*rest)
  1794. goto bad_val;
  1795. } else if (!strcmp(this_char,"mode")) {
  1796. if (remount)
  1797. continue;
  1798. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1799. if (*rest)
  1800. goto bad_val;
  1801. } else if (!strcmp(this_char,"uid")) {
  1802. if (remount)
  1803. continue;
  1804. sbinfo->uid = simple_strtoul(value, &rest, 0);
  1805. if (*rest)
  1806. goto bad_val;
  1807. } else if (!strcmp(this_char,"gid")) {
  1808. if (remount)
  1809. continue;
  1810. sbinfo->gid = simple_strtoul(value, &rest, 0);
  1811. if (*rest)
  1812. goto bad_val;
  1813. } else if (!strcmp(this_char,"mpol")) {
  1814. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  1815. goto bad_val;
  1816. } else {
  1817. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1818. this_char);
  1819. return 1;
  1820. }
  1821. }
  1822. return 0;
  1823. bad_val:
  1824. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1825. value, this_char);
  1826. return 1;
  1827. }
  1828. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1829. {
  1830. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1831. struct shmem_sb_info config = *sbinfo;
  1832. unsigned long inodes;
  1833. int error = -EINVAL;
  1834. if (shmem_parse_options(data, &config, true))
  1835. return error;
  1836. spin_lock(&sbinfo->stat_lock);
  1837. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1838. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  1839. goto out;
  1840. if (config.max_inodes < inodes)
  1841. goto out;
  1842. /*
  1843. * Those tests disallow limited->unlimited while any are in use;
  1844. * but we must separately disallow unlimited->limited, because
  1845. * in that case we have no record of how much is already in use.
  1846. */
  1847. if (config.max_blocks && !sbinfo->max_blocks)
  1848. goto out;
  1849. if (config.max_inodes && !sbinfo->max_inodes)
  1850. goto out;
  1851. error = 0;
  1852. sbinfo->max_blocks = config.max_blocks;
  1853. sbinfo->max_inodes = config.max_inodes;
  1854. sbinfo->free_inodes = config.max_inodes - inodes;
  1855. mpol_put(sbinfo->mpol);
  1856. sbinfo->mpol = config.mpol; /* transfers initial ref */
  1857. out:
  1858. spin_unlock(&sbinfo->stat_lock);
  1859. return error;
  1860. }
  1861. static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
  1862. {
  1863. struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
  1864. if (sbinfo->max_blocks != shmem_default_max_blocks())
  1865. seq_printf(seq, ",size=%luk",
  1866. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  1867. if (sbinfo->max_inodes != shmem_default_max_inodes())
  1868. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  1869. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  1870. seq_printf(seq, ",mode=%03o", sbinfo->mode);
  1871. if (sbinfo->uid != 0)
  1872. seq_printf(seq, ",uid=%u", sbinfo->uid);
  1873. if (sbinfo->gid != 0)
  1874. seq_printf(seq, ",gid=%u", sbinfo->gid);
  1875. shmem_show_mpol(seq, sbinfo->mpol);
  1876. return 0;
  1877. }
  1878. #endif /* CONFIG_TMPFS */
  1879. static void shmem_put_super(struct super_block *sb)
  1880. {
  1881. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1882. percpu_counter_destroy(&sbinfo->used_blocks);
  1883. kfree(sbinfo);
  1884. sb->s_fs_info = NULL;
  1885. }
  1886. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  1887. {
  1888. struct inode *inode;
  1889. struct dentry *root;
  1890. struct shmem_sb_info *sbinfo;
  1891. int err = -ENOMEM;
  1892. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1893. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  1894. L1_CACHE_BYTES), GFP_KERNEL);
  1895. if (!sbinfo)
  1896. return -ENOMEM;
  1897. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  1898. sbinfo->uid = current_fsuid();
  1899. sbinfo->gid = current_fsgid();
  1900. sb->s_fs_info = sbinfo;
  1901. #ifdef CONFIG_TMPFS
  1902. /*
  1903. * Per default we only allow half of the physical ram per
  1904. * tmpfs instance, limiting inodes to one per page of lowmem;
  1905. * but the internal instance is left unlimited.
  1906. */
  1907. if (!(sb->s_flags & MS_NOUSER)) {
  1908. sbinfo->max_blocks = shmem_default_max_blocks();
  1909. sbinfo->max_inodes = shmem_default_max_inodes();
  1910. if (shmem_parse_options(data, sbinfo, false)) {
  1911. err = -EINVAL;
  1912. goto failed;
  1913. }
  1914. }
  1915. sb->s_export_op = &shmem_export_ops;
  1916. #else
  1917. sb->s_flags |= MS_NOUSER;
  1918. #endif
  1919. spin_lock_init(&sbinfo->stat_lock);
  1920. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  1921. goto failed;
  1922. sbinfo->free_inodes = sbinfo->max_inodes;
  1923. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1924. sb->s_blocksize = PAGE_CACHE_SIZE;
  1925. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1926. sb->s_magic = TMPFS_MAGIC;
  1927. sb->s_op = &shmem_ops;
  1928. sb->s_time_gran = 1;
  1929. #ifdef CONFIG_TMPFS_XATTR
  1930. sb->s_xattr = shmem_xattr_handlers;
  1931. #endif
  1932. #ifdef CONFIG_TMPFS_POSIX_ACL
  1933. sb->s_flags |= MS_POSIXACL;
  1934. #endif
  1935. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  1936. if (!inode)
  1937. goto failed;
  1938. inode->i_uid = sbinfo->uid;
  1939. inode->i_gid = sbinfo->gid;
  1940. root = d_alloc_root(inode);
  1941. if (!root)
  1942. goto failed_iput;
  1943. sb->s_root = root;
  1944. return 0;
  1945. failed_iput:
  1946. iput(inode);
  1947. failed:
  1948. shmem_put_super(sb);
  1949. return err;
  1950. }
  1951. static struct kmem_cache *shmem_inode_cachep;
  1952. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1953. {
  1954. struct shmem_inode_info *info;
  1955. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  1956. if (!info)
  1957. return NULL;
  1958. return &info->vfs_inode;
  1959. }
  1960. static void shmem_destroy_callback(struct rcu_head *head)
  1961. {
  1962. struct inode *inode = container_of(head, struct inode, i_rcu);
  1963. INIT_LIST_HEAD(&inode->i_dentry);
  1964. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1965. }
  1966. static void shmem_destroy_inode(struct inode *inode)
  1967. {
  1968. if ((inode->i_mode & S_IFMT) == S_IFREG)
  1969. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1970. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  1971. }
  1972. static void shmem_init_inode(void *foo)
  1973. {
  1974. struct shmem_inode_info *info = foo;
  1975. inode_init_once(&info->vfs_inode);
  1976. }
  1977. static int shmem_init_inodecache(void)
  1978. {
  1979. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1980. sizeof(struct shmem_inode_info),
  1981. 0, SLAB_PANIC, shmem_init_inode);
  1982. return 0;
  1983. }
  1984. static void shmem_destroy_inodecache(void)
  1985. {
  1986. kmem_cache_destroy(shmem_inode_cachep);
  1987. }
  1988. static const struct address_space_operations shmem_aops = {
  1989. .writepage = shmem_writepage,
  1990. .set_page_dirty = __set_page_dirty_no_writeback,
  1991. #ifdef CONFIG_TMPFS
  1992. .write_begin = shmem_write_begin,
  1993. .write_end = shmem_write_end,
  1994. #endif
  1995. .migratepage = migrate_page,
  1996. .error_remove_page = generic_error_remove_page,
  1997. };
  1998. static const struct file_operations shmem_file_operations = {
  1999. .mmap = shmem_mmap,
  2000. #ifdef CONFIG_TMPFS
  2001. .llseek = generic_file_llseek,
  2002. .read = do_sync_read,
  2003. .write = do_sync_write,
  2004. .aio_read = shmem_file_aio_read,
  2005. .aio_write = generic_file_aio_write,
  2006. .fsync = noop_fsync,
  2007. .splice_read = shmem_file_splice_read,
  2008. .splice_write = generic_file_splice_write,
  2009. #endif
  2010. };
  2011. static const struct inode_operations shmem_inode_operations = {
  2012. .setattr = shmem_setattr,
  2013. .truncate_range = shmem_truncate_range,
  2014. #ifdef CONFIG_TMPFS_XATTR
  2015. .setxattr = shmem_setxattr,
  2016. .getxattr = shmem_getxattr,
  2017. .listxattr = shmem_listxattr,
  2018. .removexattr = shmem_removexattr,
  2019. #endif
  2020. };
  2021. static const struct inode_operations shmem_dir_inode_operations = {
  2022. #ifdef CONFIG_TMPFS
  2023. .create = shmem_create,
  2024. .lookup = simple_lookup,
  2025. .link = shmem_link,
  2026. .unlink = shmem_unlink,
  2027. .symlink = shmem_symlink,
  2028. .mkdir = shmem_mkdir,
  2029. .rmdir = shmem_rmdir,
  2030. .mknod = shmem_mknod,
  2031. .rename = shmem_rename,
  2032. #endif
  2033. #ifdef CONFIG_TMPFS_XATTR
  2034. .setxattr = shmem_setxattr,
  2035. .getxattr = shmem_getxattr,
  2036. .listxattr = shmem_listxattr,
  2037. .removexattr = shmem_removexattr,
  2038. #endif
  2039. #ifdef CONFIG_TMPFS_POSIX_ACL
  2040. .setattr = shmem_setattr,
  2041. #endif
  2042. };
  2043. static const struct inode_operations shmem_special_inode_operations = {
  2044. #ifdef CONFIG_TMPFS_XATTR
  2045. .setxattr = shmem_setxattr,
  2046. .getxattr = shmem_getxattr,
  2047. .listxattr = shmem_listxattr,
  2048. .removexattr = shmem_removexattr,
  2049. #endif
  2050. #ifdef CONFIG_TMPFS_POSIX_ACL
  2051. .setattr = shmem_setattr,
  2052. #endif
  2053. };
  2054. static const struct super_operations shmem_ops = {
  2055. .alloc_inode = shmem_alloc_inode,
  2056. .destroy_inode = shmem_destroy_inode,
  2057. #ifdef CONFIG_TMPFS
  2058. .statfs = shmem_statfs,
  2059. .remount_fs = shmem_remount_fs,
  2060. .show_options = shmem_show_options,
  2061. #endif
  2062. .evict_inode = shmem_evict_inode,
  2063. .drop_inode = generic_delete_inode,
  2064. .put_super = shmem_put_super,
  2065. };
  2066. static const struct vm_operations_struct shmem_vm_ops = {
  2067. .fault = shmem_fault,
  2068. #ifdef CONFIG_NUMA
  2069. .set_policy = shmem_set_policy,
  2070. .get_policy = shmem_get_policy,
  2071. #endif
  2072. };
  2073. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2074. int flags, const char *dev_name, void *data)
  2075. {
  2076. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2077. }
  2078. static struct file_system_type shmem_fs_type = {
  2079. .owner = THIS_MODULE,
  2080. .name = "tmpfs",
  2081. .mount = shmem_mount,
  2082. .kill_sb = kill_litter_super,
  2083. };
  2084. int __init shmem_init(void)
  2085. {
  2086. int error;
  2087. error = bdi_init(&shmem_backing_dev_info);
  2088. if (error)
  2089. goto out4;
  2090. error = shmem_init_inodecache();
  2091. if (error)
  2092. goto out3;
  2093. error = register_filesystem(&shmem_fs_type);
  2094. if (error) {
  2095. printk(KERN_ERR "Could not register tmpfs\n");
  2096. goto out2;
  2097. }
  2098. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2099. shmem_fs_type.name, NULL);
  2100. if (IS_ERR(shm_mnt)) {
  2101. error = PTR_ERR(shm_mnt);
  2102. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2103. goto out1;
  2104. }
  2105. return 0;
  2106. out1:
  2107. unregister_filesystem(&shmem_fs_type);
  2108. out2:
  2109. shmem_destroy_inodecache();
  2110. out3:
  2111. bdi_destroy(&shmem_backing_dev_info);
  2112. out4:
  2113. shm_mnt = ERR_PTR(error);
  2114. return error;
  2115. }
  2116. #else /* !CONFIG_SHMEM */
  2117. /*
  2118. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2119. *
  2120. * This is intended for small system where the benefits of the full
  2121. * shmem code (swap-backed and resource-limited) are outweighed by
  2122. * their complexity. On systems without swap this code should be
  2123. * effectively equivalent, but much lighter weight.
  2124. */
  2125. #include <linux/ramfs.h>
  2126. static struct file_system_type shmem_fs_type = {
  2127. .name = "tmpfs",
  2128. .mount = ramfs_mount,
  2129. .kill_sb = kill_litter_super,
  2130. };
  2131. int __init shmem_init(void)
  2132. {
  2133. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2134. shm_mnt = kern_mount(&shmem_fs_type);
  2135. BUG_ON(IS_ERR(shm_mnt));
  2136. return 0;
  2137. }
  2138. int shmem_unuse(swp_entry_t swap, struct page *page)
  2139. {
  2140. return 0;
  2141. }
  2142. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2143. {
  2144. return 0;
  2145. }
  2146. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2147. {
  2148. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2149. }
  2150. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2151. #define shmem_vm_ops generic_file_vm_ops
  2152. #define shmem_file_operations ramfs_file_operations
  2153. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2154. #define shmem_acct_size(flags, size) 0
  2155. #define shmem_unacct_size(flags, size) do {} while (0)
  2156. #endif /* CONFIG_SHMEM */
  2157. /* common code */
  2158. /**
  2159. * shmem_file_setup - get an unlinked file living in tmpfs
  2160. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2161. * @size: size to be set for the file
  2162. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2163. */
  2164. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2165. {
  2166. int error;
  2167. struct file *file;
  2168. struct inode *inode;
  2169. struct path path;
  2170. struct dentry *root;
  2171. struct qstr this;
  2172. if (IS_ERR(shm_mnt))
  2173. return (void *)shm_mnt;
  2174. if (size < 0 || size > MAX_LFS_FILESIZE)
  2175. return ERR_PTR(-EINVAL);
  2176. if (shmem_acct_size(flags, size))
  2177. return ERR_PTR(-ENOMEM);
  2178. error = -ENOMEM;
  2179. this.name = name;
  2180. this.len = strlen(name);
  2181. this.hash = 0; /* will go */
  2182. root = shm_mnt->mnt_root;
  2183. path.dentry = d_alloc(root, &this);
  2184. if (!path.dentry)
  2185. goto put_memory;
  2186. path.mnt = mntget(shm_mnt);
  2187. error = -ENOSPC;
  2188. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2189. if (!inode)
  2190. goto put_dentry;
  2191. d_instantiate(path.dentry, inode);
  2192. inode->i_size = size;
  2193. inode->i_nlink = 0; /* It is unlinked */
  2194. #ifndef CONFIG_MMU
  2195. error = ramfs_nommu_expand_for_mapping(inode, size);
  2196. if (error)
  2197. goto put_dentry;
  2198. #endif
  2199. error = -ENFILE;
  2200. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2201. &shmem_file_operations);
  2202. if (!file)
  2203. goto put_dentry;
  2204. return file;
  2205. put_dentry:
  2206. path_put(&path);
  2207. put_memory:
  2208. shmem_unacct_size(flags, size);
  2209. return ERR_PTR(error);
  2210. }
  2211. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2212. /**
  2213. * shmem_zero_setup - setup a shared anonymous mapping
  2214. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2215. */
  2216. int shmem_zero_setup(struct vm_area_struct *vma)
  2217. {
  2218. struct file *file;
  2219. loff_t size = vma->vm_end - vma->vm_start;
  2220. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2221. if (IS_ERR(file))
  2222. return PTR_ERR(file);
  2223. if (vma->vm_file)
  2224. fput(vma->vm_file);
  2225. vma->vm_file = file;
  2226. vma->vm_ops = &shmem_vm_ops;
  2227. vma->vm_flags |= VM_CAN_NONLINEAR;
  2228. return 0;
  2229. }
  2230. /**
  2231. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2232. * @mapping: the page's address_space
  2233. * @index: the page index
  2234. * @gfp: the page allocator flags to use if allocating
  2235. *
  2236. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2237. * with any new page allocations done using the specified allocation flags.
  2238. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2239. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2240. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2241. *
  2242. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2243. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2244. */
  2245. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2246. pgoff_t index, gfp_t gfp)
  2247. {
  2248. #ifdef CONFIG_SHMEM
  2249. struct inode *inode = mapping->host;
  2250. struct page *page;
  2251. int error;
  2252. BUG_ON(mapping->a_ops != &shmem_aops);
  2253. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2254. if (error)
  2255. page = ERR_PTR(error);
  2256. else
  2257. unlock_page(page);
  2258. return page;
  2259. #else
  2260. /*
  2261. * The tiny !SHMEM case uses ramfs without swap
  2262. */
  2263. return read_cache_page_gfp(mapping, index, gfp);
  2264. #endif
  2265. }
  2266. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);