shmem.c 66 KB

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