shmem.c 64 KB

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