The phrase “x years before the mast” was used by sailors to indicate the length of time they’ve been in their profession. Common seamen slept in the forward part of the ship, i.e. before the main mast on sailing ships. Officers slept in the after part of the ship where they could get easy access to the tiller.